Search in sources :

Example 1 with BadRequestException

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);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) UnmarshalException(javax.xml.bind.UnmarshalException) JAXBException(javax.xml.bind.JAXBException) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) EntityInputStream(org.glassfish.jersey.message.internal.EntityInputStream) NoContentException(javax.ws.rs.core.NoContentException)

Example 2 with BadRequestException

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();
}
Also used : Response(javax.ws.rs.core.Response) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) BadRequestException(javax.ws.rs.BadRequestException) Herder(org.apache.kafka.connect.runtime.Herder) FutureCallback(org.apache.kafka.connect.util.FutureCallback) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Example 3 with BadRequestException

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());
            }
        }
    }
}
Also used : Form(javax.ws.rs.core.Form) BadRequestException(javax.ws.rs.BadRequestException) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) List(java.util.List) UriBuilder(javax.ws.rs.core.UriBuilder) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map)

Example 4 with BadRequestException

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"));
    }
}
Also used : MultiPart(org.glassfish.jersey.media.multipart.MultiPart) LogRecord(java.util.logging.LogRecord) MediaType(javax.ws.rs.core.MediaType) BadRequestException(javax.ws.rs.BadRequestException) WebTarget(javax.ws.rs.client.WebTarget) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 5 with BadRequestException

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();
}
Also used : StreamRule(org.graylog2.plugin.streams.StreamRule) CreateStreamRuleRequest(org.graylog2.rest.resources.streams.rules.requests.CreateStreamRuleRequest) BadRequestException(javax.ws.rs.BadRequestException) Stream(org.graylog2.plugin.streams.Stream) URI(java.net.URI) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Aggregations

BadRequestException (javax.ws.rs.BadRequestException)178 ApiOperation (io.swagger.annotations.ApiOperation)80 Path (javax.ws.rs.Path)68 AuditEvent (org.graylog2.audit.jersey.AuditEvent)52 POST (javax.ws.rs.POST)51 Produces (javax.ws.rs.Produces)46 Consumes (javax.ws.rs.Consumes)45 Timed (com.codahale.metrics.annotation.Timed)40 ApiResponses (io.swagger.annotations.ApiResponses)38 PUT (javax.ws.rs.PUT)33 NotFoundException (javax.ws.rs.NotFoundException)31 GET (javax.ws.rs.GET)27 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)27 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)26 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)23 URI (java.net.URI)22 Response (javax.ws.rs.core.Response)20 DELETE (javax.ws.rs.DELETE)19 SearchQuery (org.graylog2.search.SearchQuery)19 IOException (java.io.IOException)17