use of org.jboss.resteasy.util.InputStreamToByteArray in project resteasy by resteasy.
the class DigitalVerificationInterceptor method aroundReadFrom.
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
LogMessages.LOGGER.debugf("Interceptor : %s, Method : aroundReadFrom", getClass().getName());
if (!context.hasProperty(Verifier.class.getName())) {
return context.proceed();
}
// System.out.println("TRACE: found verifier");
MultivaluedMap<String, String> headers = context.getHeaders();
List<String> strings = headers.get(DKIMSignature.DKIM_SIGNATURE);
if (strings == null) {
throw new UnauthorizedSignatureException(Messages.MESSAGES.thereWasNoSignatureHeader(DKIMSignature.DKIM_SIGNATURE));
}
List<DKIMSignature> signatures = new ArrayList<DKIMSignature>();
for (String headerVal : strings) {
try {
signatures.add(new DKIMSignature(headerVal));
} catch (Exception e) {
throw new UnauthorizedSignatureException(Messages.MESSAGES.malformedSignatureHeader(DKIMSignature.DKIM_SIGNATURE));
}
}
InputStream old = context.getInputStream();
try {
InputStreamToByteArray stream = new InputStreamToByteArray(old);
context.setInputStream(stream);
Object rtn = context.proceed();
byte[] body = stream.toByteArray();
Verifier verifier = (Verifier) context.getProperty(Verifier.class.getName());
if (verifier.getRepository() == null) {
KeyRepository repository = (KeyRepository) context.getProperty(KeyRepository.class.getName());
if (repository == null) {
repository = ResteasyContext.getContextData(KeyRepository.class);
}
verifier.setRepository(repository);
}
VerificationResults results = verifier.verify(signatures, headers, body);
if (results.isVerified() == false) {
throw new UnauthorizedSignatureException(results);
}
return rtn;
} finally {
context.setInputStream(old);
}
}
use of org.jboss.resteasy.util.InputStreamToByteArray in project resteasy by resteasy.
the class ClientResponse method readFrom.
// this is synchronized in conjunction with finalize to protect against premature finalize called by the GC
@Override
protected synchronized <T> Object readFrom(Class<T> type, Type genericType, MediaType media, Annotation[] annotations) {
Type useGeneric = genericType == null ? type : genericType;
Class<?> useType = type;
media = media == null ? MediaType.WILDCARD_TYPE : media;
annotations = annotations == null ? this.annotations : annotations;
boolean isMarshalledEntity = false;
if (type.equals(MarshalledEntity.class)) {
isMarshalledEntity = true;
ParameterizedType param = (ParameterizedType) useGeneric;
useGeneric = param.getActualTypeArguments()[0];
useType = Types.getRawType(useGeneric);
}
Providers current = ResteasyContext.getContextData(Providers.class);
ResteasyContext.pushContext(Providers.class, configuration);
Object obj = null;
try {
InputStream is = getEntityStream();
if (is == null) {
throw new IllegalStateException(Messages.MESSAGES.inputStreamWasEmpty());
}
if (isMarshalledEntity) {
is = new InputStreamToByteArray(is);
}
ReaderInterceptor[] readerInterceptors = configuration.getReaderInterceptors(null, null);
final Object finalObj;
final long timestamp = tracingLogger.timestamp("RI_SUMMARY");
AbstractReaderInterceptorContext context = new ClientReaderInterceptorContext(readerInterceptors, configuration.getProviderFactory(), useType, useGeneric, annotations, media, getStringHeaders(), is, properties, tracingLogger);
try {
finalObj = context.proceed();
obj = finalObj;
} finally {
tracingLogger.logDuration("RI_SUMMARY", timestamp, context.getProcessedInterceptorCount());
}
if (isMarshalledEntity) {
InputStreamToByteArray isba = (InputStreamToByteArray) is;
final byte[] bytes = isba.toByteArray();
return new MarshalledEntity<Object>() {
@Override
public byte[] getMarshalledBytes() {
return bytes;
}
@Override
public Object getEntity() {
return finalObj;
}
};
} else {
return finalObj;
}
} catch (ProcessingException pe) {
throw pe;
} catch (Exception ex) {
throw new ProcessingException(ex);
} finally {
if (!Publisher.class.isAssignableFrom(type) && !EventInput.class.isAssignableFrom(type)) {
ResteasyContext.popContextData(Providers.class);
if (current != null)
ResteasyContext.pushContext(Providers.class, current);
if (obj instanceof ProvidersContextRetainer)
((ProvidersContextRetainer) obj).setProviders(configuration);
}
}
}
use of org.jboss.resteasy.util.InputStreamToByteArray in project resteasy by resteasy.
the class BuiltResponse method readFrom.
// this is synchronized in conjunction with finalize to protect against premature finalize called by the GC
protected synchronized <T> Object readFrom(Class<T> type, Type genericType, MediaType media, Annotation[] annotations) {
Type useGeneric = genericType == null ? type : genericType;
Class<?> useType = type;
media = media == null ? MediaType.WILDCARD_TYPE : media;
annotations = annotations == null ? this.annotations : annotations;
boolean isMarshalledEntity = false;
if (type.equals(MarshalledEntity.class)) {
isMarshalledEntity = true;
ParameterizedType param = (ParameterizedType) useGeneric;
useGeneric = param.getActualTypeArguments()[0];
useType = Types.getRawType(useGeneric);
}
try {
InputStream is = getEntityStream();
if (is == null) {
throw new IllegalStateException(Messages.MESSAGES.inputStreamWasEmpty());
}
if (isMarshalledEntity) {
is = new InputStreamToByteArray(is);
}
ResteasyProviderFactory providerFactory = ResteasyProviderFactory.getInstance();
ReaderInterceptor[] readerInterceptors = providerFactory.getServerReaderInterceptorRegistry().postMatch(null, null);
final Object finalObj;
AbstractReaderInterceptorContext context = new ClientReaderInterceptorContext(readerInterceptors, providerFactory, useType, useGeneric, annotations, media, getStringHeaders(), is, new HashMap<String, Object>(), null);
finalObj = context.proceed();
if (isMarshalledEntity) {
InputStreamToByteArray isba = (InputStreamToByteArray) is;
final byte[] bytes = isba.toByteArray();
return new MarshalledEntity<Object>() {
@Override
public byte[] getMarshalledBytes() {
return bytes;
}
@Override
public Object getEntity() {
return finalObj;
}
};
} else {
return finalObj;
}
} catch (ProcessingException pe) {
throw pe;
} catch (Exception ex) {
throw new ProcessingException(ex);
}
}
use of org.jboss.resteasy.util.InputStreamToByteArray in project resteasy by resteasy.
the class MessageBodyParameterInjector method inject.
@Override
public Object inject(HttpRequest request, HttpResponse response, boolean unwrapAsync) {
Object o = getBody();
if (o != null) {
return o;
}
MediaType mediaType = request.getHttpHeaders().getMediaType();
if (mediaType == null) {
mediaType = MediaType.WILDCARD_TYPE;
// throw new BadRequestException("content-type was null and expecting to extract a body into " + this.target);
}
InputStream is = null;
if (MediaType.APPLICATION_FORM_URLENCODED_TYPE.equals(mediaType)) {
if (request.formParametersRead()) {
MultivaluedMap<String, String> map = request.getDecodedFormParameters();
if (map != null) {
StringBuilder sb = new StringBuilder();
for (Entry<String, List<String>> entry : map.entrySet()) {
String key = entry.getKey();
sb.append(key);
List<String> values = entry.getValue();
for (String value : values) {
if (!("".equals(value))) {
sb.append("=").append(value);
}
sb.append("&");
}
}
if (sb.length() > 0 && '&' == sb.charAt(sb.length() - 1)) {
sb.deleteCharAt(sb.length() - 1);
}
String charset = "UTF-8";
if (mediaType.getParameters().get("charset") != null) {
charset = mediaType.getParameters().get("charset");
}
try {
is = new ByteArrayInputStream(sb.toString().getBytes(charset));
} catch (Exception e) {
LogMessages.LOGGER.charsetUnavailable(charset);
}
}
}
}
try {
if (is == null) {
is = request.getInputStream();
}
if (isMarshalledEntity) {
is = new InputStreamToByteArray(is);
}
AbstractReaderInterceptorContext messageBodyReaderContext = new ServerReaderInterceptorContext(getReaderInterceptors(), factory, type, genericType, annotations, mediaType, request.getMutableHeaders(), is, request);
RESTEasyTracingLogger tracingLogger = RESTEasyTracingLogger.getInstance(request);
final long timestamp = tracingLogger.timestamp("RI_SUMMARY");
final Object obj;
try {
obj = messageBodyReaderContext.proceed();
} finally {
tracingLogger.logDuration("RI_SUMMARY", timestamp, messageBodyReaderContext.getProcessedInterceptorCount());
}
if (isMarshalledEntity) {
InputStreamToByteArray isba = (InputStreamToByteArray) is;
final byte[] bytes = isba.toByteArray();
return new MarshalledEntity() {
@Override
public byte[] getMarshalledBytes() {
return bytes;
}
@Override
public Object getEntity() {
return obj;
}
};
} else {
return obj;
}
} catch (Exception e) {
if (e instanceof ReaderException) {
throw (ReaderException) e;
} else {
throw new ReaderException(e);
}
}
}
Aggregations