You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@oltu.apache.org by ja...@apache.org on 2016/03/28 19:16:01 UTC

svn commit: r1736908 - /oltu/trunk/oauth-2.0/client/src/main/java/org/apache/oltu/oauth2/client/URLConnectionClient.java

Author: jasha
Date: Mon Mar 28 17:16:01 2016
New Revision: 1736908

URL: http://svn.apache.org/viewvc?rev=1736908&view=rev
Log:
OLTU-195 URLConnectionClient PUT body content

Modified:
    oltu/trunk/oauth-2.0/client/src/main/java/org/apache/oltu/oauth2/client/URLConnectionClient.java

Modified: oltu/trunk/oauth-2.0/client/src/main/java/org/apache/oltu/oauth2/client/URLConnectionClient.java
URL: http://svn.apache.org/viewvc/oltu/trunk/oauth-2.0/client/src/main/java/org/apache/oltu/oauth2/client/URLConnectionClient.java?rev=1736908&r1=1736907&r2=1736908&view=diff
==============================================================================
--- oltu/trunk/oauth-2.0/client/src/main/java/org/apache/oltu/oauth2/client/URLConnectionClient.java (original)
+++ oltu/trunk/oauth-2.0/client/src/main/java/org/apache/oltu/oauth2/client/URLConnectionClient.java Mon Mar 28 17:16:01 2016
@@ -21,6 +21,14 @@
 
 package org.apache.oltu.oauth2.client;
 
+import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
+import org.apache.oltu.oauth2.client.response.OAuthClientResponse;
+import org.apache.oltu.oauth2.client.response.OAuthClientResponseFactory;
+import org.apache.oltu.oauth2.common.OAuth;
+import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
+import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
+import org.apache.oltu.oauth2.common.utils.OAuthUtils;
+
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -32,13 +40,8 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
-import org.apache.oltu.oauth2.client.response.OAuthClientResponse;
-import org.apache.oltu.oauth2.client.response.OAuthClientResponseFactory;
-import org.apache.oltu.oauth2.common.OAuth;
-import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
-import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
-import org.apache.oltu.oauth2.common.utils.OAuthUtils;
+import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
+import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED;
 
 
 /**
@@ -58,9 +61,9 @@ public class URLConnectionClient impleme
             throws OAuthSystemException, OAuthProblemException {
 
         InputStream responseBody = null;
-        URLConnection c = null;
+        URLConnection c;
         Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
-        int responseCode = 0;
+        int responseCode;
         try {
             URL url = new URL(request.getLocationUri());
 
@@ -81,25 +84,18 @@ public class URLConnectionClient impleme
                     }
                 }
 
-                if (!OAuthUtils.isEmpty(requestMethod)) {
-                    httpURLConnection.setRequestMethod(requestMethod);
-                    if (requestMethod.equals(OAuth.HttpMethod.POST)) {
-                        httpURLConnection.setDoOutput(true);
-                        OutputStream ost = httpURLConnection.getOutputStream();
-                        PrintWriter pw = new PrintWriter(ost);
-                        pw.print(request.getBody());
-                        pw.flush();
-                        pw.close();
-                    }
-                } else {
+                if (OAuthUtils.isEmpty(requestMethod)) {
                     httpURLConnection.setRequestMethod(OAuth.HttpMethod.GET);
+                } else {
+                    httpURLConnection.setRequestMethod(requestMethod);
+                    setRequestBody(request, requestMethod, httpURLConnection);
                 }
 
                 httpURLConnection.connect();
 
                 InputStream inputStream;
                 responseCode = httpURLConnection.getResponseCode();
-                if (responseCode == 400 || responseCode == 401) {
+                if (responseCode == SC_BAD_REQUEST || responseCode == SC_UNAUTHORIZED) {
                     inputStream = httpURLConnection.getErrorStream();
                 } else {
                     inputStream = httpURLConnection.getInputStream();
@@ -116,6 +112,23 @@ public class URLConnectionClient impleme
                 .createCustomResponse(responseBody, c.getContentType(), responseCode, responseHeaders, responseClass);
     }
 
+    private void setRequestBody(OAuthClientRequest request, String requestMethod, HttpURLConnection httpURLConnection)
+            throws IOException {
+        String requestBody = request.getBody();
+        if (OAuthUtils.isEmpty(requestBody)) {
+            return;
+        }
+
+        if (OAuth.HttpMethod.POST.equals(requestMethod) || OAuth.HttpMethod.PUT.equals(requestMethod)) {
+            httpURLConnection.setDoOutput(true);
+            OutputStream ost = httpURLConnection.getOutputStream();
+            PrintWriter pw = new PrintWriter(ost);
+            pw.print(requestBody);
+            pw.flush();
+            pw.close();
+        }
+    }
+
     @Override
     public void shutdown() {
         // Nothing to do here