Search in sources :

Example 66 with HttpPost

use of org.apache.http.client.methods.HttpPost in project RoboZombie by sahan.

the class EntityProcessor method process.

/**
	 * <p>Accepts the {@link InvocationContext} of an {@link HttpEntityEnclosingRequest} and inserts 
	 * <b>the</b> request parameter which is annotated with @{@link Entity} into its body.</p>
	 * 
	 * <p><b>Note</b> that it makes no sense to scope multiple entities within the same entity enclosing 
	 * request (HTTP/1.1 <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html">RFC-2616</a>). 
	 * This processor fails for the following scenarios:</p>
	 * 
	 * <ul>
	 * 	<li><b>No entity</b> was found in the endpoint method definition.</li>
	 * 	<li><b>Multiple entities</b> were found in the endpoint method definition.</li>
	 * 	<li>The annotated entity <b>failed to be resolved</b> to a matching {@link HttpEntity}.</li>
	 * </ul>
	 * 
	 * <p>Parameter types are resolved to their {@link HttpEntity} as specified in 
	 * {@link Entities#resolve(Object)}. If an attached @{@link Serialize} is discovered, the entity 
	 * will be serialized using the specified serializer before translation to an {@link HttpEntity}.</p>
	 * 
	 * <p>See {@link AbstractRequestProcessor#process(InvocationContext, HttpRequestBase)}.</p>
	 *
	 * @param context
	 * 			the {@link InvocationContext} which is used to retrieve the entity
	 * <br><br>
	 * @param request
	 * 			an instance of {@link HttpEntityEnclosingRequestBase} which allows the inclusion of an 
	 * 			{@link HttpEntity} in its body
	 * <br><br>
 	 * @return the same instance of {@link HttpRequestBase} which was given for processing entities 
	 * <br><br>
	 * @throws RequestProcessorException
	 * 			if an {@link HttpEntityEnclosingRequestBase} was discovered and yet the entity failed to 
	 * 			be resolved and inserted into the request body
	 * <br><br>
	 * @since 1.3.0
	 */
//welcomes a ClassCastException on misuse of @Serialize(Custom.class)
@Override
//welcomes a ClassCastException on misuse of @Serialize(Custom.class)
@SuppressWarnings("unchecked")
protected HttpRequestBase process(InvocationContext context, HttpRequestBase request) {
    try {
        if (request instanceof HttpEntityEnclosingRequestBase) {
            List<Entry<Entity, Object>> entities = Metadata.onParams(Entity.class, context);
            if (entities.isEmpty()) {
                throw new MissingEntityException(context);
            }
            if (entities.size() > 1) {
                throw new MultipleEntityException(context);
            }
            Object entity = entities.get(0).getValue();
            Serialize metadata = (metadata = context.getRequest().getAnnotation(Serialize.class)) == null ? context.getEndpoint().getAnnotation(Serialize.class) : metadata;
            if (metadata != null && !isDetached(context, Serialize.class)) {
                //no restrictions on custom serializer types with @Serialize
                @SuppressWarnings("rawtypes") AbstractSerializer serializer = (metadata.value() == UNDEFINED) ? Serializers.resolve(metadata.type()) : Serializers.resolve(metadata.value());
                entity = serializer.run(context, entity);
            }
            HttpEntity httpEntity = Entities.resolve(entity);
            ((HttpEntityEnclosingRequestBase) request).setHeader(HttpHeaders.CONTENT_TYPE, ContentType.getOrDefault(httpEntity).getMimeType());
            ((HttpEntityEnclosingRequestBase) request).setEntity(httpEntity);
        }
    } catch (MissingEntityException mee) {
        if (!(request instanceof HttpPost)) {
            //allow leeway for POST requests
            StringBuilder errorContext = new StringBuilder("It is imperative that this request encloses an entity.").append(" Identify exactly one entity by annotating an argument with @").append(Entity.class.getSimpleName());
            throw new RequestProcessorException(errorContext.toString(), mee);
        }
    } catch (MultipleEntityException mee) {
        //violates HTTP 1.1 specification, be more verbose 
        StringBuilder errorContext = new StringBuilder("This request is only able to enclose exactly one entity.").append(" Remove all @").append(Entity.class.getSimpleName()).append(" annotations except for a single entity which is identified by this URI. ");
        throw new RequestProcessorException(errorContext.toString(), mee);
    } catch (EntityResolutionFailedException erfe) {
        //violates HTTP 1.1 specification, be more verbose
        StringBuilder errorContext = new StringBuilder("This request cannot proceed without an enclosing entity.").append(" Ensure that the entity which is annotated with ").append(Entity.class.getSimpleName()).append(" complies with the supported types as documented in ").append(RequestUtils.class.getName()).append("#resolveHttpEntity(Object)");
        throw new RequestProcessorException(errorContext.toString(), erfe);
    } catch (Exception e) {
        throw new RequestProcessorException(context, getClass(), e);
    }
    return request;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) Entity(com.lonepulse.robozombie.annotation.Entity) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) Serialize(com.lonepulse.robozombie.annotation.Serialize) HttpEntity(org.apache.http.HttpEntity) EntityResolutionFailedException(com.lonepulse.robozombie.util.EntityResolutionFailedException) EntityResolutionFailedException(com.lonepulse.robozombie.util.EntityResolutionFailedException) Entry(java.util.Map.Entry)

Example 67 with HttpPost

use of org.apache.http.client.methods.HttpPost in project spring-framework by spring-projects.

the class HttpComponentsHttpInvokerRequestExecutor method doExecuteRequest.

/**
	 * Execute the given request through the HttpClient.
	 * <p>This method implements the basic processing workflow:
	 * The actual work happens in this class's template methods.
	 * @see #createHttpPost
	 * @see #setRequestBody
	 * @see #executeHttpPost
	 * @see #validateResponse
	 * @see #getResponseBody
	 */
@Override
protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws IOException, ClassNotFoundException {
    HttpPost postMethod = createHttpPost(config);
    setRequestBody(config, postMethod, baos);
    try {
        HttpResponse response = executeHttpPost(config, getHttpClient(), postMethod);
        validateResponse(config, response);
        InputStream responseBody = getResponseBody(config, response);
        return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());
    } finally {
        postMethod.releaseConnection();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse)

Example 68 with HttpPost

use of org.apache.http.client.methods.HttpPost in project spring-framework by spring-projects.

the class HttpComponentsHttpInvokerRequestExecutorTests method ignoreFactorySettings.

@Test
public void ignoreFactorySettings() throws IOException {
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor(httpClient) {

        @Override
        protected RequestConfig createRequestConfig(HttpInvokerClientConfiguration config) {
            return null;
        }
    };
    HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
    HttpPost httpPost = executor.createHttpPost(config);
    assertNull("custom request config should not be set", httpPost.getConfig());
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) Test(org.junit.Test)

Example 69 with HttpPost

use of org.apache.http.client.methods.HttpPost in project spring-framework by spring-projects.

the class HttpComponentsHttpInvokerRequestExecutorTests method customizeReadTimeout.

@Test
public void customizeReadTimeout() throws IOException {
    HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor();
    executor.setReadTimeout(10000);
    HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
    HttpPost httpPost = executor.createHttpPost(config);
    assertEquals(10000, httpPost.getConfig().getSocketTimeout());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) Test(org.junit.Test)

Example 70 with HttpPost

use of org.apache.http.client.methods.HttpPost in project spring-framework by spring-projects.

the class HttpComponentsHttpInvokerRequestExecutorTests method defaultSettingsOfHttpClientMergedOnExecutorCustomization.

@Test
public void defaultSettingsOfHttpClientMergedOnExecutorCustomization() throws IOException {
    RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).build();
    CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class));
    Configurable configurable = (Configurable) client;
    when(configurable.getConfig()).thenReturn(defaultConfig);
    HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor(client);
    HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
    HttpPost httpPost = executor.createHttpPost(config);
    assertSame("Default client configuration is expected", defaultConfig, httpPost.getConfig());
    executor.setConnectionRequestTimeout(4567);
    HttpPost httpPost2 = executor.createHttpPost(config);
    assertNotNull(httpPost2.getConfig());
    assertEquals(4567, httpPost2.getConfig().getConnectionRequestTimeout());
    // Default connection timeout merged
    assertEquals(1234, httpPost2.getConfig().getConnectTimeout());
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) Configurable(org.apache.http.client.methods.Configurable) Test(org.junit.Test)

Aggregations

HttpPost (org.apache.http.client.methods.HttpPost)531 HttpResponse (org.apache.http.HttpResponse)238 StringEntity (org.apache.http.entity.StringEntity)220 Test (org.junit.Test)153 IOException (java.io.IOException)143 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)107 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)99 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)90 ArrayList (java.util.ArrayList)86 NameValuePair (org.apache.http.NameValuePair)84 HttpEntity (org.apache.http.HttpEntity)83 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)68 HttpClient (org.apache.http.client.HttpClient)64 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)63 HttpGet (org.apache.http.client.methods.HttpGet)55 TestHttpClient (io.undertow.testutils.TestHttpClient)54 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)49 ClientProtocolException (org.apache.http.client.ClientProtocolException)49 JsonNode (com.fasterxml.jackson.databind.JsonNode)39 InputStream (java.io.InputStream)29