List of usage examples for org.apache.http.client.fluent Request addHeader
public Request addHeader(final String name, final String value)
From source file:photosharing.api.oauth.OAuth20Handler.java
/** * renews an access token with the user's data * /*from ww w . j av a2 s .c o m*/ * @param oData * the current OAuth 2.0 data. * @return {OAuth20Data} or null * @throws IOException */ public OAuth20Data renewAccessToken(OAuth20Data oData) throws IOException { logger.finest("renewAccessToken activated"); Configuration config = Configuration.getInstance(null); String body = this.generateRenewAccessTokenRequestBody(oData.getAccessToken(), oData.getRefreshToken(), oData.getIssuedOn(), oData.getExpiresIn()); // Builds the URL in a StringBuilder StringBuilder builder = new StringBuilder(); builder.append(config.getValue(Configuration.BASEURL)); builder.append(TOKENURL); Request post = Request.Post(builder.toString()); post.addHeader("Content-Type", ContentType.APPLICATION_FORM_URLENCODED.getMimeType()); post.body(new StringEntity(body)); /** * Block is executed if there is a trace */ logger.info("URL Encoded body is " + body); logger.info("Token URL is " + builder.toString()); /** * Executes with a wrapped executor */ Executor exec = ExecutorUtil.getExecutor(); Response apiResponse = exec.execute(post); HttpResponse hr = apiResponse.returnResponse(); /** * Check the status codes and if 200, convert to String and process the * response body */ int statusCode = hr.getStatusLine().getStatusCode(); if (statusCode == 200) { InputStream in = hr.getEntity().getContent(); String x = IOUtils.toString(in); oData = OAuth20Data.createInstance(x); } else { logger.warning("OAuth20Data status code " + statusCode); } return oData; }
From source file:photosharing.api.oauth.OAuth20Handler.java
/** * gets an access token based on the code * //from w w w . ja v a2s .co m * @param code * - the >254 character code representing temporary credentials * @return the OAuth 20 configuration for the user requesting * @throws IOException */ public OAuth20Data getAccessToken(String code) throws IOException { logger.info("getAccessToken activated"); OAuth20Data oData = null; Configuration config = Configuration.getInstance(null); String body = this.generateAccessTokenRequestBody(config.getValue(Configuration.CLIENTID), config.getValue(Configuration.CLIENTSECRET), config.getValue(Configuration.CALLBACKURL), code); // Builds the URL in a StringBuilder StringBuilder builder = new StringBuilder(); builder.append(config.getValue(Configuration.BASEURL)); builder.append(TOKENURL); Request post = Request.Post(builder.toString()); post.addHeader("Content-Type", ContentType.APPLICATION_FORM_URLENCODED.getMimeType()); post.body(new StringEntity(body)); /** * Block is executed if there is a trace */ logger.info("URL Encoded body is " + body); logger.info("Token URL is " + builder.toString()); /** * Executes with a wrapped executor */ Executor exec = ExecutorUtil.getExecutor(); Response apiResponse = exec.execute(post); HttpResponse hr = apiResponse.returnResponse(); /** * Check the status codes and if 200, convert to String and process the * response body */ int statusCode = hr.getStatusLine().getStatusCode(); if (statusCode == 200) { InputStream in = hr.getEntity().getContent(); String x = IOUtils.toString(in); oData = OAuth20Data.createInstance(x); } else { logger.warning("OAuth20Data status code " + statusCode); } return oData; }
From source file:photosharing.api.conx.UploadFileDefinition.java
/** * get nonce as described with nonce <a href="http://ibm.co/1fG83gY">Get a * Cryptographic Key</a>// ww w . j ava 2 s . c o m * * @param bearer */ private String getNonce(String bearer, HttpServletResponse response) { String nonce = ""; // Build the Request Request get = Request.Get(getNonceUrl()); get.addHeader("Authorization", "Bearer " + bearer); try { Executor exec = ExecutorUtil.getExecutor(); Response apiResponse = exec.execute(get); HttpResponse hr = apiResponse.returnResponse(); /** * Check the status codes and if 200, convert to String */ int code = hr.getStatusLine().getStatusCode(); // Session is no longer valid or access token is expired if (code == HttpStatus.SC_FORBIDDEN) { response.sendRedirect("./api/logout"); } // User is not authorized else if (code == HttpStatus.SC_UNAUTHORIZED) { response.setStatus(HttpStatus.SC_UNAUTHORIZED); } else if (code == HttpStatus.SC_OK) { InputStream in = hr.getEntity().getContent(); nonce = IOUtils.toString(in); } } catch (IOException e) { response.setHeader("X-Application-Error", e.getClass().getName()); response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); logger.severe("IOException " + e.toString()); } return nonce; }
From source file:org.vas.test.rest.RestImpl.java
protected void configureRequest(Request request) { request.connectTimeout(TIMEOUT).socketTimeout(TIMEOUT).userAgent(USER_AGENT); defaultHeaders.forEach((header, value) -> request.addHeader(header, value)); }
From source file:org.apache.james.jmap.methods.integration.cucumber.DownloadStepdefs.java
@When("^\"([^\"]*)\" checks for the availability of the attachment endpoint$") public void optionDownload(String username) throws Throwable { AccessToken accessToken = userStepdefs.tokenByUser.get(username); URI target = mainStepdefs.baseUri().setPath("/download/" + ONE_ATTACHMENT_EML_ATTACHEMENT_BLOB_ID).build(); Request request = Request.Options(target); if (accessToken != null) { request.addHeader("Authorization", accessToken.serialize()); }/*from w w w. j av a 2 s. c o m*/ response = request.execute().returnResponse(); }
From source file:org.jspare.jsdbc.JsdbcTransportImpl.java
/** * Builds the authentication.//from w w w.j a va 2 s . c o m * * @param request * the request * @param datasource * the datasource * @param credential * the credential * @return the request * @throws JsdbcException * the jsdbc exception */ private Request buildAuthentication(Request request, DataSource datasource, Credential credential) throws JsdbcException { if (credential == null) { credential = keymaker.provideDataSource(datasource).loadDiskCredentials(); } return request.addHeader(AUTHENTICATION_KEY, credential.getToken()); }
From source file:photosharing.api.conx.RecommendationDefinition.java
/** * get nonce as described with nonce <a href="http://ibm.co/1fG83gY">Get a * Cryptographic Key</a>/*from ww w .j av a 2 s .co m*/ * * @param bearer */ private String getNonce(String bearer, HttpServletResponse response) { String nonce = ""; // Build the Request Request get = Request.Get(getNonceUrl()); get.addHeader("Authorization", "Bearer " + bearer); try { Executor exec = ExecutorUtil.getExecutor(); Response apiResponse = exec.execute(get); HttpResponse hr = apiResponse.returnResponse(); /** * Check the status codes and if SC_OK (200), convert to String */ int code = hr.getStatusLine().getStatusCode(); // Session is no longer valid or access token is expired if (code == HttpStatus.SC_FORBIDDEN) { response.sendRedirect("./api/logout"); } // User is not authorized else if (code == HttpStatus.SC_UNAUTHORIZED) { response.setStatus(HttpStatus.SC_UNAUTHORIZED); } else if (code == HttpStatus.SC_OK) { InputStream in = hr.getEntity().getContent(); nonce = IOUtils.toString(in); } else { //Given a bad proxied request response.setStatus(HttpStatus.SC_BAD_GATEWAY); } } catch (IOException e) { response.setHeader("X-Application-Error", e.getClass().getName()); response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); logger.severe("IOException " + e.toString()); } return nonce; }
From source file:photosharing.api.conx.RecommendationDefinition.java
/** * unlike a file//w w w. j a va 2s . c o m * * Example URL * http://localhost:9080/photoSharing/api/like?r=off&lid=f8ad2a54 * -4d20-4b3b-ba3f-834e0b0cf90b&uid=bec24e93-8165-431d-bf38-0c668a5e6727 * maps to * https://apps.collabservdaily.swg.usma.ibm.com/files/basic/api/library/00c129c9-f3b6-4d22-9988-99e69d16d7a7/document/bf33a9b5-3042-46f0-a96e-b8742fced7a4/feed * * @param bearer * @param lid * @param uid * @param nonce * @param response */ public void unlike(String bearer, String pid, String lid, String nonce, HttpServletResponse response) { String apiUrl = getApiUrl() + "/library/" + lid + "/document/" + pid + "/feed"; try { String recommendation = generateRecommendationContent(); logger.info("like -> " + apiUrl + " " + recommendation); // Generate the Request post = Request.Post(apiUrl); post.addHeader("Authorization", "Bearer " + bearer); post.addHeader("X-Update-Nonce", nonce); post.addHeader("X-METHOD-OVERRIDE", "DELETE"); post.addHeader("Content-Type", "application/atom+xml"); ByteArrayEntity entity = new ByteArrayEntity(recommendation.getBytes("UTF-8")); post.body(entity); Executor exec = ExecutorUtil.getExecutor(); Response apiResponse = exec.execute(post); HttpResponse hr = apiResponse.returnResponse(); /** * Check the status codes */ int code = hr.getStatusLine().getStatusCode(); // Session is no longer valid or access token is expired if (code == HttpStatus.SC_FORBIDDEN) { response.sendRedirect("./api/logout"); } // User is not authorized else if (code == HttpStatus.SC_UNAUTHORIZED) { response.setStatus(HttpStatus.SC_UNAUTHORIZED); } // Default to SC_NO_CONTENT (204) else { response.setStatus(HttpStatus.SC_NO_CONTENT); } } catch (IOException e) { response.setHeader("X-Application-Error", e.getClass().getName()); response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); logger.severe("IOException " + e.toString()); } }
From source file:photosharing.api.conx.RecommendationDefinition.java
/** * like a file/* ww w.j a v a 2 s . c o m*/ * * Example URL * http://localhost:9080/photoSharing/api/like?r=on&lid=f8ad2a54- * 4d20-4b3b-ba3f-834e0b0cf90b&uid=bec24e93-8165-431d-bf38-0c668a5e6727 maps * to * https://apps.collabservdaily.swg.usma.ibm.com/files/basic/api/library/00c129c9-f3b6-4d22-9988-99e69d16d7a7/document/bf33a9b5-3042-46f0-a96e-b8742fced7a4/feed * * * @param bearer * @param pid * @param lid * @param nonce * @param response */ public void like(String bearer, String pid, String lid, String nonce, HttpServletResponse response) { String apiUrl = getApiUrl() + "/library/" + lid + "/document/" + pid + "/feed"; try { String recommendation = generateRecommendationContent(); logger.info("like -> " + apiUrl + " " + recommendation); // Generate the apiUrl for like Request post = Request.Post(apiUrl); post.addHeader("Authorization", "Bearer " + bearer); post.addHeader("X-Update-Nonce", nonce); post.addHeader("Content-Type", "application/atom+xml"); ByteArrayEntity entity = new ByteArrayEntity(recommendation.getBytes("UTF-8")); post.body(entity); Executor exec = ExecutorUtil.getExecutor(); Response apiResponse = exec.execute(post); HttpResponse hr = apiResponse.returnResponse(); /** * Check the status codes */ int code = hr.getStatusLine().getStatusCode(); logger.info("code " + code); // Session is no longer valid or access token is expired if (code == HttpStatus.SC_FORBIDDEN) { response.sendRedirect("./api/logout"); } // User is not authorized else if (code == HttpStatus.SC_UNAUTHORIZED) { response.setStatus(HttpStatus.SC_UNAUTHORIZED); } // Default to SC_NO_CONTENT (204) else { response.setStatus(HttpStatus.SC_NO_CONTENT); } } catch (IOException e) { response.setHeader("X-Application-Error", e.getClass().getName()); response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); logger.severe("IOException " + e.toString()); e.printStackTrace(); } }
From source file:org.apache.james.jmap.methods.integration.cucumber.DownloadStepdefs.java
private Request authenticatedDownloadRequest(URIBuilder uriBuilder, String blobId, String username) throws URISyntaxException { AccessToken accessToken = userStepdefs.tokenByUser.get(username); AttachmentAccessTokenKey key = new AttachmentAccessTokenKey(username, blobId); if (attachmentAccessTokens.containsKey(key)) { uriBuilder.addParameter("access_token", attachmentAccessTokens.get(key).serialize()); }/*from ww w.ja v a2 s. c o m*/ Request request = Request.Get(uriBuilder.build()); if (accessToken != null) { request.addHeader("Authorization", accessToken.serialize()); } return request; }