use of javax.ws.rs.BadRequestException in project jersey by jersey.
the class AbstractJaxbElementProvider method readFrom.
@Override
public final JAXBElement<?> readFrom(Class<JAXBElement<?>> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream inputStream) throws IOException {
final EntityInputStream entityStream = EntityInputStream.create(inputStream);
if (entityStream.isEmpty()) {
throw new NoContentException(LocalizationMessages.ERROR_READING_ENTITY_MISSING());
}
final ParameterizedType pt = (ParameterizedType) genericType;
final Class ta = (Class) pt.getActualTypeArguments()[0];
try {
return readFrom(ta, mediaType, getUnmarshaller(ta, mediaType), entityStream);
} catch (UnmarshalException ex) {
throw new BadRequestException(ex);
} catch (JAXBException ex) {
throw new InternalServerErrorException(ex);
}
}
use of javax.ws.rs.BadRequestException in project kafka by apache.
the class ConnectorsResource method putConnectorConfig.
@PUT
@Path("/{connector}/config")
public Response putConnectorConfig(@PathParam("connector") final String connector, @QueryParam("forward") final Boolean forward, final Map<String, String> connectorConfig) throws Throwable {
FutureCallback<Herder.Created<ConnectorInfo>> cb = new FutureCallback<>();
String includedName = connectorConfig.get(ConnectorConfig.NAME_CONFIG);
if (includedName != null) {
if (!includedName.equals(connector))
throw new BadRequestException("Connector name configuration (" + includedName + ") doesn't match connector name in the URL (" + connector + ")");
} else {
connectorConfig.put(ConnectorConfig.NAME_CONFIG, connector);
}
herder.putConnectorConfig(connector, connectorConfig, true, cb);
Herder.Created<ConnectorInfo> createdInfo = completeOrForwardRequest(cb, "/connectors/" + connector + "/config", "PUT", connectorConfig, new TypeReference<ConnectorInfo>() {
}, new CreatedConnectorInfoTranslator(), forward);
Response.ResponseBuilder response;
if (createdInfo.created())
response = Response.created(URI.create("/connectors/" + connector));
else
response = Response.ok();
return response.entity(createdInfo.result()).build();
}
use of javax.ws.rs.BadRequestException in project jersey by jersey.
the class HttpMethodOverrideFilter method filter.
@Override
public void filter(final ContainerRequestContext request) {
if (!request.getMethod().equalsIgnoreCase("POST")) {
return;
}
final String header = getParamValue(Source.HEADER, request.getHeaders(), "X-HTTP-Method-Override");
final String query = getParamValue(Source.QUERY, request.getUriInfo().getQueryParameters(), "_method");
final String override;
if (header == null) {
override = query;
} else {
override = header;
if (query != null && !query.equals(header)) {
// inconsistent query and header param values
throw new BadRequestException();
}
}
if (override != null) {
request.setMethod(override);
if (override.equals("GET")) {
if (request.getMediaType() != null && MediaType.APPLICATION_FORM_URLENCODED_TYPE.getType().equals(request.getMediaType().getType())) {
final UriBuilder ub = request.getUriInfo().getRequestUriBuilder();
final Form f = ((ContainerRequest) request).readEntity(Form.class);
for (final Map.Entry<String, List<String>> param : f.asMap().entrySet()) {
ub.queryParam(param.getKey(), param.getValue().toArray());
}
request.setRequestUri(request.getUriInfo().getBaseUri(), ub.build());
}
}
}
}
use of javax.ws.rs.BadRequestException in project jersey by jersey.
the class MultiPartHeaderModificationTest method testLogMessage.
@Test
public void testLogMessage() {
final WebTarget target = target().path("multipart/ten");
MultiPartBean bean = new MultiPartBean("myname", "myvalue");
MultiPart entity = new MultiPart().bodyPart(bean, new MediaType("x-application", "x-format")).bodyPart("", MediaType.APPLICATION_OCTET_STREAM_TYPE);
final String UNSENT_HEADER_CHANGES = "Unsent header changes";
try {
target.request("text/plain").put(Entity.entity(entity, "multipart/mixed"), String.class);
assertFalse("BadRequestException can not be thrown just in case JERSEY-2341 is not fixed.", messageLogged);
LogRecord logRecord = findLogRecord(UNSENT_HEADER_CHANGES);
assertNull(logRecord);
} catch (BadRequestException brex) {
assertTrue("BadRequestException can be thrown just in case JERSEY-2341 is not fixed.", messageLogged);
LogRecord logRecord = findLogRecord(UNSENT_HEADER_CHANGES);
assertNotNull("Missing LogRecord for message '" + UNSENT_HEADER_CHANGES + "'.", logRecord);
assertThat(logRecord.getMessage(), containsString("MIME-Version"));
assertThat(logRecord.getMessage(), containsString("Content-Type"));
}
}
use of javax.ws.rs.BadRequestException in project graylog2-server by Graylog2.
the class StreamResource method create.
@POST
@Timed
@ApiOperation(value = "Create a stream")
@RequiresPermissions(RestPermissions.STREAMS_CREATE)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.STREAM_CREATE)
public Response create(@ApiParam(name = "JSON body", required = true) final CreateStreamRequest cr) throws ValidationException {
// Create stream.
final Stream stream = streamService.create(cr, getCurrentUser().getName());
stream.setDisabled(true);
if (!stream.getIndexSet().getConfig().isWritable()) {
throw new BadRequestException("Assigned index set must be writable!");
}
final String id = streamService.save(stream);
final List<CreateStreamRuleRequest> rules = firstNonNull(cr.rules(), Collections.<CreateStreamRuleRequest>emptyList());
for (CreateStreamRuleRequest request : rules) {
StreamRule streamRule = streamRuleService.create(id, request);
streamRuleService.save(streamRule);
}
clusterEventBus.post(StreamsChangedEvent.create(stream.getId()));
final Map<String, String> result = ImmutableMap.of("stream_id", id);
final URI streamUri = getUriBuilderToSelf().path(StreamResource.class).path("{streamId}").build(id);
return Response.created(streamUri).entity(result).build();
}
Aggregations