private void ensureMediaType() { if (getMediaType() == null) { // Content-Type is not present choose a default type final GenericType<?> entityType = new GenericType(getEntityType()); final List<MediaType> mediaTypes = workers.getMessageBodyWriterMediaTypes( entityType.getRawType(), entityType.getType(), getEntityAnnotations()); setMediaType(getMediaType(mediaTypes)); } }
@SuppressWarnings("unchecked") private <T> T readAndCast(GenericType<T> type, MediaType effectiveMediaType, MessageBodyReader reader) { try { return (T) reader.readFrom( type.getRawType(), type.getType(), annotations, effectiveMediaType, headers, new ByteArrayInputStream(data)); } catch (IOException ex) { throw new ProcessingException(ex); } }
@Override public TokenResult readFrom(final Class<TokenResult> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException, WebApplicationException { final GenericType<Map<String, Object>> mapType = new GenericType<Map<String, Object>>() { }; final Map<String, Object> map = (Map<String, Object>) workers.get().readFrom(mapType.getRawType(), mapType.getType(), annotations, mediaType, httpHeaders, propertiesDelegateProvider.get(), entityStream, EMPTY_INTERCEPTORS, false); return new TokenResult(map); } }
@Override public void completed(final ClientResponse response, final RequestScope scope) { if (responseFuture.isCancelled()) { response.close(); failed(new ProcessingException( new CancellationException(LocalizationMessages.ERROR_REQUEST_CANCELLED()))); return; } final T result; if (callbackParamClass == Response.class) { result = callbackParamClass.cast(new InboundJaxrsResponse(response, scope)); responseFuture.complete(result); callback.completed(result); } else if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) { result = response.readEntity(new GenericType<T>(callbackParamType)); responseFuture.complete(result); callback.completed(result); } else { failed(convertToException(new InboundJaxrsResponse(response, scope))); } }
callbackParamType = responseType.getType(); callbackParamClass = ReflectionHelper.erasure(responseType.getRawType()); final ClientRuntime runtime = request().getClientRuntime(); runtime.submit(runtime.createRunnableForAsyncProcessing(requestForCall(requestContext), responseCallback)); } catch (final Throwable error) { responseFuture.completeExceptionally(ce); } else if (error instanceof WebApplicationException) { ce = new ProcessingException(error); responseFuture.completeExceptionally(error); } else { ce = new ProcessingException(error); responseFuture.completeExceptionally(ce);
try { try { entityStream = writeWorkers.writeTo( getEntity(), entityType.getRawType(), entityType.getType(), getEntityAnnotations(), getMediaType(), getHeaders(), getPropertiesDelegate(),
public <T> T readEntity(GenericType<T> entityType, Annotation[] annotations) throws ProcessingException, IllegalStateException { return (T) readEntity(entityType.getRawType(), entityType.getType(), annotations, requestContext.getPropertiesDelegate());
private <T> T translate(final ClientResponse response, final RequestScope scope, final GenericType<T> responseType) throws ProcessingException { if (responseType.getRawType() == Response.class) { //noinspection unchecked return (T) new InboundJaxrsResponse(response, scope); } if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) { try { return response.readEntity(responseType); } catch (final ProcessingException ex) { throw new ResponseProcessingException( new InboundJaxrsResponse(response, scope), ex.getCause() != null ? ex.getCause() : ex); } catch (final WebApplicationException ex) { throw new ResponseProcessingException(new InboundJaxrsResponse(response, scope), ex); } catch (final Exception ex) { throw new ResponseProcessingException(new InboundJaxrsResponse(response, scope), LocalizationMessages.UNEXPECTED_ERROR_RESPONSE_PROCESSING(), ex); } } else { throw convertToException(new InboundJaxrsResponse(response, scope)); } }
/** * Start building a new generic type-based service binding. * <p> * Binds the generic service type itself as a contract type. * * @param <T> service type. * @param serviceType generic service type information. * @return initialized binding builder. */ @SuppressWarnings("unchecked") public static <T> ClassBinding<T> service(GenericType<T> serviceType) { return (ClassBinding<T>) new ClassBinding<>(serviceType.getRawType()) .asType((Class<T>) serviceType.getType()); }
/** * Set the message entity type information. * <p> * This method overrides any computed or previously set entity type information. * * @param type overriding message entity type. */ public void setEntityType(Type type) { this.entityType = new GenericType(type); }
private static Class getClass(Type type) { if (type instanceof Class) { return (Class) type; } else if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; if (parameterizedType.getRawType() instanceof Class) { return (Class) parameterizedType.getRawType(); } } else if (type instanceof GenericArrayType) { GenericArrayType array = (GenericArrayType) type; final Class<?> componentRawType = getClass(array.getGenericComponentType()); return getArrayClass(componentRawType); } throw new IllegalArgumentException("Type parameter " + type.toString() + " not a class or " + "parameterized type whose raw type is a class"); }
/** * Write (serialize) the entity set in this request into the {@link #getEntityStream() entity stream}. The method * use {@link javax.ws.rs.ext.WriterInterceptor writer interceptors} and {@link javax.ws.rs.ext.MessageBodyWriter * message body writer}. * <p/> * This method modifies the state of this request and therefore it can be called only once per request life cycle otherwise * IllegalStateException is thrown. * <p/> * Note that {@link #setStreamProvider(org.glassfish.jersey.message.internal.OutboundMessageContext.StreamProvider)} * and optionally {@link #enableBuffering()} must be called before calling this method. * * @throws IOException In the case of IO error. */ public void writeEntity() throws IOException { Preconditions.checkState(!entityWritten, LocalizationMessages.REQUEST_ENTITY_ALREADY_WRITTEN()); entityWritten = true; ensureMediaType(); final GenericType<?> entityType = new GenericType(getEntityType()); doWriteEntity(workers, entityType); }
/** * Read event data as a given generic type. * * @param type generic type to be used for event data de-serialization. * @param mediaType {@link MediaType media type} to be used for event data de-serialization. * @return event data de-serialized as an instance of a given type. * @throws javax.ws.rs.ProcessingException when provided type can't be read. The thrown exception wraps the original cause. * @since 2.3 */ public <T> T readData(GenericType<T> type, MediaType mediaType) { final MediaType effectiveMediaType = mediaType == null ? this.mediaType : mediaType; final MessageBodyReader reader = messageBodyWorkers.getMessageBodyReader(type.getRawType(), type.getType(), annotations, mediaType); if (reader == null) { throw new MessageBodyProviderNotFoundException(LocalizationMessages.EVENT_DATA_READER_NOT_FOUND()); } return readAndCast(type, effectiveMediaType, reader); }
entityType = new GenericType(((GenericEntity) requestEntity).getType()); } else { entity = requestEntity; entityType = new GenericType(requestEntity.getClass()); stream = workers.writeTo(entity, entity.getClass(), entityType.getType(), new Annotation[0], getMediaType(),
/** * Constructs a new generic type, deriving the generic type and class from * type parameter. Note that this constructor is protected, users should create * a (usually anonymous) subclass as shown above. * * @throws IllegalArgumentException in case the generic type parameter value is not * provided by any of the subclasses. */ protected GenericType() { // Get the type parameter of GenericType<T> (aka the T value) type = getTypeArgument(getClass(), GenericType.class); rawType = getClass(type); }
/** * Get the raw message entity type information. * * @return raw message entity type information. */ public Class<?> getEntityClass() { return entityType == null ? null : entityType.getRawType(); }
/** * Get the message entity type information. * * @return message entity type. */ public Type getEntityType() { return entityType == null ? null : entityType.getType(); }
private boolean isWriteable(final RequestSpecificConsumesProducesAcceptor candidate) { final Invocable invocable = candidate.methodRouting.method.getInvocable(); final Class<?> responseType = Primitives.wrap(invocable.getRawRoutingResponseType()); if (Response.class.isAssignableFrom(responseType) || Void.class.isAssignableFrom(responseType)) { return true; } final Type genericType = invocable.getRoutingResponseType(); final Type genericReturnType = genericType instanceof GenericType ? ((GenericType) genericType).getType() : genericType; for (final WriterModel model : workers.getWritersModelsForType(responseType)) { if (model.isWriteable( responseType, genericReturnType, invocable.getHandlingMethod().getDeclaredAnnotations(), candidate.produces.combinedType)) { return true; } } return false; }
protected GenericEntity(final T entity) { if (entity == null) { throw new IllegalArgumentException("The entity must not be null"); } this.entity = entity; this.type = GenericType.getTypeArgument(getClass(), GenericEntity.class); this.rawType = entity.getClass(); }
public GenericType(Type genericType) { if (genericType == null) { throw new IllegalArgumentException("Type must not be null"); } type = genericType; rawType = getClass(type); }