net.oauth.http.HttpMessage Java Examples

The following examples show how to use net.oauth.http.HttpMessage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractRobotTest.java    From incubator-retired-wave with Apache License 2.0 6 votes vote down vote up
public void testSubmit() throws Exception {
  HttpFetcher fetcher = mock(HttpFetcher.class);
  when(fetcher.execute(any(HttpMessage.class), anyMapOf(String.class, Object.class)))
      .thenReturn(new HttpResponse("POST", new URL("http://foo.google.com"), 0,
          new ByteArrayInputStream("[{\"id\":\"op1\",\"data\":{}}]".getBytes())));


  MockRobot robot = new MockRobot();
  robot.setupOAuth("consumerKey", "consumerSecret", "http://gmodules.com/api/rpc");

  WaveService service = new WaveService(fetcher, robot.computeHash());
  service.setupOAuth("consumerKey", "consumerSecret", "http://gmodules.com/api/rpc");

  OperationQueue opQueue = new OperationQueue();
  opQueue.appendOperation(OperationType.ROBOT_NOTIFY,
      Parameter.of(ParamsProperty.CAPABILITIES_HASH, "123"));
  Wavelet wavelet = mock(Wavelet.class);
  when(wavelet.getOperationQueue()).thenReturn(opQueue);

  assertEquals(1, opQueue.getPendingOperations().size());
  robot.submit(wavelet, "http://gmodules.com/api/rpc", service);
  assertEquals(0, opQueue.getPendingOperations().size());
  verify(fetcher, times(1)).execute(any(HttpMessage.class), anyMapOf(String.class, Object.class));
}
 
Example #2
Source File: OpenSocialHttpClient.java    From incubator-retired-wave with Apache License 2.0 6 votes vote down vote up
/**
 * Executes the request, sending the request body if applicable.
 * 
 * @param request
 * @return Response message
 * @throws IOException
 */
private OpenSocialHttpResponseMessage execute(OpenSocialHttpMessage request)
    throws IOException {
  final String method = request.method;
  final boolean isPut = PUT.equalsIgnoreCase(method);
  final boolean isPost = POST.equalsIgnoreCase(method);
  final boolean isDelete = DELETE.equalsIgnoreCase(method);

  final String bodyString = request.getBodyString();
  final String contentType = request.getHeader(HttpMessage.CONTENT_TYPE);
  final OpenSocialUrl url = request.getUrl();

  OpenSocialHttpResponseMessage response = null;
  if (isPut) {
    response = send("PUT", url, contentType, bodyString);
  } else if (isPost) {
    response = send("POST", url, contentType, bodyString);
  } else if (isDelete) {
    response = send("DELETE", url, contentType);
  } else {
    response = send("GET", url, contentType);
  }

  return response;
}
 
Example #3
Source File: AbstractRobotTest.java    From swellrt with Apache License 2.0 6 votes vote down vote up
public void testSubmit() throws Exception {
  HttpFetcher fetcher = mock(HttpFetcher.class);
  when(fetcher.execute(any(HttpMessage.class), anyMapOf(String.class, Object.class)))
      .thenReturn(new HttpResponse("POST", new URL("http://foo.google.com"), 0,
          new ByteArrayInputStream("[{\"id\":\"op1\",\"data\":{}}]".getBytes())));


  MockRobot robot = new MockRobot();
  robot.setupOAuth("consumerKey", "consumerSecret", "http://gmodules.com/api/rpc");

  WaveService service = new WaveService(fetcher, robot.computeHash());
  service.setupOAuth("consumerKey", "consumerSecret", "http://gmodules.com/api/rpc");

  OperationQueue opQueue = new OperationQueue();
  opQueue.appendOperation(OperationType.ROBOT_NOTIFY,
      Parameter.of(ParamsProperty.CAPABILITIES_HASH, "123"));
  Wavelet wavelet = mock(Wavelet.class);
  when(wavelet.getOperationQueue()).thenReturn(opQueue);

  assertEquals(1, opQueue.getPendingOperations().size());
  robot.submit(wavelet, "http://gmodules.com/api/rpc", service);
  assertEquals(0, opQueue.getPendingOperations().size());
  verify(fetcher, times(1)).execute(any(HttpMessage.class), anyMapOf(String.class, Object.class));
}
 
Example #4
Source File: OpenSocialHttpClient.java    From swellrt with Apache License 2.0 6 votes vote down vote up
/**
 * Executes the request, sending the request body if applicable.
 * 
 * @param request
 * @return Response message
 * @throws IOException
 */
private OpenSocialHttpResponseMessage execute(OpenSocialHttpMessage request)
    throws IOException {
  final String method = request.method;
  final boolean isPut = PUT.equalsIgnoreCase(method);
  final boolean isPost = POST.equalsIgnoreCase(method);
  final boolean isDelete = DELETE.equalsIgnoreCase(method);

  final String bodyString = request.getBodyString();
  final String contentType = request.getHeader(HttpMessage.CONTENT_TYPE);
  final OpenSocialUrl url = request.getUrl();

  OpenSocialHttpResponseMessage response = null;
  if (isPut) {
    response = send("PUT", url, contentType, bodyString);
  } else if (isPost) {
    response = send("POST", url, contentType, bodyString);
  } else if (isDelete) {
    response = send("DELETE", url, contentType);
  } else {
    response = send("GET", url, contentType);
  }

  return response;
}
 
Example #5
Source File: OpenSocialHttpClient.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
/**
 * Opens a new HTTP connection for the URL associated with this object.
 *
 * @param method
 * @param url
 * @return Opened connection
 * @throws IOException if URL is invalid, or unsupported
 */
private HttpURLConnection getConnection(String method, OpenSocialUrl url,
    String contentType) throws IOException {
  HttpURLConnection connection =
    (HttpURLConnection) new URL(url.toString()).openConnection();
  if (contentType != null && !contentType.equals("")) {
    connection.setRequestProperty(HttpMessage.CONTENT_TYPE, contentType);
  }
  connection.setRequestMethod(method);
  connection.setDoOutput(true);
  connection.connect();

  return connection;
}
 
Example #6
Source File: OAuthServlet.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static void handleException(HttpServletResponse response,
        Exception e, String realm, boolean sendBody) throws IOException,
        ServletException {
    if (e instanceof OAuthProblemException) {
        OAuthProblemException problem = (OAuthProblemException) e;
        Object httpCode = problem.getParameters().get(HttpMessage.STATUS_CODE);
        if (httpCode == null) {
            httpCode = PROBLEM_TO_HTTP_CODE.get(problem.getProblem());
        }
        if (httpCode == null) {
            httpCode = SC_FORBIDDEN;
        }
        response.reset();
        response.setStatus(Integer.parseInt(httpCode.toString()));
        OAuthMessage message = new OAuthMessage(null, null, problem
                .getParameters().entrySet());
        response.addHeader("WWW-Authenticate", message
                .getAuthorizationHeader(realm));
        if (sendBody) {
            sendForm(response, message.getParameters());
        }
    } else if (e instanceof IOException) {
        throw (IOException) e;
    } else if (e instanceof ServletException) {
        throw (ServletException) e;
    } else if (e instanceof RuntimeException) {
        throw (RuntimeException) e;
    } else {
        throw new ServletException(e);
    }
}
 
Example #7
Source File: OAuthClient.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #8
Source File: OAuthClient.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
    * Construct a request message, send it to the service provider and get the
    * response.
    * 
    * @param httpMethod
    *            the HTTP request method, or null to use the default method
    * @return the response
    * @throws URISyntaxException
    *             the given url isn't valid syntactically
    * @throws OAuthProblemException
    *             the HTTP response status code was not 200 (OK)
    */
   @SuppressWarnings("rawtypes")
public OAuthMessage invoke(OAuthAccessor accessor, String httpMethod,
           String url, Collection<? extends Map.Entry> parameters)
   throws IOException, OAuthException, URISyntaxException {
       OAuthMessage request = accessor.newRequestMessage(httpMethod, url, parameters);
       Object accepted = accessor.consumer.getProperty(OAuthConsumer.ACCEPT_ENCODING);
       if (accepted != null) {
           request.getHeaders().add(new OAuth.Parameter(HttpMessage.ACCEPT_ENCODING, accepted.toString()));
       }
       Object ps = accessor.consumer.getProperty(PARAMETER_STYLE);
       net.oauth.ParameterStyle style = (ps == null) ? net.oauth.ParameterStyle.BODY
               : Enum.valueOf(net.oauth.ParameterStyle.class, ps.toString());
       return invoke(request, style);
   }
 
Example #9
Source File: OAuthProblemException.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public int getHttpStatusCode() {
    Object code = getParameters().get(HttpMessage.STATUS_CODE);
    if (code == null) {
        return 200;
    } else if (code instanceof Number) { // the usual case
        return ((Number) code).intValue();
    } else {
        return Integer.parseInt(code.toString());
    }
}
 
Example #10
Source File: OAuthProblemException.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public String getMessage() {
    String msg = super.getMessage();
    if (msg != null)
        return msg;
    msg = getProblem();
    if (msg != null)
        return msg;
    Object response = getParameters().get(HttpMessage.RESPONSE);
    if (response != null) {
        msg = response.toString();
        int eol = msg.indexOf("\n");
        if (eol < 0) {
            eol = msg.indexOf("\r");
        }
        if (eol >= 0) {
            msg = msg.substring(0, eol);
        }
        msg = msg.trim();
        if (msg.length() > 0) {
            return msg;
        }
    }
    response = getHttpStatusCode();
    if (response != null) {
        return HttpMessage.STATUS_CODE + " " + response;
    }
    return null;
}
 
Example #11
Source File: OpenSocialHttpClient.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
/**
 * Executes the request, sending the request body if applicable.
 * 
 * @param request
 * @return Response message
 * @throws IOException
 */
@Override
public OpenSocialHttpResponseMessage execute(HttpMessage request, 
    Map<String,Object> parameters) throws IOException {
  
  // If a POST message, translates the body into a string.
  String body = null;
  if (request.getBody() != null) {
    body = new String(toByteArray(request.getBody()));
  }
  
  OpenSocialHttpMessage openSocialRequest = new OpenSocialHttpMessage(
      request.method, new OpenSocialUrl(request.url.toExternalForm()), body);
  return execute(openSocialRequest);
}
 
Example #12
Source File: OAuthProblemException.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public int getHttpStatusCode() {
    Object code = getParameters().get(HttpMessage.STATUS_CODE);
    if (code == null) {
        return 200;
    } else if (code instanceof Number) { // the usual case
        return ((Number) code).intValue();
    } else {
        return Integer.parseInt(code.toString());
    }
}
 
Example #13
Source File: OpenSocialHttpClient.java    From swellrt with Apache License 2.0 5 votes vote down vote up
/**
 * Opens a new HTTP connection for the URL associated with this object.
 *
 * @param method
 * @param url
 * @return Opened connection
 * @throws IOException if URL is invalid, or unsupported
 */
private HttpURLConnection getConnection(String method, OpenSocialUrl url,
    String contentType) throws IOException {
  HttpURLConnection connection =
    (HttpURLConnection) new URL(url.toString()).openConnection();
  if (contentType != null && !contentType.equals("")) {
    connection.setRequestProperty(HttpMessage.CONTENT_TYPE, contentType);
  }
  connection.setRequestMethod(method);
  connection.setDoOutput(true);
  connection.connect();

  return connection;
}
 
Example #14
Source File: OAuthProblemException.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public String getMessage() {
    String msg = super.getMessage();
    if (msg != null)
        return msg;
    msg = getProblem();
    if (msg != null)
        return msg;
    Object response = getParameters().get(HttpMessage.RESPONSE);
    if (response != null) {
        msg = response.toString();
        int eol = msg.indexOf("\n");
        if (eol < 0) {
            eol = msg.indexOf("\r");
        }
        if (eol >= 0) {
            msg = msg.substring(0, eol);
        }
        msg = msg.trim();
        if (msg.length() > 0) {
            return msg;
        }
    }
    response = getHttpStatusCode();
    if (response != null) {
        return HttpMessage.STATUS_CODE + " " + response;
    }
    return null;
}
 
Example #15
Source File: OpenSocialHttpClient.java    From swellrt with Apache License 2.0 5 votes vote down vote up
/**
 * Executes the request, sending the request body if applicable.
 * 
 * @param request
 * @return Response message
 * @throws IOException
 */
@Override
public OpenSocialHttpResponseMessage execute(HttpMessage request, 
    Map<String,Object> parameters) throws IOException {
  
  // If a POST message, translates the body into a string.
  String body = null;
  if (request.getBody() != null) {
    body = new String(toByteArray(request.getBody()));
  }
  
  OpenSocialHttpMessage openSocialRequest = new OpenSocialHttpMessage(
      request.method, new OpenSocialUrl(request.url.toExternalForm()), body);
  return execute(openSocialRequest);
}
 
Example #16
Source File: OAuthServlet.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static void handleException(HttpServletResponse response,
        Exception e, String realm, boolean sendBody) throws IOException,
        ServletException {
    if (e instanceof OAuthProblemException) {
        OAuthProblemException problem = (OAuthProblemException) e;
        Object httpCode = problem.getParameters().get(HttpMessage.STATUS_CODE);
        if (httpCode == null) {
            httpCode = PROBLEM_TO_HTTP_CODE.get(problem.getProblem());
        }
        if (httpCode == null) {
            httpCode = SC_FORBIDDEN;
        }
        response.reset();
        response.setStatus(Integer.parseInt(httpCode.toString()));
        OAuthMessage message = new OAuthMessage(null, null, problem
                .getParameters().entrySet());
        response.addHeader("WWW-Authenticate", message
                .getAuthorizationHeader(realm));
        if (sendBody) {
            sendForm(response, message.getParameters());
        }
    } else if (e instanceof IOException) {
        throw (IOException) e;
    } else if (e instanceof ServletException) {
        throw (ServletException) e;
    } else if (e instanceof RuntimeException) {
        throw (RuntimeException) e;
    } else {
        throw new ServletException(e);
    }
}
 
Example #17
Source File: OAuthClient.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #18
Source File: OAuthClient.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
    * Construct a request message, send it to the service provider and get the
    * response.
    * 
    * @param httpMethod
    *            the HTTP request method, or null to use the default method
    * @return the response
    * @throws URISyntaxException
    *             the given url isn't valid syntactically
    * @throws OAuthProblemException
    *             the HTTP response status code was not 200 (OK)
    */
   @SuppressWarnings("rawtypes")
public OAuthMessage invoke(OAuthAccessor accessor, String httpMethod,
           String url, Collection<? extends Map.Entry> parameters)
   throws IOException, OAuthException, URISyntaxException {
       OAuthMessage request = accessor.newRequestMessage(httpMethod, url, parameters);
       Object accepted = accessor.consumer.getProperty(OAuthConsumer.ACCEPT_ENCODING);
       if (accepted != null) {
           request.getHeaders().add(new OAuth.Parameter(HttpMessage.ACCEPT_ENCODING, accepted.toString()));
       }
       Object ps = accessor.consumer.getProperty(PARAMETER_STYLE);
       net.oauth.ParameterStyle style = (ps == null) ? net.oauth.ParameterStyle.BODY
               : Enum.valueOf(net.oauth.ParameterStyle.class, ps.toString());
       return invoke(request, style);
   }
 
Example #19
Source File: OAuthUtil.java    From incubator-retired-wave with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a {@link OAuthProblemException} with the given problem and the
 * correct status code. The problem should come from {@link OAuth.Problems}.
 */
public static OAuthProblemException newOAuthProblemException(String problem) {
  OAuthProblemException exception = new OAuthProblemException(problem);
  exception.setParameter(HttpMessage.STATUS_CODE, OAuth.Problems.TO_HTTP_CODE.get(problem));
  return exception;
}
 
Example #20
Source File: OAuthUtil.java    From swellrt with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a {@link OAuthProblemException} with the given problem and the
 * correct status code. The problem should come from {@link OAuth.Problems}.
 */
public static OAuthProblemException newOAuthProblemException(String problem) {
  OAuthProblemException exception = new OAuthProblemException(problem);
  exception.setParameter(HttpMessage.STATUS_CODE, OAuth.Problems.TO_HTTP_CODE.get(problem));
  return exception;
}
 
Example #21
Source File: OAuthMessage.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * The MIME type of the body of this message.
 * 
 * @return the MIME type, or null to indicate the type is unknown.
 */
public String getBodyType() {
    return getHeader(HttpMessage.CONTENT_TYPE);
}
 
Example #22
Source File: OAuthMessage.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * Construct an HTTP request from this OAuth message.
 * 
 * @param style
 *            where to put the OAuth parameters, within the HTTP request
 * @deprecated use HttpMessage.newRequest
 */
public HttpMessage toHttpRequest(OAuthClient.ParameterStyle style) throws IOException {
    return HttpMessage.newRequest(this, style.getReplacement());
}
 
Example #23
Source File: OAuthMessage.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * The character encoding of the body of this message.
 * 
 * @return the name of an encoding, or "ISO-8859-1" if no charset has been
 *         specified.
 */
public String getBodyEncoding() {
    return HttpMessage.DEFAULT_CHARSET;
}
 
Example #24
Source File: OAuthMessage.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * The MIME type of the body of this message.
 * 
 * @return the MIME type, or null to indicate the type is unknown.
 */
public String getBodyType() {
    return getHeader(HttpMessage.CONTENT_TYPE);
}
 
Example #25
Source File: OAuthMessage.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * Construct an HTTP request from this OAuth message.
 * 
 * @param style
 *            where to put the OAuth parameters, within the HTTP request
 * @deprecated use HttpMessage.newRequest
 */
public HttpMessage toHttpRequest(OAuthClient.ParameterStyle style) throws IOException {
    return HttpMessage.newRequest(this, style.getReplacement());
}
 
Example #26
Source File: OAuthMessage.java    From sakai with Educational Community License v2.0 2 votes vote down vote up
/**
 * The character encoding of the body of this message.
 * 
 * @return the name of an encoding, or "ISO-8859-1" if no charset has been
 *         specified.
 */
public String getBodyEncoding() {
    return HttpMessage.DEFAULT_CHARSET;
}