use of org.jboss.resteasy.spi.MarshalledEntity in project resteasy by resteasy.
the class SigningTest method testSigningManual.
@Test
public void testSigningManual() throws Exception {
// ResteasyClientImpl client = new ResteasyClientImpl();
WebTarget target = client.target(TestPortProvider.generateURL("/signed"));
Response response = target.request().get();
Assert.assertEquals(200, response.getStatus());
MarshalledEntity<String> marshalledEntity = response.readEntity(new GenericType<MarshalledEntity<String>>() {
});
Assert.assertEquals("hello world", marshalledEntity.getEntity());
String signatureHeader = response.getHeaderString(DKIMSignature.DKIM_SIGNATURE);
// System.out.println(DKIMSignature.DKIM_SIGNATURE + ": " + signatureHeader);
Assert.assertNotNull(signatureHeader);
DKIMSignature contentSignature = new DKIMSignature(signatureHeader);
contentSignature.verify(response.getStringHeaders(), marshalledEntity.getMarshalledBytes(), keys.getPublic());
response.close();
}
use of org.jboss.resteasy.spi.MarshalledEntity 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.spi.MarshalledEntity in project resteasy by resteasy.
the class SigningTest method testSigningManual.
/**
* @tpTestDetails Test for manual signing
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testSigningManual() throws Exception {
WebTarget target = client.target(generateURL("/signed"));
Response response = target.request().get();
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
MarshalledEntity<String> marshalledEntity = response.readEntity(new GenericType<MarshalledEntity<String>>() {
});
Assert.assertEquals(RESPONSE_ERROR_MSG, "hello world", marshalledEntity.getEntity());
String signatureHeader = response.getHeaderString(DKIMSignature.DKIM_SIGNATURE);
logger.info(DKIMSignature.DKIM_SIGNATURE + ": " + signatureHeader);
Assert.assertNotNull("Missing DKIM_SIGNATURE header", signatureHeader);
DKIMSignature contentSignature = new DKIMSignature(signatureHeader);
contentSignature.verify(response.getStringHeaders(), marshalledEntity.getMarshalledBytes(), keys.getPublic());
response.close();
}
use of org.jboss.resteasy.spi.MarshalledEntity 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.spi.MarshalledEntity in project resteasy by resteasy.
the class SigningTest method testBadSignature.
@Test
public void testBadSignature() throws Exception {
// ResteasyClientImpl client = new ResteasyClientImpl();
WebTarget target = client.target(TestPortProvider.generateURL("/signed/bad-signature"));
Response response = target.request().get();
Assert.assertEquals(200, response.getStatus());
String signatureHeader = response.getHeaderString(DKIMSignature.DKIM_SIGNATURE);
Assert.assertNotNull(signatureHeader);
// System.out.println(DKIMSignature.DKIM_SIGNATURE + ": " + signatureHeader);
DKIMSignature contentSignature = new DKIMSignature(signatureHeader);
MarshalledEntity<String> entity = response.readEntity(new GenericType<MarshalledEntity<String>>() {
});
boolean failedVerification = false;
try {
contentSignature.verify(response.getStringHeaders(), entity.getMarshalledBytes(), keys.getPublic());
} catch (SignatureException e) {
failedVerification = true;
}
Assert.assertTrue(failedVerification);
response.close();
}
Aggregations