Search in sources :

Example 26 with BadRequestException

use of javax.ws.rs.BadRequestException in project jersey by jersey.

the class AbstractCollectionJaxbProvider method readFrom.

@Override
@SuppressWarnings("unchecked")
public final Object readFrom(Class<Object> 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());
    }
    try {
        final Class<?> elementType = getElementClass(type, genericType);
        final Unmarshaller u = getUnmarshaller(elementType, mediaType);
        final XMLStreamReader r = getXMLStreamReader(elementType, mediaType, u, entityStream);
        boolean jaxbElement = false;
        Collection<Object> l = null;
        if (type.isArray()) {
            l = new ArrayList<Object>();
        } else {
            try {
                l = (Collection<Object>) type.newInstance();
            } catch (Exception e) {
                for (Class<?> c : DEFAULT_IMPLS) {
                    if (type.isAssignableFrom(c)) {
                        try {
                            l = (Collection<Object>) c.newInstance();
                            break;
                        } catch (InstantiationException ex) {
                            LOGGER.log(Level.WARNING, LocalizationMessages.UNABLE_TO_INSTANTIATE_CLASS(c.getName()), ex);
                        } catch (IllegalAccessException ex) {
                            LOGGER.log(Level.WARNING, LocalizationMessages.UNABLE_TO_INSTANTIATE_CLASS(c.getName()), ex);
                        } catch (SecurityException ex) {
                            LOGGER.log(Level.WARNING, LocalizationMessages.UNABLE_TO_INSTANTIATE_CLASS(c.getName()), ex);
                        }
                    }
                }
            }
        }
        if (l == null) {
            l = new ArrayList<Object>();
        }
        // Move to root element
        int event = r.next();
        while (event != XMLStreamReader.START_ELEMENT) {
            event = r.next();
        }
        // Move to first child (if any)
        event = r.next();
        while (event != XMLStreamReader.START_ELEMENT && event != XMLStreamReader.END_DOCUMENT) {
            event = r.next();
        }
        while (event != XMLStreamReader.END_DOCUMENT) {
            if (elementType.isAnnotationPresent(XmlRootElement.class)) {
                l.add(u.unmarshal(r));
            } else if (elementType.isAnnotationPresent(XmlType.class)) {
                l.add(u.unmarshal(r, elementType).getValue());
            } else {
                l.add(u.unmarshal(r, elementType));
                jaxbElement = true;
            }
            // Move to next peer (if any)
            event = r.getEventType();
            while (event != XMLStreamReader.START_ELEMENT && event != XMLStreamReader.END_DOCUMENT) {
                event = r.next();
            }
        }
        return (type.isArray()) ? createArray(l, jaxbElement ? JAXBElement.class : elementType) : l;
    } catch (UnmarshalException ex) {
        throw new BadRequestException(ex);
    } catch (XMLStreamException ex) {
        throw new BadRequestException(ex);
    } catch (JAXBException ex) {
        throw new InternalServerErrorException(ex);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) JAXBException(javax.xml.bind.JAXBException) NoContentException(javax.ws.rs.core.NoContentException) XMLStreamException(javax.xml.stream.XMLStreamException) BadRequestException(javax.ws.rs.BadRequestException) UnmarshalException(javax.xml.bind.UnmarshalException) NoContentException(javax.ws.rs.core.NoContentException) IOException(java.io.IOException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) JAXBException(javax.xml.bind.JAXBException) WebApplicationException(javax.ws.rs.WebApplicationException) XmlType(javax.xml.bind.annotation.XmlType) XMLStreamException(javax.xml.stream.XMLStreamException) UnmarshalException(javax.xml.bind.UnmarshalException) Collection(java.util.Collection) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) EntityInputStream(org.glassfish.jersey.message.internal.EntityInputStream) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 27 with BadRequestException

use of javax.ws.rs.BadRequestException in project jersey by jersey.

the class AircraftsResource method delete.

@DELETE
@Path("{id}")
@Produces(TEXT_PLAIN)
@RolesAllowed("admin")
public String delete(@ValidAircraftId @PathParam("id") Integer id) {
    Flight flight = DataStore.selectFlightByAircraft(id);
    if (flight != null) {
        throw new BadRequestException("Aircraft assigned to a flight.");
    }
    Aircraft aircraft = DataStore.removeAircraft(id);
    return String.format("%03d", aircraft.getId());
}
Also used : Flight(org.glassfish.jersey.examples.flight.model.Flight) BadRequestException(javax.ws.rs.BadRequestException) Aircraft(org.glassfish.jersey.examples.flight.model.Aircraft) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces)

Example 28 with BadRequestException

use of javax.ws.rs.BadRequestException in project jersey by jersey.

the class AircraftsResource method create.

@POST
@Consumes(APPLICATION_FORM_URLENCODED)
@RolesAllowed("admin")
@Detail
public Aircraft create(@FormParam("manufacturer") String manufacturer, @FormParam("type") String type, @FormParam("capacity") Integer capacity, @DefaultValue("0") @FormParam("x-pos") Integer x, @DefaultValue("0") @FormParam("y-pos") Integer y) {
    if (manufacturer == null || type == null || capacity == null) {
        throw new BadRequestException("Incomplete data.");
    }
    Aircraft aircraft = new Aircraft();
    aircraft.setType(new AircraftType(manufacturer, type, capacity));
    aircraft.setLocation(SimEngine.bound(new Location(x, y)));
    if (!DataStore.addAircraft(aircraft)) {
        throw new InternalServerErrorException("Unable to add new aircraft.");
    }
    return aircraft;
}
Also used : BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Aircraft(org.glassfish.jersey.examples.flight.model.Aircraft) AircraftType(org.glassfish.jersey.examples.flight.model.AircraftType) Location(org.glassfish.jersey.examples.flight.model.Location) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Detail(org.glassfish.jersey.examples.flight.filtering.Detail)

Example 29 with BadRequestException

use of javax.ws.rs.BadRequestException in project jersey by jersey.

the class FlightsResource method updateStatus.

@POST
@Path("{id}/status")
@Consumes(APPLICATION_FORM_URLENCODED)
@Produces(TEXT_PLAIN)
@RolesAllowed("admin")
public String updateStatus(@ValidFlightId @PathParam("id") String flightId, @FormParam("status") String newStatus) {
    Flight.Status status;
    try {
        status = Flight.Status.valueOf(newStatus);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException("Unknown status.");
    }
    final Flight flight = DataStore.selectFlight(flightId);
    flight.setStatus(status);
    return status.name();
}
Also used : Flight(org.glassfish.jersey.examples.flight.model.Flight) BadRequestException(javax.ws.rs.BadRequestException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 30 with BadRequestException

use of javax.ws.rs.BadRequestException in project jersey by jersey.

the class FlightsResource method create.

@POST
@Consumes(APPLICATION_FORM_URLENCODED)
@RolesAllowed("admin")
@Detail
public Flight create(@ValidAircraftId @FormParam("aircraftId") Integer aircraftId) {
    final Aircraft aircraft = DataStore.selectAircraft(aircraftId);
    if (!aircraft.marAssigned()) {
        throw new BadRequestException("Aircraft already assigned.");
    }
    Flight flight = new Flight(null, aircraft);
    if (!DataStore.addFlight(flight)) {
        aircraft.marAvailable();
        throw new BadRequestException("Flight already exists.");
    }
    return flight;
}
Also used : Flight(org.glassfish.jersey.examples.flight.model.Flight) BadRequestException(javax.ws.rs.BadRequestException) Aircraft(org.glassfish.jersey.examples.flight.model.Aircraft) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Detail(org.glassfish.jersey.examples.flight.filtering.Detail)

Aggregations

BadRequestException (javax.ws.rs.BadRequestException)58 ApiOperation (io.swagger.annotations.ApiOperation)34 AuditEvent (org.graylog2.audit.jersey.AuditEvent)31 Timed (com.codahale.metrics.annotation.Timed)26 Path (javax.ws.rs.Path)26 ApiResponses (io.swagger.annotations.ApiResponses)22 POST (javax.ws.rs.POST)20 Produces (javax.ws.rs.Produces)20 Consumes (javax.ws.rs.Consumes)18 URI (java.net.URI)13 PUT (javax.ws.rs.PUT)13 ValidationException (org.graylog2.plugin.database.ValidationException)11 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)9 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)8 NotFoundException (org.graylog2.database.NotFoundException)8 Stream (org.graylog2.plugin.streams.Stream)8 DELETE (javax.ws.rs.DELETE)6 NotFoundException (javax.ws.rs.NotFoundException)6 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)5 ConfigurationException (org.graylog2.plugin.configuration.ConfigurationException)5