Search in sources :

Example 6 with MediaType

use of javax.ws.rs.core.MediaType in project OpenAttestation by OpenAttestation.

the class ApiExceptionTest method testGetHttpReasonPhrase.

@Test
public void testGetHttpReasonPhrase() {
    MediaType contentType = new MediaType();
    byte[] content = new byte[] { 0x30, 0x31 };
    ApiResponse response = new ApiResponse(404, "Not Found", contentType, content);
    ApiException apiexception = new ApiException(response, "error!");
    assertEquals("Not Found", apiexception.getHttpReasonPhrase());
}
Also used : MediaType(javax.ws.rs.core.MediaType) Test(org.junit.Test)

Example 7 with MediaType

use of javax.ws.rs.core.MediaType in project OpenAttestation by OpenAttestation.

the class ApacheHttpClient method readResponse.

private ApiResponse readResponse(HttpResponse response) throws IOException {
    MediaType contentType = createMediaType(response);
    byte[] content = null;
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream contentStream = entity.getContent();
        if (contentStream != null) {
            content = IOUtils.toByteArray(contentStream);
            contentStream.close();
        }
    }
    return new ApiResponse(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase(), contentType, content);
}
Also used : HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) MediaType(javax.ws.rs.core.MediaType)

Example 8 with MediaType

use of javax.ws.rs.core.MediaType in project jersey by jersey.

the class HttpAuthenticationFilter method repeatRequest.

/**
     * Repeat the {@code request} with provided {@code newAuthorizationHeader}
     * and update the {@code response} with newest response data.
     *
     * @param request                Request context.
     * @param response               Response context (will be updated with the new response data).
     * @param newAuthorizationHeader {@code Authorization} header that should be added to the new request.
     * @return {@code true} is the authentication was successful ({@code true} if 401 response code was not returned;
     * {@code false} otherwise).
     */
static boolean repeatRequest(ClientRequestContext request, ClientResponseContext response, String newAuthorizationHeader) {
    Client client = request.getClient();
    String method = request.getMethod();
    MediaType mediaType = request.getMediaType();
    URI lUri = request.getUri();
    WebTarget resourceTarget = client.target(lUri);
    Invocation.Builder builder = resourceTarget.request(mediaType);
    MultivaluedMap<String, Object> newHeaders = new MultivaluedHashMap<String, Object>();
    for (Map.Entry<String, List<Object>> entry : request.getHeaders().entrySet()) {
        if (HttpHeaders.AUTHORIZATION.equals(entry.getKey())) {
            continue;
        }
        newHeaders.put(entry.getKey(), entry.getValue());
    }
    newHeaders.add(HttpHeaders.AUTHORIZATION, newAuthorizationHeader);
    builder.headers(newHeaders);
    builder.property(REQUEST_PROPERTY_FILTER_REUSED, "true");
    Invocation invocation;
    if (request.getEntity() == null) {
        invocation = builder.build(method);
    } else {
        invocation = builder.build(method, Entity.entity(request.getEntity(), request.getMediaType()));
    }
    Response nextResponse = invocation.invoke();
    if (nextResponse.hasEntity()) {
        response.setEntityStream(nextResponse.readEntity(InputStream.class));
    }
    MultivaluedMap<String, String> headers = response.getHeaders();
    headers.clear();
    headers.putAll(nextResponse.getStringHeaders());
    response.setStatus(nextResponse.getStatus());
    return response.getStatus() != Response.Status.UNAUTHORIZED.getStatusCode();
}
Also used : Invocation(javax.ws.rs.client.Invocation) InputStream(java.io.InputStream) URI(java.net.URI) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Response(javax.ws.rs.core.Response) MediaType(javax.ws.rs.core.MediaType) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 9 with MediaType

use of javax.ws.rs.core.MediaType in project jersey by jersey.

the class OutboundMessageContext method getAcceptableMediaTypes.

/**
     * Get a list of media types that are acceptable for the message.
     *
     * @return a read-only list of requested message media types sorted according
     * to their q-value, with highest preference first.
     */
@SuppressWarnings("unchecked")
public List<MediaType> getAcceptableMediaTypes() {
    final List<Object> values = headers.get(HttpHeaders.ACCEPT);
    if (values == null || values.isEmpty()) {
        return WILDCARD_ACCEPTABLE_TYPE_SINGLETON_LIST;
    }
    final List<MediaType> result = new ArrayList<>(values.size());
    final RuntimeDelegate rd = RuntimeDelegate.getInstance();
    boolean conversionApplied = false;
    for (final Object value : values) {
        try {
            if (value instanceof MediaType) {
                final AcceptableMediaType _value = AcceptableMediaType.valueOf((MediaType) value);
                // true if value was not an instance of AcceptableMediaType already
                conversionApplied = _value != value;
                result.add(_value);
            } else {
                conversionApplied = true;
                result.addAll(HttpHeaderReader.readAcceptMediaType(HeaderUtils.asString(value, rd)));
            }
        } catch (java.text.ParseException e) {
            throw exception(HttpHeaders.ACCEPT, value, e);
        }
    }
    if (conversionApplied) {
        // cache converted
        headers.put(HttpHeaders.ACCEPT, result.stream().map((Function<MediaType, Object>) mediaType -> mediaType).collect(Collectors.toList()));
    }
    return Collections.unmodifiableList(result);
}
Also used : CommonProperties(org.glassfish.jersey.CommonProperties) LocalizationMessages(org.glassfish.jersey.internal.LocalizationMessages) Date(java.util.Date) Configuration(javax.ws.rs.core.Configuration) HashMap(java.util.HashMap) NewCookie(javax.ws.rs.core.NewCookie) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) MediaType(javax.ws.rs.core.MediaType) RuntimeDelegate(javax.ws.rs.ext.RuntimeDelegate) Locale(java.util.Locale) Map(java.util.Map) URI(java.net.URI) ParseException(java.text.ParseException) OutputStream(java.io.OutputStream) GenericEntity(javax.ws.rs.core.GenericEntity) Set(java.util.Set) IOException(java.io.IOException) Logger(java.util.logging.Logger) EntityTag(javax.ws.rs.core.EntityTag) Collectors(java.util.stream.Collectors) ReflectionHelper(org.glassfish.jersey.internal.util.ReflectionHelper) Cookie(javax.ws.rs.core.Cookie) GenericType(javax.ws.rs.core.GenericType) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) ProcessingException(javax.ws.rs.ProcessingException) Collections(java.util.Collections) Link(javax.ws.rs.core.Link) ArrayList(java.util.ArrayList) MediaType(javax.ws.rs.core.MediaType) ParseException(java.text.ParseException) RuntimeDelegate(javax.ws.rs.ext.RuntimeDelegate)

Example 10 with MediaType

use of javax.ws.rs.core.MediaType in project jersey by jersey.

the class MessageBodyFactory method getMessageBodyWriterMediaTypes.

@Override
@SuppressWarnings("unchecked")
public List<MediaType> getMessageBodyWriterMediaTypes(final Class<?> c, final Type t, final Annotation[] as) {
    final Set<MediaType> writeableMediaTypes = new LinkedHashSet<>();
    for (final WriterModel model : writers) {
        boolean writeableWorker = false;
        for (final MediaType mt : model.declaredTypes()) {
            if (model.isWriteable(c, t, as, mt)) {
                writeableMediaTypes.add(mt);
                writeableWorker = true;
            }
            if (!writeableMediaTypes.contains(MediaType.WILDCARD_TYPE) && writeableWorker && model.declaredTypes().contains(MediaType.WILDCARD_TYPE)) {
                writeableMediaTypes.add(MediaType.WILDCARD_TYPE);
            }
        }
    }
    final List<MediaType> mtl = new ArrayList<>(writeableMediaTypes);
    Collections.sort(mtl, MediaTypes.PARTIAL_ORDER_COMPARATOR);
    return mtl;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WriterModel(org.glassfish.jersey.message.WriterModel) ArrayList(java.util.ArrayList) MediaType(javax.ws.rs.core.MediaType)

Aggregations

MediaType (javax.ws.rs.core.MediaType)228 Test (org.junit.Test)112 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)29 Path (javax.ws.rs.Path)25 Produces (javax.ws.rs.Produces)24 WebApplicationException (javax.ws.rs.WebApplicationException)24 ByteArrayInputStream (java.io.ByteArrayInputStream)20 HashSet (java.util.HashSet)20 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)20 IOException (java.io.IOException)18 AcceptableMediaType (org.glassfish.jersey.message.internal.AcceptableMediaType)18 InputStream (java.io.InputStream)16 GET (javax.ws.rs.GET)16 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)16 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)15 ArrayList (java.util.ArrayList)14 Consumes (javax.ws.rs.Consumes)14 HashMap (java.util.HashMap)13 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)12 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)12