private HttpResponse makeOAuthProblemReport(String code, String text, int rc) throws IOException { if (vagueErrors) { return new HttpResponseBuilder() .setHttpStatusCode(rc) .setResponseString("some vague error") .create(); } OAuthMessage msg = new OAuthMessage(null, null, null); msg.addParameter("oauth_problem", code); msg.addParameter("oauth_problem_advice", text); return new HttpResponseBuilder() .setHttpStatusCode(rc) .addHeader("WWW-Authenticate", msg.getAuthorizationHeader("realm")) .create(); }
/** * Construct an HTTP request from this OAuth message. * * @param style where to put the OAuth parameters, within the HTTP request */ public static HttpMessage newRequest(OAuthMessage from, ParameterStyle style) throws IOException { final boolean isPost = OAuthMessage.POST.equalsIgnoreCase(from.method); InputStream body = from.getBodyAsStream(); if (style == ParameterStyle.BODY && !(isPost && body == null)) { style = ParameterStyle.QUERY_STRING; } String url = from.URL; final List<Map.Entry<String, String>> headers = new ArrayList<Map.Entry<String, String>>(from.getHeaders()); switch (style) { case QUERY_STRING: url = OAuth.addParameters(url, from.getParameters()); break; case BODY: { byte[] form = OAuth.formEncode(from.getParameters()).getBytes(from.getBodyEncoding()); headers.add(new OAuth.Parameter(CONTENT_TYPE, OAuth.FORM_ENCODED)); headers.add(new OAuth.Parameter(CONTENT_LENGTH, form.length + "")); body = new ByteArrayInputStream(form); break; } case AUTHORIZATION_HEADER: headers.add(new OAuth.Parameter("Authorization", from.getAuthorizationHeader(null))); // Find the non-OAuth parameters: List<Map.Entry<String, String>> others = from.getParameters(); if (others != null && !others.isEmpty()) { others = new ArrayList<Map.Entry<String, String>>(others); for (Iterator<Map.Entry<String, String>> p = others.iterator(); p.hasNext(); ) { if (p.next().getKey().startsWith("oauth_")) { p.remove(); } } // Place the non-OAuth parameters elsewhere in the request: if (isPost && body == null) { byte[] form = OAuth.formEncode(others).getBytes(from.getBodyEncoding()); headers.add(new OAuth.Parameter(CONTENT_TYPE, OAuth.FORM_ENCODED)); headers.add(new OAuth.Parameter(CONTENT_LENGTH, form.length + "")); body = new ByteArrayInputStream(form); } else { url = OAuth.addParameters(url, others); } } break; } HttpMessage httpRequest = new HttpMessage(from.method, new URL(url), body); httpRequest.headers.addAll(headers); return httpRequest; }