/** * Send a request message to the service provider and get the response. * * @return the response * @throws IOException * failed to communicate with the service provider * @throws OAuthProblemException * the HTTP response status code was not 200 (OK) */ public OAuthMessage invoke(OAuthMessage request, net.oauth.ParameterStyle style) throws IOException, OAuthException { OAuthResponseMessage response = access(request, style); if ((response.getHttpResponse().getStatusCode() / 100) != 2) { throw response.toOAuthProblemException(); } return response; }
@Override protected void completeParameters() throws IOException { super.completeParameters(); String body = readBodyAsString(); if (body != null) { addParameters(OAuth.decodeForm(body.trim())); } }
OAuthResponseMessage(HttpResponseMessage http) throws IOException { super(http.method, http.url.toExternalForm(), null); this.http = http; getHeaders().addAll(http.headers); for (Map.Entry<String, String> header : http.headers) { if ("WWW-Authenticate".equalsIgnoreCase(header.getKey())) { for (OAuth.Parameter parameter : decodeAuthorization(header.getValue())) { if (!"realm".equalsIgnoreCase(parameter.getKey())) { addParameter(parameter); } } } } }
/** * Encapsulate this message as an exception. Read and close the body of this * message. */ public OAuthProblemException toOAuthProblemException() throws IOException { OAuthProblemException problem = new OAuthProblemException(); try { getParameters(); // decode the response body } catch (IOException ignored) { } problem.getParameters().putAll(getDump()); try { InputStream b = getBodyAsStream(); if (b != null) { b.close(); // release resources } } catch (IOException ignored) { } return problem; }
String responseBody; try { responseBody = oauthResponseMessage.readBodyAsString(); responseStatusCode = oauthResponseMessage.getHttpResponse().getStatusCode();
@Test public void testGetWebSudoProtectedResourcePermitsOAuthToByPassWebSudo() throws Exception { // setup the consumer first. setUpOAuthConsumer(); OAuthAccessor accessor = createAccessor(CONSUMER_KEY); OAuthClient client = new OAuthClient(new HttpClient4()); // make the request. OAuthMessage response = client.invoke(accessor, getResourceUri().toString(), ImmutableMap.of("oauth_token", "").entrySet()); // assert stuff assertEquals(HttpServletResponse.SC_OK, ((OAuthResponseMessage) response).getHttpResponse().getStatusCode()); assertEquals("hello", response.readBodyAsString()); }
@Override public void requireParameters(String... names) throws OAuthProblemException, IOException { try { super.requireParameters(names); } catch (OAuthProblemException problem) { problem.getParameters().putAll(getDump()); throw problem; } }
/** * Send a request and return the response. Don't try to decide whether the * response indicates success; merely return it. */ public OAuthResponseMessage access(OAuthMessage request, net.oauth.ParameterStyle style) throws IOException { HttpMessage httpRequest = HttpMessage.newRequest(request, style); HttpResponseMessage httpResponse = http.execute(httpRequest, httpParameters); httpResponse = HttpMessageDecoder.decode(httpResponse); return new OAuthResponseMessage(httpResponse); }