List of usage examples for org.apache.http.client.methods HttpRequestBase getURI
public URI getURI()
From source file:com.decody.android.core.json.JSONClient.java
private <T> T performCall(HttpRequestBase request, Class<T> classOfT) throws ResourceNotFoundException, UnauthorizedException { String url = request.getURI().toString(); T toReturn = null;// w w w . j a va 2 s . c om Log.i(TAG, "Calling to get the resource in the url: " + url); try { HttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) { Log.w(TAG, "Resource not found, throwing an exception"); throw new ResourceNotFoundException("Resource not found in " + url); } else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) { Log.w(TAG, "Not authorized to get the given resource"); throw new UnauthorizedException("Your user is not authorized to access to the resource: " + url); } else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { Log.w(TAG, "Bad request from the server"); throw new UnauthorizedException("Bad request to the resource: " + url); } HttpEntity entity = response.getEntity(); Reader reader; reader = new InputStreamReader(entity.getContent()); toReturn = (T) factory.newInstance().fromJson(reader, classOfT); } catch (IllegalStateException e) { Log.e(TAG, "Unexpected illegal state reading the content from the given resource: " + e.toString()); throw new ResourceNotFoundException( "Unexpected illegal state reading the given resource: " + e.toString()); } catch (ClientProtocolException e) { Log.e(TAG, "Unexpected communication exception: " + e.toString()); throw new ResourceNotFoundException("Unexpected communication problem with the given resource"); } catch (IOException e) { Log.e(TAG, "Unexpected communication reading exception: " + e.toString()); throw new ResourceNotFoundException("Unexpected communication reading problem with the given resource"); } catch (JsonSyntaxException e) { Log.e(TAG, "Unexpected response received from the server: " + e.toString()); throw new ResourceNotFoundException("Unexpected response received"); } return toReturn; }
From source file:com.ksc.http.apache.client.impl.ApacheDefaultHttpRequestFactoryTest.java
@Test public void query_params_in_uri_for_post_request_with_payload() throws IOException, URISyntaxException { final Request<Object> request = newDefaultRequest(HttpMethodName.POST); request.withParameter("foo", "bar"); final String payload = "dummy string stream"; request.setContent(new StringInputStream(payload)); HttpRequestBase requestBase = requestFactory.create(request, settings); Assert.assertThat(requestBase, Matchers.instanceOf(HttpPost.class)); Assert.assertEquals("foo=bar", requestBase.getURI().getQuery()); Assert.assertThat(requestBase, Matchers.instanceOf(HttpPost.class)); Assert.assertEquals(payload, IOUtils.toString(((HttpPost) requestBase).getEntity().getContent())); }
From source file:com.gsma.mobileconnect.utils.RestClient.java
/** * Builds a RestResponse from the given HttpResponse * * @param request The original request.//from ww w .jav a 2 s .c o m * @param closeableHttpResponse The HttpResponse to build the RestResponse for. * @return The RestResponse of the HttpResponse * @throws IOException */ private RestResponse buildRestResponse(HttpRequestBase request, CloseableHttpResponse closeableHttpResponse) throws IOException { String requestUri = request.getURI().toString(); int statusCode = closeableHttpResponse.getStatusLine().getStatusCode(); HeaderIterator headers = closeableHttpResponse.headerIterator(); List<KeyValuePair> headerList = new ArrayList<KeyValuePair>(3); while (headers.hasNext()) { Header header = headers.nextHeader(); headerList.add(new KeyValuePair(header.getName(), header.getValue())); } HttpEntity httpEntity = closeableHttpResponse.getEntity(); String responseData = EntityUtils.toString(httpEntity); return new RestResponse(requestUri, statusCode, headerList, responseData); }
From source file:com.facebook.presto.jdbc.ApacheQueryHttpClient.java
private RuntimeException requestFailedStatus(String task, HttpRequestBase request, String reason) { return new RuntimeException( format("Error " + task + " at %s failed with status %s", request.getURI(), reason)); }
From source file:com.eviware.soapui.impl.rest.actions.oauth.OltuOAuth2ClientFacade.java
private void appendAccessTokenToQuery(HttpRequestBase request, OAuthBearerClientRequest oAuthClientRequest) throws OAuthSystemException { String queryString = getQueryStringFromOAuthClientRequest(oAuthClientRequest); URI oldUri = request.getURI(); String requestQueryString = oldUri.getQuery() != null ? oldUri.getQuery() + "&" + queryString : queryString; try {//from w w w . j a va 2s . c om request.setURI(URIUtils.createURI(oldUri.getScheme(), oldUri.getHost(), oldUri.getPort(), oldUri.getRawPath(), requestQueryString, oldUri.getFragment())); } catch (URISyntaxException e) { throw new OAuthSystemException(e); } }
From source file:com.threatconnect.sdk.conn.HttpRequestExecutor.java
@Override public String execute(String path, HttpMethod type, Object obj) throws IOException { path += (path.contains("?") ? "&" : "?"); path += "createActivityLog=" + this.conn.getConfig().isActivityLogEnabled(); logger.trace("Path: " + path); String fullPath = this.conn.getConfig().getTcApiUrl() + path.replace("/api/", "/"); logger.trace("Full: " + type + ": " + fullPath); HttpRequestBase httpBase = getBase(fullPath, type); if (obj != null) applyEntityAsJSON(httpBase, obj); logger.trace("RawPath: " + httpBase.getURI().getPath()); logger.trace("Query: " + httpBase.getURI().getRawQuery()); logger.trace("Path: " + path); String headerPath = httpBase.getURI().getRawPath() + "?" + httpBase.getURI().getRawQuery(); logger.trace("HeaderPath: " + headerPath); ConnectionUtil.applyHeaders(this.conn.getConfig(), httpBase, type.toString(), headerPath); logger.trace("Request: " + httpBase.getRequestLine()); long startMs = System.currentTimeMillis(); CloseableHttpResponse response = this.conn.getApiClient().execute(httpBase); notifyListeners(type, fullPath, (System.currentTimeMillis() - startMs)); String result = null;/* w w w . j a v a 2 s .co m*/ try { logger.trace(response.getStatusLine().toString()); HttpEntity entity = response.getEntity(); logger.trace("Response Headers: " + Arrays.toString(response.getAllHeaders())); logger.trace("Content Encoding: " + entity.getContentEncoding()); if (entity != null) { result = EntityUtils.toString(entity, StandardCharsets.UTF_8); logger.trace("Result:" + result); EntityUtils.consume(entity); } } finally { response.close(); } return result; }
From source file:net.lizhaoweb.aws.api.service.impl.SNSNotificationHandlerForCloudSearch.java
private String getAccessUrl(S3Object s3Object) { if (s3Object == null) { return null; }/*from www . j ava2 s. co m*/ S3ObjectInputStream s3ObjectInputStream = s3Object.getObjectContent(); if (s3ObjectInputStream == null) { return null; } HttpRequestBase httpRequestBase = s3ObjectInputStream.getHttpRequest(); if (httpRequestBase == null) { return null; } URI uri = httpRequestBase.getURI(); if (uri == null) { return null; } String accessUrl = uri.toString(); return accessUrl; }
From source file:com.xively.client.http.DefaultRequestHandler.java
/** * * Make the request to Xively API and return the response string * * @param <T extends ConnectedObject> * * @param requestMethod/*from w ww . ja v a 2s.com*/ * http request methods * @param appPath * restful app path * @param bodyObjects * objects to be parsed as body for api call * @param params * key-value of params for api call * * @return response string */ private <T extends DomainObject> Response<T> doRequest(HttpMethod requestMethod, String appPath, Map<String, Object> params, T... bodyObjects) { Response<T> response = null; HttpRequestBase request = buildRequest(requestMethod, appPath, params, bodyObjects); DefaultRequestHandler.log.debug(String.format("Requesting %s", request.getURI())); try { DefaultResponseHandler<T> responseHandler = new DefaultResponseHandler<T>(); response = getClient().execute(request, responseHandler); } catch (HttpException e) { throw e; } catch (IOException e) { throw new HttpException("Http request did not return successfully.", e); } catch (RuntimeException e) { // release resources manually on unexpected exceptions request.abort(); throw new HttpException("Http request did not return successfully.", e); } return response; }
From source file:com.facebook.presto.jdbc.ApacheQueryHttpClient.java
private RuntimeException requestFailedException(String task, HttpRequestBase request, Exception exception) { return new RuntimeException(format("Error " + task + " at %s returned an invalid response: %s", request.getURI(), exception.getMessage()), exception); }
From source file:org.cogsurv.cogsurver.http.AbstractHttpApi.java
/** * execute() an httpRequest catching exceptions and returning null instead. * * @param httpRequest//from w w w . ja v a 2 s. c om * @return * @throws IOException */ public HttpResponse executeHttpRequest(HttpRequestBase httpRequest) throws IOException { if (DEBUG) LOG.log(Level.FINE, "executing HttpRequest for: " + httpRequest.getURI().toString()); try { mHttpClient.getConnectionManager().closeExpiredConnections(); return mHttpClient.execute(httpRequest); } catch (IOException e) { httpRequest.abort(); throw e; } }