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);
}
}
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());
}
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;
}
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();
}
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;
}
Aggregations