Search in sources :

Example 1 with OAuthResponseMessage

use of net.oauth.client.OAuthResponseMessage in project cxf by apache.

the class GetProtectedResourceController method handleRequest.

@RequestMapping("/getProtectedResource")
protected ModelAndView handleRequest(@ModelAttribute("oAuthParams") OAuthParams oAuthParams, HttpServletRequest request) throws Exception {
    OAuthServiceProvider provider = new OAuthServiceProvider(oAuthParams.getTemporaryCredentialsEndpoint(), oAuthParams.getResourceOwnerAuthorizationEndpoint(), null);
    OAuthConsumer consumer = new OAuthConsumer(null, oAuthParams.getClientID(), oAuthParams.getClientSecret(), provider);
    OAuthAccessor accessor = new OAuthAccessor(consumer);
    accessor.requestToken = oAuthParams.getOauthToken();
    accessor.tokenSecret = oAuthParams.getOauthTokenSecret();
    Map<String, String> parameters = new HashMap<>();
    parameters.put(OAuth.OAUTH_SIGNATURE_METHOD, oAuthParams.getSignatureMethod());
    parameters.put(OAuth.OAUTH_NONCE, UUID.randomUUID().toString());
    parameters.put(OAuth.OAUTH_TIMESTAMP, String.valueOf(System.currentTimeMillis() / 1000));
    parameters.put(OAuth.OAUTH_TOKEN, oAuthParams.getOauthToken());
    parameters.put(OAuth.OAUTH_CONSUMER_KEY, oAuthParams.getClientID());
    OAuthMessage msg = null;
    String method = request.getParameter("op");
    if ("GET".equals(method)) {
        msg = accessor.newRequestMessage(OAuthMessage.GET, oAuthParams.getGetResourceURL(), parameters.entrySet());
    } else {
        msg = accessor.newRequestMessage(OAuthMessage.POST, oAuthParams.getPostResourceURL(), parameters.entrySet());
    }
    OAuthClient client = new OAuthClient(new URLConnectionClient());
    msg = client.access(msg, ParameterStyle.QUERY_STRING);
    StringBuilder bodyBuffer = readBody(msg);
    oAuthParams.setResourceResponse(bodyBuffer.toString());
    String authHeader = msg.getHeader("WWW-Authenticate");
    String oauthHeader = msg.getHeader("OAuth");
    String header = "";
    if (authHeader != null) {
        header += "WWW-Authenticate:" + authHeader;
    }
    if (oauthHeader != null) {
        header += "OAuth:" + oauthHeader;
    }
    oAuthParams.setHeader(header);
    oAuthParams.setResponseCode(((OAuthResponseMessage) msg).getHttpResponse().getStatusCode());
    return new ModelAndView("accessToken");
}
Also used : OAuthAccessor(net.oauth.OAuthAccessor) OAuthServiceProvider(net.oauth.OAuthServiceProvider) OAuthMessage(net.oauth.OAuthMessage) URLConnectionClient(net.oauth.client.URLConnectionClient) HashMap(java.util.HashMap) OAuthClient(net.oauth.client.OAuthClient) OAuthResponseMessage(net.oauth.client.OAuthResponseMessage) ModelAndView(org.springframework.web.servlet.ModelAndView) OAuthConsumer(net.oauth.OAuthConsumer) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

HashMap (java.util.HashMap)1 OAuthAccessor (net.oauth.OAuthAccessor)1 OAuthConsumer (net.oauth.OAuthConsumer)1 OAuthMessage (net.oauth.OAuthMessage)1 OAuthServiceProvider (net.oauth.OAuthServiceProvider)1 OAuthClient (net.oauth.client.OAuthClient)1 OAuthResponseMessage (net.oauth.client.OAuthResponseMessage)1 URLConnectionClient (net.oauth.client.URLConnectionClient)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1