use of javax.ws.rs.BadRequestException in project cxf by apache.
the class JAXRSXmlSecTest method testPostEncryptedSignedBookInvalid.
@Test
public void testPostEncryptedSignedBookInvalid() throws Exception {
String address = "https://localhost:" + test.port + "/xmlsec-validate/bookstore/books";
Map<String, Object> properties = new HashMap<>();
properties.put(SecurityConstants.CALLBACK_HANDLER, "org.apache.cxf.systest.jaxrs.security.saml.KeystorePasswordCallback");
properties.put(SecurityConstants.ENCRYPT_USERNAME, "bob");
properties.put(SecurityConstants.ENCRYPT_PROPERTIES, "org/apache/cxf/systest/jaxrs/security/bob.properties");
properties.put(SecurityConstants.SIGNATURE_USERNAME, "alice");
properties.put(SecurityConstants.SIGNATURE_PROPERTIES, "org/apache/cxf/systest/jaxrs/security/alice.properties");
EncryptionProperties encryptionProperties = new EncryptionProperties();
encryptionProperties.setEncryptionSymmetricKeyAlgo("http://www.w3.org/2009/xmlenc11#aes128-gcm");
encryptionProperties.setEncryptionKeyIdType(RSSecurityUtils.X509_CERT);
try {
doTestPostEncryptedBook(address, true, properties, encryptionProperties, true, test.streaming);
} catch (BadRequestException ex) {
assertEquals(400, ex.getResponse().getStatus());
}
}
use of javax.ws.rs.BadRequestException in project cxf by apache.
the class JAXRSXmlSecTest method testUnsignedServerResponse.
@Test
public void testUnsignedServerResponse() throws Exception {
if (STAX_PORT.equals(test.port)) {
// We are only testing the client here
return;
}
String address = "https://localhost:" + test.port + "/xmlnosigresponse/bookstore/books";
JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
bean.setAddress(address);
SpringBusFactory bf = new SpringBusFactory();
URL busFile = JAXRSXmlSecTest.class.getResource("client.xml");
Bus springBus = bf.createBus(busFile.toString());
bean.setBus(springBus);
Map<String, Object> properties = new HashMap<>();
properties.put(SecurityConstants.CALLBACK_HANDLER, "org.apache.cxf.systest.jaxrs.security.saml.KeystorePasswordCallback");
properties.put(SecurityConstants.SIGNATURE_USERNAME, "alice");
properties.put(SecurityConstants.SIGNATURE_PROPERTIES, "org/apache/cxf/systest/jaxrs/security/alice.properties");
bean.setProperties(properties);
if (test.streaming) {
XmlSecOutInterceptor sigOutInterceptor = new XmlSecOutInterceptor();
sigOutInterceptor.setSignRequest(true);
bean.getOutInterceptors().add(sigOutInterceptor);
XmlSecInInterceptor sigInInterceptor = new XmlSecInInterceptor();
sigInInterceptor.setRequireSignature(true);
bean.setProvider(sigInInterceptor);
} else {
XmlSigOutInterceptor sigOutInterceptor = new XmlSigOutInterceptor();
bean.getOutInterceptors().add(sigOutInterceptor);
XmlSigInInterceptor sigInInterceptor = new XmlSigInInterceptor();
bean.getInInterceptors().add(sigInInterceptor);
}
WebClient wc = bean.createWebClient();
WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000L);
try {
wc.type("application/xml").post(new Book("CXF", 126L), Book.class);
fail("Failure expected on an unsigned response message");
} catch (ProcessingException ex) {
assertTrue(ex.getCause() instanceof BadRequestException);
}
}
use of javax.ws.rs.BadRequestException in project cxf by apache.
the class AbstractSignatureInFilter method verifyDigest.
protected byte[] verifyDigest(MultivaluedMap<String, String> headers, InputStream entityStream) {
byte[] messageBody = null;
if (!enabled) {
return messageBody;
}
// configuration to require that the digest is signed (and hence present)
if (entityStream != null && headers.containsKey("Digest")) {
LOG.fine("Digesting message body");
try {
messageBody = IOUtils.readBytesFromStream(entityStream);
} catch (IOException e) {
throw new DigestFailureException("failed to validate the digest", e);
}
DigestVerifier digestVerifier = new DigestVerifier();
try {
digestVerifier.inspectDigest(messageBody, headers);
} catch (DigestFailureException | DifferentDigestsException | MissingDigestException ex) {
Message message = PhaseInterceptorChain.getCurrentMessage();
if (MessageUtils.isRequestor(message)) {
throw ex;
}
throw new BadRequestException(ex);
}
}
LOG.fine("Finished digest message verification process");
return messageBody;
}
use of javax.ws.rs.BadRequestException in project openremote by openremote.
the class AssetDatapointResourceImpl method getDatapointExport.
@Override
public void getDatapointExport(AsyncResponse asyncResponse, String attributeRefsString, long fromTimestamp, long toTimestamp) {
try {
AttributeRef[] attributeRefs = JSON.readValue(attributeRefsString, AttributeRef[].class);
for (AttributeRef attributeRef : attributeRefs) {
if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), attributeRef.getId())) {
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
Asset<?> asset = assetStorageService.find(attributeRef.getId(), true);
if (asset == null) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
if (!isTenantActiveAndAccessible(asset.getRealm())) {
DATA_EXPORT_LOG.info("Forbidden access for user '" + getUsername() + "': " + asset);
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
asset.getAttribute(attributeRef.getName()).orElseThrow(() -> new WebApplicationException(Response.Status.NOT_FOUND));
}
DATA_EXPORT_LOG.info("User '" + getUsername() + "' started data export for " + attributeRefsString + " from " + fromTimestamp + " to " + toTimestamp);
ScheduledFuture<File> exportFuture = assetDatapointService.exportDatapoints(attributeRefs, fromTimestamp, toTimestamp);
asyncResponse.register((ConnectionCallback) disconnected -> {
exportFuture.cancel(true);
});
File exportFile = null;
try {
exportFile = exportFuture.get();
ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
FileInputStream fin = new FileInputStream(exportFile);
ZipEntry zipEntry = new ZipEntry(exportFile.getName());
zipOut.putNextEntry(zipEntry);
IOUtils.copy(fin, zipOut);
zipOut.closeEntry();
zipOut.close();
fin.close();
response.setContentType("application/zip");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"dataexport.zip\"");
asyncResponse.resume(response);
} catch (Exception ex) {
exportFuture.cancel(true);
asyncResponse.resume(new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR));
DATA_EXPORT_LOG.log(Level.SEVERE, "Exception in ScheduledFuture: ", ex);
} finally {
if (exportFile != null && exportFile.exists()) {
try {
exportFile.delete();
} catch (Exception e) {
DATA_EXPORT_LOG.log(Level.SEVERE, "Failed to delete temporary export file: " + exportFile.getPath(), e);
}
}
}
} catch (JsonProcessingException ex) {
asyncResponse.resume(new BadRequestException(ex));
}
}
use of javax.ws.rs.BadRequestException in project openremote by openremote.
the class AssetDatapointResourceImpl method getDatapoints.
@Override
public ValueDatapoint<?>[] getDatapoints(@BeanParam RequestParams requestParams, String assetId, String attributeName, DatapointInterval interval, Integer stepSize, long fromTimestamp, long toTimestamp) {
try {
if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), assetId)) {
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
Asset<?> asset = assetStorageService.find(assetId, true);
if (asset == null) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
if (!isTenantActiveAndAccessible(asset.getRealm())) {
LOG.info("Forbidden access for user '" + getUsername() + "': " + asset);
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
Attribute<?> attribute = asset.getAttribute(attributeName).orElseThrow(() -> new WebApplicationException(Response.Status.NOT_FOUND));
return assetDatapointService.getValueDatapoints(assetId, attribute, interval, stepSize, LocalDateTime.ofInstant(Instant.ofEpochMilli(fromTimestamp), ZoneId.systemDefault()), LocalDateTime.ofInstant(Instant.ofEpochMilli(toTimestamp), ZoneId.systemDefault()));
} catch (IllegalStateException ex) {
throw new BadRequestException(ex);
} catch (UnsupportedOperationException ex) {
throw new NotSupportedException(ex);
}
}
Aggregations