Skip to content

Commit

Permalink
Applying formatter to ClientInvocation class to simplify working on b…
Browse files Browse the repository at this point in the history
…ig PR affecting it
  • Loading branch information
asoldano committed Aug 21, 2017
1 parent 812e19c commit 2ff94b0
Showing 1 changed file with 80 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.jboss.resteasy.client.jaxrs.internal;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.core.interception.jaxrs.AbstractWriterInterceptorContext;
import org.jboss.resteasy.core.interception.jaxrs.ClientWriterInterceptorContext;
import org.jboss.resteasy.plugins.providers.sse.EventInput;
import org.jboss.resteasy.specimpl.MultivaluedTreeMap;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.jboss.resteasy.util.DelegatingOutputStream;
import org.jboss.resteasy.util.Types;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import javax.ws.rs.BadRequestException;
import javax.ws.rs.ClientErrorException;
Expand Down Expand Up @@ -37,19 +39,14 @@
import javax.ws.rs.ext.Providers;
import javax.ws.rs.ext.WriterInterceptor;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.core.interception.jaxrs.AbstractWriterInterceptorContext;
import org.jboss.resteasy.core.interception.jaxrs.ClientWriterInterceptorContext;
import org.jboss.resteasy.plugins.providers.sse.EventInput;
import org.jboss.resteasy.specimpl.MultivaluedTreeMap;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.jboss.resteasy.util.DelegatingOutputStream;
import org.jboss.resteasy.util.Types;

/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
Expand All @@ -58,21 +55,31 @@
public class ClientInvocation implements Invocation
{
protected ResteasyClient client;

protected ClientRequestHeaders headers;

protected String method;

protected Object entity;

protected Type entityGenericType;

protected Class<?> entityClass;

protected Annotation[] entityAnnotations;

protected ClientConfiguration configuration;

protected URI uri;

protected boolean chunked;

// todo need a better solution for this. Apache Http Client 4 does not let you obtain the OutputStream before executing
// this request. is problematic for obtaining and setting
// the output stream. It also does not let you modify the request headers before the output stream is available
// Since MessageBodyWriter allows you to modify headers, you're s
protected DelegatingOutputStream delegatingOutputStream = new DelegatingOutputStream();

protected OutputStream entityStream = delegatingOutputStream;

public ClientInvocation(ResteasyClient client, URI uri, ClientRequestHeaders headers, ClientConfiguration parent)
Expand All @@ -82,12 +89,12 @@ public ClientInvocation(ResteasyClient client, URI uri, ClientRequestHeaders hea
this.configuration = new ClientConfiguration(parent);
this.headers = headers;
}

ClientInvocation(ClientInvocation clientInvocation)
{
this.client = clientInvocation.client;
this.configuration = new ClientConfiguration(clientInvocation.configuration);
this.headers = new ClientRequestHeaders(this.configuration);
this.headers = new ClientRequestHeaders(this.configuration);
MultivaluedTreeMap.copy(clientInvocation.headers.getHeaders(), this.headers.headers);
this.method = clientInvocation.method;
this.entity = clientInvocation.entity;
Expand Down Expand Up @@ -121,13 +128,12 @@ public static <T> T extractResult(GenericType<T> responseType, Response response
else
{
T rtn = response.readEntity(responseType, annotations);
if (InputStream.class.isInstance(rtn)
|| Reader.class.isInstance(rtn)
|| EventInput.class.isInstance(rtn))
if (InputStream.class.isInstance(rtn) || Reader.class.isInstance(rtn)
|| EventInput.class.isInstance(rtn))
{
if (response instanceof ClientResponse)
{
ClientResponse clientResponse = (ClientResponse)response;
ClientResponse clientResponse = (ClientResponse) response;
clientResponse.noReleaseConnection();
}
}
Expand Down Expand Up @@ -161,7 +167,8 @@ public static <T> T extractResult(GenericType<T> responseType, Response response
}
finally
{
if (response.getMediaType() == null) response.close();
if (response.getMediaType() == null)
response.close();
}
}
try
Expand All @@ -188,14 +195,16 @@ public static <T> T extractResult(GenericType<T> responseType, Response response
}
}
}
if (status >= 300 && status < 400) throw new RedirectionException(response);
if (status >= 300 && status < 400)
throw new RedirectionException(response);

return handleErrorStatus(response);
}
finally
{
// close if no content
if (response.getMediaType() == null) response.close();
if (response.getMediaType() == null)
response.close();
}

}
Expand All @@ -212,31 +221,32 @@ public static <T> T handleErrorStatus(Response response)
final int status = response.getStatus();
switch (status)
{
case 400:
case 400 :
throw new BadRequestException(response);
case 401:
case 401 :
throw new NotAuthorizedException(response);
case 403:
case 403 :
throw new ForbiddenException(response);
case 404:
case 404 :
throw new NotFoundException(response);
case 405:
case 405 :
throw new NotAllowedException(response);
case 406:
case 406 :
throw new NotAcceptableException(response);
case 415:
case 415 :
throw new NotSupportedException(response);
case 500:
case 500 :
throw new InternalServerErrorException(response);
case 503:
case 503 :
throw new ServiceUnavailableException(response);
default:
default :
break;
}

if (status >= 400 && status < 500) throw new ClientErrorException(response);
if (status >= 500) throw new ServerErrorException(response);

if (status >= 400 && status < 500)
throw new ClientErrorException(response);
if (status >= 500)
throw new ServerErrorException(response);

throw new WebApplicationException(response);
}
Expand Down Expand Up @@ -369,7 +379,7 @@ public void setEntityObject(Object ent)
{
this.entity = null;
this.entityClass = null;
this.entityGenericType = null;
this.entityGenericType = null;
}
else
{
Expand All @@ -388,11 +398,12 @@ public void writeRequestBody(OutputStream outputStream) throws IOException
}

WriterInterceptor[] interceptors = getWriterInterceptors();
AbstractWriterInterceptorContext ctx = new ClientWriterInterceptorContext(interceptors, configuration.getProviderFactory(), entity, entityClass, entityGenericType, entityAnnotations, headers.getMediaType(), headers.getHeaders(), outputStream, getMutableProperties());
AbstractWriterInterceptorContext ctx = new ClientWriterInterceptorContext(interceptors,
configuration.getProviderFactory(), entity, entityClass, entityGenericType, entityAnnotations,
headers.getMediaType(), headers.getHeaders(), outputStream, getMutableProperties());
ctx.proceed();
}


public WriterInterceptor[] getWriterInterceptors()
{
return configuration.getWriterInterceptors(null, null);
Expand All @@ -410,7 +421,6 @@ public ClientResponseFilter[] getResponseFilters()

// Invocation methods


public Configuration getConfiguration()
{
return configuration;
Expand Down Expand Up @@ -461,7 +471,8 @@ public Response invoke()
}
// spec requires that aborted response go through filter/interceptor chains.
ClientResponse response = aborted;
if (response == null) response = client.httpEngine().invoke(this);
if (response == null)
response = client.httpEngine().invoke(this);
response.setProperties(configuration.getMutableProperties());

ClientResponseFilter[] responseFilters = getResponseFilters();
Expand Down Expand Up @@ -489,7 +500,8 @@ public Response invoke()
finally
{
ResteasyProviderFactory.popContextData(Providers.class);
if (current != null) ResteasyProviderFactory.pushContext(Providers.class, current);
if (current != null)
ResteasyProviderFactory.pushContext(Providers.class, current);
}
}

Expand All @@ -498,7 +510,8 @@ public Response invoke()
public <T> T invoke(Class<T> responseType)
{
Response response = invoke();
if (Response.class.equals(responseType)) return (T)response;
if (Response.class.equals(responseType))
return (T) response;
return extractResult(new GenericType<T>(responseType), response, null);
}

Expand All @@ -507,7 +520,8 @@ public <T> T invoke(Class<T> responseType)
public <T> T invoke(GenericType<T> responseType)
{
Response response = invoke();
if (responseType.getRawType().equals(Response.class)) return (T)response;
if (responseType.getRawType().equals(Response.class))
return (T) response;
return ClientInvocation.extractResult(responseType, response, null);
}

Expand All @@ -524,7 +538,6 @@ public Response call() throws Exception
});
}


@Override
public <T> Future<T> submit(final Class<T> responseType)
{
Expand All @@ -538,7 +551,7 @@ public T call() throws Exception
});
}

@Override
@Override
public <T> Future<T> submit(final GenericType<T> responseType)
{
return client.asyncInvocationExecutor().submit(new Callable<T>()
Expand All @@ -551,11 +564,14 @@ public T call() throws Exception
});
}

@SuppressWarnings({ "rawtypes", "unchecked" })
@SuppressWarnings(
{"rawtypes", "unchecked"})
@Override
public <T> Future<T> submit(final InvocationCallback<T> callback)
{
GenericType<T> genericType = (GenericType<T>)new GenericType<Object>() {};
GenericType<T> genericType = (GenericType<T>) new GenericType<Object>()
{
};
Type[] typeInfo = Types.getActualTypeArgumentsOfAnInterface(callback.getClass(), InvocationCallback.class);
if (typeInfo != null)
{
Expand All @@ -569,21 +585,25 @@ public <T> Future<T> submit(final InvocationCallback<T> callback)
public T call() throws Exception
{
T result = null;
try {
try
{
result = invoke(responseType);
}
catch (Exception e) {
catch (Exception e)
{
callback.failed(e);
throw e;
}
try {
try
{
callback.completed(result);
return result;
}
finally {
finally
{
if (result != null && result instanceof Response)
{
((Response)result).close();
((Response) result).close();
}
}
}
Expand Down

0 comments on commit 2ff94b0

Please sign in to comment.