use of javax.ws.rs.WebApplicationException in project jersey by jersey.
the class BasicTypesMessageProvider method readFrom.
@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
final String entityString = readFromAsString(entityStream, mediaType);
if (entityString.isEmpty()) {
throw new NoContentException(LocalizationMessages.ERROR_READING_ENTITY_MISSING());
}
final PrimitiveTypes primitiveType = PrimitiveTypes.forType(type);
if (primitiveType != null) {
return primitiveType.convert(entityString);
}
final Constructor constructor = AccessController.doPrivileged(ReflectionHelper.getStringConstructorPA(type));
if (constructor != null) {
try {
return type.cast(constructor.newInstance(entityString));
} catch (Exception e) {
throw new MessageBodyProcessingException(LocalizationMessages.ERROR_ENTITY_PROVIDER_BASICTYPES_CONSTRUCTOR(type));
}
}
if (AtomicInteger.class.isAssignableFrom(type)) {
return new AtomicInteger((Integer) PrimitiveTypes.INTEGER.convert(entityString));
}
if (AtomicLong.class.isAssignableFrom(type)) {
return new AtomicLong((Long) PrimitiveTypes.LONG.convert(entityString));
}
throw new MessageBodyProcessingException(LocalizationMessages.ERROR_ENTITY_PROVIDER_BASICTYPES_UNKWNOWN(type));
}
use of javax.ws.rs.WebApplicationException in project jersey by jersey.
the class ExceptionResource method testWebApplicationExceptionEntity.
@POST
@Path("webapplication_entity")
public String testWebApplicationExceptionEntity(String s) {
String[] tokens = s.split(":");
assert tokens.length == 2;
int statusCode = Integer.valueOf(tokens[1]);
Response r = Response.status(statusCode).entity(s).build();
throw new WebApplicationException(r);
}
use of javax.ws.rs.WebApplicationException in project jersey by jersey.
the class RequestUtil method getEntityParameters.
/**
* Returns the form parameters from a request entity as a multi-valued map.
* If the request does not have a POST method, or the media type is not
* x-www-form-urlencoded, then null is returned.
*
* @param request the client request containing the entity to extract parameters from.
* @return a {@link javax.ws.rs.core.MultivaluedMap} containing the entity form parameters.
*/
@SuppressWarnings("unchecked")
public static MultivaluedMap<String, String> getEntityParameters(ClientRequestContext request, MessageBodyWorkers messageBodyWorkers) {
Object entity = request.getEntity();
String method = request.getMethod();
MediaType mediaType = request.getMediaType();
// no entity, not a post or not x-www-form-urlencoded: return empty map
if (entity == null || method == null || !HttpMethod.POST.equalsIgnoreCase(method) || mediaType == null || !mediaType.equals(MediaType.APPLICATION_FORM_URLENCODED_TYPE)) {
return new MultivaluedHashMap<String, String>();
}
// it's ready to go if already expressed as a multi-valued map
if (entity instanceof MultivaluedMap) {
return (MultivaluedMap<String, String>) entity;
}
Type entityType = entity.getClass();
// if the entity is generic, get specific type and class
if (entity instanceof GenericEntity) {
final GenericEntity generic = (GenericEntity) entity;
// overwrite
entityType = generic.getType();
entity = generic.getEntity();
}
final Class entityClass = entity.getClass();
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessageBodyWriter writer = messageBodyWorkers.getMessageBodyWriter(entityClass, entityType, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
try {
writer.writeTo(entity, entityClass, entityType, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE, null, out);
} catch (WebApplicationException wae) {
throw new IllegalStateException(wae);
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
MessageBodyReader reader = messageBodyWorkers.getMessageBodyReader(MultivaluedMap.class, MultivaluedMap.class, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
try {
return (MultivaluedMap<String, String>) reader.readFrom(MultivaluedMap.class, MultivaluedMap.class, EMPTY_ANNOTATIONS, MediaType.APPLICATION_FORM_URLENCODED_TYPE, null, in);
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
}
use of javax.ws.rs.WebApplicationException in project jersey by jersey.
the class AccessTokenResource method postAccessTokenRequest.
/**
* POST method for creating a request for Request Token.
* @return an HTTP response with content of the updated or created resource.
*/
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_FORM_URLENCODED)
@TokenResource
public Response postAccessTokenRequest(@Context ContainerRequestContext requestContext, @Context Request req) {
boolean sigIsOk = false;
OAuthServerRequest request = new OAuthServerRequest(requestContext);
OAuth1Parameters params = new OAuth1Parameters();
params.readRequest(request);
if (params.getToken() == null) {
throw new WebApplicationException(new Throwable("oauth_token MUST be present."), 400);
}
String consKey = params.getConsumerKey();
if (consKey == null) {
throw new OAuth1Exception(Response.Status.BAD_REQUEST, null);
}
OAuth1Token rt = provider.getRequestToken(params.getToken());
if (rt == null) {
// token invalid
throw new OAuth1Exception(Response.Status.BAD_REQUEST, null);
}
OAuth1Consumer consumer = rt.getConsumer();
if (consumer == null || !consKey.equals(consumer.getKey())) {
// token invalid
throw new OAuth1Exception(Response.Status.BAD_REQUEST, null);
}
OAuth1Secrets secrets = new OAuth1Secrets().consumerSecret(consumer.getSecret()).tokenSecret(rt.getSecret());
try {
sigIsOk = oAuth1Signature.verify(request, params, secrets);
} catch (OAuth1SignatureException ex) {
Logger.getLogger(AccessTokenResource.class.getName()).log(Level.SEVERE, null, ex);
}
if (!sigIsOk) {
// signature invalid
throw new OAuth1Exception(Response.Status.BAD_REQUEST, null);
}
// We're good to go.
OAuth1Token at = provider.newAccessToken(rt, params.getVerifier());
if (at == null) {
throw new OAuth1Exception(Response.Status.BAD_REQUEST, null);
}
// Preparing the response.
Form resp = new Form();
resp.param(OAuth1Parameters.TOKEN, at.getToken());
resp.param(OAuth1Parameters.TOKEN_SECRET, at.getSecret());
resp.asMap().putAll(at.getAttributes());
return Response.ok(resp).build();
}
use of javax.ws.rs.WebApplicationException in project jersey by jersey.
the class MessageBodyReaderTestFormat method readFrom.
@Override
public Message readFrom(final Class<Message> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException, WebApplicationException {
final BufferedReader reader = new BufferedReader(new InputStreamReader(entityStream, MessageUtils.getCharset(mediaType)));
final String line = reader.readLine();
if (line == null || !line.startsWith(Utils.FORMAT_PREFIX) || !line.endsWith(Utils.FORMAT_SUFFIX)) {
throw new WebApplicationException(new IllegalArgumentException("Input content '" + line + "' is not in a valid format!"));
}
final String text = line.substring(Utils.FORMAT_PREFIX.length(), line.length() - Utils.FORMAT_SUFFIX.length());
if (serverSide) {
Utils.throwException(text, this, Utils.TestAction.MESSAGE_BODY_READER_THROW_WEB_APPLICATION, Utils.TestAction.MESSAGE_BODY_READER_THROW_PROCESSING, Utils.TestAction.MESSAGE_BODY_READER_THROW_ANY);
}
return new Message(text);
}
Aggregations