use of javax.ws.rs.BadRequestException in project keywhiz by square.
the class ClientResource method doCreateClient.
private Response doCreateClient(AutomationClient automationClient, CreateClientRequestV2 request) {
String creator = automationClient.getName();
String client = request.name();
setTag("client", client);
clientDAOReadWrite.getClientByName(client).ifPresent((c) -> {
logger.info("Automation ({}) - Client {} already exists", creator, client);
throw new ConflictException("Client name already exists.");
});
// Creates new client record
long clientId;
try {
clientId = clientDAOReadWrite.createClient(client, creator, request.description(), new URI(request.spiffeId()));
} catch (URISyntaxException e) {
logger.info(format("Automation (%s) - Client %s could not be created because of invalid SPIFFE ID %s", creator, client, request.spiffeId()), e);
throw new BadRequestException("Invalid SPIFFE ID provided (not a URI)");
}
auditLog.recordEvent(new Event(Instant.now(), EventTag.CLIENT_CREATE, creator, client));
// Enrolls client in any requested groups
groupsToGroupIds(request.groups()).forEach((maybeGroupId) -> maybeGroupId.ifPresent((groupId) -> aclDAOReadWrite.findAndEnrollClient(clientId, groupId, auditLog, creator, new HashMap<>())));
URI uri = UriBuilder.fromResource(ClientResource.class).path(client).build();
return Response.created(uri).build();
}
use of javax.ws.rs.BadRequestException in project keywhiz by square.
the class SecretDAO method createSecret.
@VisibleForTesting
public long createSecret(String name, String ownerName, String encryptedSecret, String hmac, String creator, Map<String, String> metadata, long expiry, String description, @Nullable String type, @Nullable Map<String, String> generationOptions) {
return dslContext.transactionResult(configuration -> {
// check is here because this is where all APIs converge on secret creation
if (name.startsWith(".")) {
throw new BadRequestException(format("secret cannot be created with name `%s` - secret " + "names cannot begin with a period", name));
}
// enforce a shorter max length than the db to ensure secrets renamed on deletion still fit
if (name.length() > SECRET_NAME_MAX_LENGTH) {
throw new BadRequestException(format("secret cannot be created with name `%s` - secret " + "names must be %d characters or less", name, SECRET_NAME_MAX_LENGTH));
}
long now = OffsetDateTime.now().toEpochSecond();
SecretContentDAO secretContentDAO = secretContentDAOFactory.using(configuration);
SecretSeriesDAO secretSeriesDAO = secretSeriesDAOFactory.using(configuration);
Long ownerId = getOwnerId(configuration, ownerName);
Optional<SecretSeries> secretSeries = secretSeriesDAO.getSecretSeriesByName(name);
long secretId;
if (secretSeries.isPresent()) {
SecretSeries secretSeries1 = secretSeries.get();
if (secretSeries1.currentVersion().isPresent()) {
throw new DataAccessException(format("secret already present: %s", name));
} else {
// Unreachable unless the implementation of getSecretSeriesByName is changed
throw new IllegalStateException(format("secret %s retrieved without current version set", name));
}
} else {
secretId = secretSeriesDAO.createSecretSeries(name, ownerId, creator, description, type, generationOptions, now);
}
long secretContentId = secretContentDAO.createSecretContent(secretId, encryptedSecret, hmac, creator, metadata, expiry, now);
secretSeriesDAO.setCurrentVersion(secretId, secretContentId, creator, now);
return secretId;
});
}
use of javax.ws.rs.BadRequestException in project syndesis-qe by syndesisio.
the class StepDescriptorEndpoint method getStepDescriptor.
public StepDescriptor getStepDescriptor(String stepKind, DataShape in, DataShape out) {
super.setEndpointName("/steps/" + stepKind + "/descriptor");
log.debug("POST, destination : {}", getEndpointUrl());
final Invocation.Builder invocation = createInvocation(null);
DynamicActionMetadata metadata = new DynamicActionMetadata.Builder().inputShape(in).outputShape(out).build();
JsonNode response;
try {
response = invocation.post(Entity.entity(metadata, MediaType.APPLICATION_JSON), JsonNode.class);
} catch (BadRequestException ex) {
log.error("Bad request, trying again in 15 seconds");
try {
Thread.sleep(15000L);
} catch (InterruptedException ignore) {
// ignore
}
response = invocation.post(Entity.entity(metadata, MediaType.APPLICATION_JSON), JsonNode.class);
}
return transformJsonNode(response, StepDescriptor.class);
}
use of javax.ws.rs.BadRequestException in project verify-hub by alphagov.
the class SoapRequestClientTest method makePost_checkSOAPRequestErrorIsThrownWhenNotValidXML.
@Test
public void makePost_checkSOAPRequestErrorIsThrownWhenNotValidXML() throws Exception {
when(webResourceBuilder.post(any(Entity.class))).thenReturn(response);
when(response.readEntity(Document.class)).thenThrow(new BadRequestException());
when(response.getStatus()).thenReturn(200);
Element matchingServiceRequest = XmlUtils.convertToElement("<someElement/>");
URI matchingServiceUri = new URI("http://heyyeyaaeyaaaeyaeyaa.com/abc1");
try {
soapRequestClient.makeSoapRequest(matchingServiceRequest, matchingServiceUri);
fail("Exception should have been thrown");
} catch (SOAPRequestError ignored) {
} finally {
verify(response).readEntity(Document.class);
}
}
use of javax.ws.rs.BadRequestException in project cxf by apache.
the class ExceptionUtilsTest method testConvertFaultToResponseWAESubClassWithResponse.
@Test
public void testConvertFaultToResponseWAESubClassWithResponse() {
Message m = createMessage();
BadRequestException wae = new BadRequestException(Response.status(400).type(MediaType.TEXT_HTML).entity("<em>fromBRE</em>").build());
Response r = ExceptionUtils.convertFaultToResponse(wae, m);
assertEquals(400, r.getStatus());
assertEquals(MediaType.TEXT_HTML_TYPE, r.getMediaType());
assertEquals("<em>fromBRE</em>", r.readEntity(String.class));
}
Aggregations