/** * Retrieves the first request in the queue and builds the full REST URI * given the properties of this request before creating a new HTTP request, * signing it, and sending it to the container. * * @param client OpenSocialClient object with REST_BASE_URI property set * @return Object encapsulating the data requested from the container * @throws OpenSocialRequestException * @throws JSONException * @throws OAuthException * @throws IOException * @throws URISyntaxException */ private OpenSocialResponse submitRest(OpenSocialClient client) { String restBaseUri = client.getProperty(OpenSocialClient.Properties.REST_BASE_URI); OpenSocialRequest r = this.requests[0]; OpenSocialUrl requestUrl = new OpenSocialUrl(restBaseUri); requestUrl.addPathComponent(r.getRestPathComponent()); if (r.getParameter("userId") != null) { requestUrl.addPathComponent(r.getParameter("userId")); } if (r.getParameter("groupId") != null) { requestUrl.addPathComponent(r.getParameter("groupId")); } if (r.getParameter("appId") != null) { requestUrl.addPathComponent(r.getParameter("appId")); } OpenSocialHttpRequest request = new OpenSocialHttpRequest(requestUrl); OpenSocialRequestSigner.signRequest(request, client); String responseString = getHttpResponse(request); return(OpenSocialJsonParser.getResponse(responseString, r.getId())); }
/** * Extracts pertinent properties from passed OpenSocialClient object and * passes these along with OpenSocialHttpRequest object to a separate * method which does the actual signing. * * @param request OpenSocialHttpRequest object which contains both the URL * to sign as well as the POST body which must be included as a * parameter when signing POST requests * @param client OpenSocialClient object with various properties, both * optional and required, used during the signing process * @throws OAuthException * @throws IOException * @throws URISyntaxException */ public static void signRequest( OpenSocialHttpRequest request, OpenSocialClient client) { String token = client.getProperty(OpenSocialClient.Properties.TOKEN); String viewerId = client.getProperty(OpenSocialClient.Properties.VIEWER_ID); String consumerKey = client.getProperty(OpenSocialClient.Properties.CONSUMER_KEY); String consumerSecret = client.getProperty(OpenSocialClient.Properties.CONSUMER_SECRET); signRequest(request, token, viewerId, consumerKey, consumerSecret); }
/** * Adds optional query string parameters to request URL if present, then * passes the passed OpenSocialHttpRequest to a separate method which * does the actual signing. * * @param request OpenSocialHttpRequest object which contains both the URL * to sign as well as the POST body which must be included as a * parameter when signing POST requests * @param token Security token which can be borrowed from a running gadget * and appended to the URL as a query string parameter instead of * signing the request. These types of tokens are typically short- * lived and must be refreshed often, making signing much more * practical. * @param viewerId ID of the user currently using the application (or for * whom the application is making requests on behalf of). The ID * is appended to the URL as a query string parameter so it can * be signed with the rest of the URL. * @param consumerKey Application key assigned and used by containers to * uniquely identify applications * @param consumerSecret Secret key shared between application owner and * container. Used to generate the signature which is attached to * the request so containers can verify the authenticity of the * requests made by the client application. * @throws OAuthException * @throws IOException * @throws URISyntaxException */ public static void signRequest( OpenSocialHttpRequest request, String token, String viewerId, String consumerKey, String consumerSecret) { OpenSocialUrl requestUrl = request.getUrl(); if (!String.IsNullOrEmpty(viewerId)) { requestUrl.addQueryStringParameter("xoauth_requestor_id", viewerId); } if (!String.IsNullOrEmpty(token)) { requestUrl.addQueryStringParameter("st", token); } signRequest(request, consumerKey, consumerSecret); }
/** * Returns the text returned by the container after executing the passed * OpenSocialHttpRequest object. * * @param r OpenSocialHttpRequest object to execute * @return * @throws OpenSocialRequestException if the status code returned by the * container is not 200 OK. * @throws IOException */ private String getHttpResponse(OpenSocialHttpRequest r) { if (r != null) { int requestStatus = r.execute(); if (requestStatus == 200) { return(r.getResponseString()); } else { throw new OpenSocialRequestException( "Request returned error code: " + requestStatus); } } return(null); }
/** * Signs the URL associated with the passed request object using the passed * consumer key and secret in accordance with the OAuth specification and * appends signature and other required parameters to the URL as query * string parameters. * * @param request OpenSocialHttpRequest object which contains both the URL * to sign as well as the POST body which must be included as a * parameter when signing POST requests * @param consumerKey Application key assigned and used by containers to * uniquely identify applications * @param consumerSecret Secret key shared between application owner and * container. Used to generate the signature which is attached to * the request so containers can verify the authenticity of the * requests made by the client application. * @throws OAuthException * @throws IOException * @throws URISyntaxException */ public static void signRequest( OpenSocialHttpRequest request, String consumerKey, String consumerSecret) { String postBody = request.getPostBody(); String requestMethod = request.getMethod(); OpenSocialUrl requestUrl = request.getUrl(); if (!String.IsNullOrEmpty(consumerKey) && !String.IsNullOrEmpty(consumerSecret)) { OAuthMessage message = new OAuthMessage(requestMethod, requestUrl.ToString(), null); if (!String.IsNullOrEmpty(postBody)) { message.addParameter(postBody, ""); } OAuthConsumer consumer = new OAuthConsumer(null, consumerKey, consumerSecret, null); consumer.setProperty(OAuth.OAUTH_SIGNATURE_METHOD, OAuth.HMAC_SHA1); OAuthAccessor accessor = new OAuthAccessor(consumer); accessor.accessToken = ""; message.addRequiredParameters(accessor); foreach (var p in message.getParameters()) { if (!p.Key.Equals(postBody)) { requestUrl.addQueryStringParameter( OAuth.percentEncode(new List <string> { p.Key }), OAuth.percentEncode(new List <string> { p.Value })); } } } }
/** * Collects all of the queued requests and encodes them into a single JSON * string before creating a new HTTP request, attaching this string to the * request body, signing it, and sending it to the container. * * @param client OpenSocialClient object with RPC_ENDPOINT property set * @return Object encapsulating the data requested from the container * @throws OpenSocialRequestException * @throws JSONException * @throws OAuthException * @throws IOException * @throws URISyntaxException */ private OpenSocialResponse submitRpc(OpenSocialClient client) { String rpcEndpoint = client.getProperty(OpenSocialClient.Properties.RPC_ENDPOINT); JsonArray requestArray = new JsonArray(); foreach (OpenSocialRequest r in this.requests) { requestArray.Put(JsonConvert.Import(r.getJsonEncoding())); } OpenSocialUrl requestUrl = new OpenSocialUrl(rpcEndpoint); OpenSocialHttpRequest request = new OpenSocialHttpRequest(requestUrl); request.setPostBody(requestArray.ToString()); OpenSocialRequestSigner.signRequest(request, client); String responseString = getHttpResponse(request); return(OpenSocialJsonParser.getResponse(responseString)); }
/** * Signs the URL associated with the passed request object using the passed * consumer key and secret in accordance with the OAuth specification and * appends signature and other required parameters to the URL as query * string parameters. * * @param request OpenSocialHttpRequest object which contains both the URL * to sign as well as the POST body which must be included as a * parameter when signing POST requests * @param consumerKey Application key assigned and used by containers to * uniquely identify applications * @param consumerSecret Secret key shared between application owner and * container. Used to generate the signature which is attached to * the request so containers can verify the authenticity of the * requests made by the client application. * @throws OAuthException * @throws IOException * @throws URISyntaxException */ public static void signRequest( OpenSocialHttpRequest request, String consumerKey, String consumerSecret) { String postBody = request.getPostBody(); String requestMethod = request.getMethod(); OpenSocialUrl requestUrl = request.getUrl(); if (!String.IsNullOrEmpty(consumerKey) && !String.IsNullOrEmpty(consumerSecret)) { OAuthMessage message = new OAuthMessage(requestMethod, requestUrl.ToString(), null); if (!String.IsNullOrEmpty(postBody)) { message.addParameter(postBody, ""); } OAuthConsumer consumer = new OAuthConsumer(null, consumerKey, consumerSecret, null); consumer.setProperty(OAuth.OAUTH_SIGNATURE_METHOD, OAuth.HMAC_SHA1); OAuthAccessor accessor = new OAuthAccessor(consumer); accessor.accessToken = ""; message.addRequiredParameters(accessor); foreach(var p in message.getParameters()) { if (!p.Key.Equals(postBody)) { requestUrl.addQueryStringParameter( OAuth.percentEncode(new List<string> { p.Key }), OAuth.percentEncode(new List<string> {p.Value})); } } } }
/** * Returns the text returned by the container after executing the passed * OpenSocialHttpRequest object. * * @param r OpenSocialHttpRequest object to execute * @return * @throws OpenSocialRequestException if the status code returned by the * container is not 200 OK. * @throws IOException */ private String getHttpResponse(OpenSocialHttpRequest r) { if (r != null) { int requestStatus = r.execute(); if (requestStatus == 200) { return r.getResponseString(); } else { throw new OpenSocialRequestException( "Request returned error code: " + requestStatus); } } return null; }
/** * Retrieves the first request in the queue and builds the full REST URI * given the properties of this request before creating a new HTTP request, * signing it, and sending it to the container. * * @param client OpenSocialClient object with REST_BASE_URI property set * @return Object encapsulating the data requested from the container * @throws OpenSocialRequestException * @throws JSONException * @throws OAuthException * @throws IOException * @throws URISyntaxException */ private OpenSocialResponse submitRest(OpenSocialClient client) { String restBaseUri = client.getProperty(OpenSocialClient.Properties.REST_BASE_URI); OpenSocialRequest r = this.requests[0]; OpenSocialUrl requestUrl = new OpenSocialUrl(restBaseUri); requestUrl.addPathComponent(r.getRestPathComponent()); if (r.getParameter("userId") != null) { requestUrl.addPathComponent(r.getParameter("userId")); } if (r.getParameter("groupId") != null) { requestUrl.addPathComponent(r.getParameter("groupId")); } if (r.getParameter("appId") != null) { requestUrl.addPathComponent(r.getParameter("appId")); } OpenSocialHttpRequest request = new OpenSocialHttpRequest(requestUrl); OpenSocialRequestSigner.signRequest(request, client); String responseString = getHttpResponse(request); return OpenSocialJsonParser.getResponse(responseString, r.getId()); }
/** * Collects all of the queued requests and encodes them into a single JSON * string before creating a new HTTP request, attaching this string to the * request body, signing it, and sending it to the container. * * @param client OpenSocialClient object with RPC_ENDPOINT property set * @return Object encapsulating the data requested from the container * @throws OpenSocialRequestException * @throws JSONException * @throws OAuthException * @throws IOException * @throws URISyntaxException */ private OpenSocialResponse submitRpc(OpenSocialClient client) { String rpcEndpoint = client.getProperty(OpenSocialClient.Properties.RPC_ENDPOINT); JsonArray requestArray = new JsonArray(); foreach(OpenSocialRequest r in this.requests) { requestArray.Put(JsonConvert.Import(r.getJsonEncoding())); } OpenSocialUrl requestUrl = new OpenSocialUrl(rpcEndpoint); OpenSocialHttpRequest request = new OpenSocialHttpRequest(requestUrl); request.setPostBody(requestArray.ToString()); OpenSocialRequestSigner.signRequest(request, client); String responseString = getHttpResponse(request); return OpenSocialJsonParser.getResponse(responseString); }