List of usage examples for org.apache.http.client.utils URIBuilder build
public URI build() throws URISyntaxException
From source file:com.stormpath.sdk.servlet.mvc.provider.DefaultAccountStoreModel.java
public DefaultAccountStoreModel(Directory directory, ProviderModel provider, String authorizeBaseUri) { this.directory = directory; this.providerModel = provider; if (providerModel instanceof OAuthProviderModel && authorizeBaseUri != null) { try {//w w w . j a va 2 s . c o m URIBuilder builder = new URIBuilder(authorizeBaseUri); builder.setPath("/authorize"); builder.addParameter("response_type", "stormpath_token"); builder.addParameter("account_store_href", directory.getHref()); authorizeUri = builder.build().toString(); } catch (URISyntaxException e) { throw new IllegalArgumentException("authorizeBaseUri must be value URI", e); } } else { authorizeUri = null; } }
From source file:com.ibm.nytimes.NewYorkTimes.java
public BestSellerList getList() throws Exception { BestSellerList returnedList = new BestSellerList(); try {/*from w ww. j av a2 s .co m*/ if (listName != null && listDate != null && url != null && apiKey != null) { RequestConfig config = RequestConfig.custom().setSocketTimeout(10 * 1000) .setConnectTimeout(10 * 1000).build(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(config).build(); URIBuilder builder = new URIBuilder(); builder.setScheme("http").setHost(url).setPath("/svc/books/v2/lists/" + listDate + "/" + listName) .setParameter("api-key", apiKey); URI uri = builder.build(); HttpGet httpGet = new HttpGet(uri); httpGet.setHeader("Content-Type", "text/plain"); HttpResponse httpResponse = httpclient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() == 200) { BufferedReader rd = new BufferedReader( new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8")); // Read all the books from the best seller list ObjectMapper mapper = new ObjectMapper(); returnedList = mapper.readValue(rd, BestSellerList.class); } else { logger.error("could not get list from ny times http code {}", httpResponse.getStatusLine().getStatusCode()); } } } catch (Exception e) { logger.error("could not get list from ny times {}", e.getMessage()); throw e; } return returnedList; }
From source file:com.google.appengine.tck.blobstore.BlobstoreServeTest.java
private void doTest(HttpClient client, URI uri, Set<Header> headers, Map<String, String> params, String expected) throws Exception { URIBuilder builder = new URIBuilder(uri); if (params != null) { for (Map.Entry<String, String> entry : params.entrySet()) { builder.addParameter(entry.getKey(), entry.getValue()); }/*ww w . j ava2s . c o m*/ } HttpGet get = new HttpGet(builder.build()); if (headers != null) { for (Header h : headers) { get.addHeader(h); } } HttpResponse response = client.execute(get); Assert.assertEquals(expected, EntityUtils.toString(response.getEntity())); }
From source file:com.cloud.utils.rest.HttpUriRequestBuilder.java
private URI buildUri() { try {// w w w .j av a2 s. co m final URIBuilder builder = new URIBuilder().setPath(path); for (final Map.Entry<String, String> entry : parameters.entrySet()) { builder.addParameter(entry.getKey(), entry.getValue()); } return builder.build(); } catch (final URISyntaxException e) { throw new IllegalArgumentException("Unable to build REST Service URI", e); } }
From source file:com.rackspacecloud.blueflood.inputs.handlers.HttpAnnotationsEndToEndTest.java
private HttpResponse postEvent(String requestBody, String tenantId) throws Exception { URIBuilder builder = new URIBuilder().setScheme("http").setHost("127.0.0.1").setPort(httpPort) .setPath("/v2.0/" + tenantId + "/events"); HttpPost post = new HttpPost(builder.build()); HttpEntity entity = new StringEntity(requestBody, ContentType.APPLICATION_JSON); post.setEntity(entity);//from w ww .j a v a2 s .com post.setHeader(Event.FieldLabels.tenantId.name(), tenantId); post.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); HttpResponse response = client.execute(post); return response; }
From source file:org.broadleafcommerce.core.catalog.service.CatalogURLServiceImpl.java
/** * Adds the fragment to the end of the path and optionally adds an id param depending upon * the value of appendIdToRelativeURI.//from ww w . j av a 2 s .co m */ protected String buildRelativeUrlWithParam(String currentUrl, String fragment, String idParam, String idValue) { try { URIBuilder builder = new URIBuilder(currentUrl); builder.setPath(builder.getPath() + "/" + fragment); if (appendIdToRelativeURI) { builder.setParameter(idParam, String.valueOf(idValue)); } return builder.build().toString(); } catch (URISyntaxException e) { return currentUrl; } }
From source file:ar.edu.ubp.das.src.chat.actions.UsuariosListAction.java
@Override public ForwardConfig execute(ActionMapping mapping, DynaActionForm form, HttpServletRequest request, HttpServletResponse response) throws SQLException, RuntimeException { try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { //prepare http get SalaBean sala = (SalaBean) request.getSession().getAttribute("sala"); String authToken = String.valueOf(request.getSession().getAttribute("token")); URIBuilder builder = new URIBuilder(); builder.setScheme("http").setHost("25.136.78.82").setPort(8080) .setPath("/usuarios-salas/sala/" + sala.getId()); HttpGet getRequest = new HttpGet(); getRequest.setURI(builder.build()); getRequest.addHeader("Authorization", "BEARER " + authToken); getRequest.addHeader("accept", "application/json"); CloseableHttpResponse getResponse = httpClient.execute(getRequest); HttpEntity responseEntity = getResponse.getEntity(); StatusLine responseStatus = getResponse.getStatusLine(); String restResp = EntityUtils.toString(responseEntity); if (responseStatus.getStatusCode() != 200) { throw new RuntimeException(restResp); }/*from w ww.j av a 2s.co m*/ //parse message data from response Gson gson = new Gson(); UsuarioBean[] usrList = gson.fromJson(restResp, UsuarioBean[].class); request.setAttribute("usuarios", usrList); return mapping.getForwardByName("success"); } catch (IOException | URISyntaxException | RuntimeException e) { request.setAttribute("message", "Error al intentar listar Usuarios " + e.getMessage()); response.setStatus(400); return mapping.getForwardByName("failure"); } }
From source file:com.squid.kraken.v4.auth.ChangePasswordServlet.java
private User getUser(HttpServletRequest request) throws URISyntaxException, ServiceException, ServerUnavailableException, IOException, SSORedirectException { URIBuilder builder = new URIBuilder(privateServerURL + "/rs/user"); builder.addParameter("access_token", request.getParameter("access_token")); HttpGet req = new HttpGet(builder.build()); User user = RequestHelper.processRequest(User.class, request, req); return user;/*from w w w .j a va 2s . com*/ }
From source file:org.metaeffekt.dcc.agent.DccAgentEndpoint.java
private HttpPut createPutRequest(String command, String deploymentId, String packageId, String unitId, byte[] payload) { StringBuilder sb = new StringBuilder("/"); sb.append(deploymentId).append("/"); if (!"clean".equals(command)) { sb.append("packages").append("/").append(packageId).append("/"); sb.append("units").append("/").append(unitId != null ? unitId + "/" : ""); }// w w w . jav a 2 s . c o m String paramsPart = sb.toString(); URIBuilder uriBuilder = getUriBuilder(); String path = String.format("/%s%s%s", PATH_ROOT, paramsPart, command); uriBuilder.setPath(path); URI uri; try { uri = uriBuilder.build(); } catch (URISyntaxException e) { throw new RuntimeException(e); } HttpPut put = new HttpPut(uri); if (payload != null && payload.length > 0) { put.setEntity(new ByteArrayEntity(payload)); } if (requestConfig != null) { put.setConfig(requestConfig); } return put; }
From source file:ar.edu.ubp.das.src.chat.actions.ExpulsarUsuarioAction.java
@Override public ForwardConfig execute(ActionMapping mapping, DynaActionForm form, HttpServletRequest request, HttpServletResponse response) throws SQLException, RuntimeException { try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { //get request data String id_usuario = form.getItem("id_usuario"); String id_sala = form.getItem("id_sala"); String authToken = String.valueOf(request.getSession().getAttribute("token")); URIBuilder builder = new URIBuilder(); builder.setScheme("http").setHost("25.136.78.82").setPort(8080) .setPath("/usuarios-salas/" + id_usuario + "/" + id_sala); HttpDelete delete = new HttpDelete(); delete.setURI(builder.build()); delete.addHeader("Authorization", "BEARER " + authToken); delete.addHeader("accept", "application/json"); CloseableHttpResponse deleteResponse = httpClient.execute(delete); HttpEntity responseEntity = deleteResponse.getEntity(); StatusLine responseStatus = deleteResponse.getStatusLine(); String restResp = EntityUtils.toString(responseEntity); if (responseStatus.getStatusCode() != 200) { throw new RuntimeException(restResp); }/*from w w w.j a v a2s . c o m*/ return mapping.getForwardByName("success"); } catch (IOException | URISyntaxException | RuntimeException e) { String id_usuario = form.getItem("id_usuario"); request.setAttribute("message", "Error al intentar expulsar usuario: " + id_usuario + "; " + e.getMessage()); response.setStatus(400); return mapping.getForwardByName("failure"); } }