Search in sources :

Example 1 with RuntimeDelegate

use of javax.ws.rs.ext.RuntimeDelegate 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 2 with RuntimeDelegate

use of javax.ws.rs.ext.RuntimeDelegate in project jersey by jersey.

the class OutboundMessageContext method getAcceptableLanguages.

/**
     * Get a list of languages that are acceptable for the message.
     *
     * @return a read-only list of acceptable languages sorted according
     * to their q-value, with highest preference first.
     */
public List<Locale> getAcceptableLanguages() {
    final List<Object> values = headers.get(HttpHeaders.ACCEPT_LANGUAGE);
    if (values == null || values.isEmpty()) {
        return Collections.singletonList(new AcceptableLanguageTag("*", null).getAsLocale());
    }
    final List<Locale> result = new ArrayList<Locale>(values.size());
    final RuntimeDelegate rd = RuntimeDelegate.getInstance();
    boolean conversionApplied = false;
    for (final Object value : values) {
        if (value instanceof Locale) {
            result.add((Locale) value);
        } else {
            conversionApplied = true;
            try {
                result.addAll(HttpHeaderReader.readAcceptLanguage(HeaderUtils.asString(value, rd)).stream().map(LanguageTag::getAsLocale).collect(Collectors.toList()));
            } catch (java.text.ParseException e) {
                throw exception(HttpHeaders.ACCEPT_LANGUAGE, value, e);
            }
        }
    }
    if (conversionApplied) {
        // cache converted
        headers.put(HttpHeaders.ACCEPT_LANGUAGE, result.stream().map((Function<Locale, Object>) locale -> locale).collect(Collectors.toList()));
    }
    return Collections.unmodifiableList(result);
}
Also used : Locale(java.util.Locale) 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) ParseException(java.text.ParseException) RuntimeDelegate(javax.ws.rs.ext.RuntimeDelegate)

Example 3 with RuntimeDelegate

use of javax.ws.rs.ext.RuntimeDelegate in project jersey by jersey.

the class HeaderUtils method checkHeaderChanges.

/**
     * Compares two snapshots of headers from jersey {@code ClientRequest} and logs {@code WARNING} in case of difference.
     *
     * Current container implementations does not support header modification in {@link javax.ws.rs.ext.WriterInterceptor}
     * and {@link javax.ws.rs.ext.MessageBodyWriter}. The method checks there are some newly added headers
     * (probably by WI or MBW) and logs {@code WARNING} message about it.
     *
     * @param headersSnapshot first immutable snapshot of headers
     * @param currentHeaders  current instance of headers tobe compared to
     * @param connectorName   name of connector the method is invoked from, used just in logged message
     * @see <a href="https://java.net/jira/browse/JERSEY-2341">JERSEY-2341</a>
     */
public static void checkHeaderChanges(final Map<String, String> headersSnapshot, final MultivaluedMap<String, Object> currentHeaders, final String connectorName) {
    if (HeaderUtils.LOGGER.isLoggable(Level.WARNING)) {
        final RuntimeDelegate rd = RuntimeDelegate.getInstance();
        final Set<String> changedHeaderNames = new HashSet<String>();
        for (final Map.Entry<? extends String, ? extends List<Object>> entry : currentHeaders.entrySet()) {
            if (!headersSnapshot.containsKey(entry.getKey())) {
                changedHeaderNames.add(entry.getKey());
            } else {
                final String prevValue = headersSnapshot.get(entry.getKey());
                final String newValue = asHeaderString(currentHeaders.get(entry.getKey()), rd);
                if (!prevValue.equals(newValue)) {
                    changedHeaderNames.add(entry.getKey());
                }
            }
        }
        if (!changedHeaderNames.isEmpty()) {
            if (HeaderUtils.LOGGER.isLoggable(Level.WARNING)) {
                HeaderUtils.LOGGER.warning(LocalizationMessages.SOME_HEADERS_NOT_SENT(connectorName, changedHeaderNames.toString()));
            }
        }
    }
}
Also used : MultivaluedMap(javax.ws.rs.core.MultivaluedMap) StringKeyIgnoreCaseMultivaluedMap(org.glassfish.jersey.internal.util.collection.StringKeyIgnoreCaseMultivaluedMap) ImmutableMultivaluedMap(org.glassfish.jersey.internal.util.collection.ImmutableMultivaluedMap) Map(java.util.Map) AbstractMultivaluedMap(javax.ws.rs.core.AbstractMultivaluedMap) RuntimeDelegate(javax.ws.rs.ext.RuntimeDelegate) HashSet(java.util.HashSet)

Example 4 with RuntimeDelegate

use of javax.ws.rs.ext.RuntimeDelegate in project tesb-rt-se by Talend.

the class ApplicationServer method startApplication.

private static Server startApplication(Application app) {
    RuntimeDelegate delegate = RuntimeDelegate.getInstance();
    JAXRSServerFactoryBean bean = delegate.createEndpoint(app, JAXRSServerFactoryBean.class);
    bean.setAddress("http://localhost:8080" + bean.getAddress());
    bean.setStart(false);
    Server server = bean.create();
    JettyHTTPDestination dest = (JettyHTTPDestination) server.getDestination();
    JettyHTTPServerEngine engine = (JettyHTTPServerEngine) dest.getEngine();
    engine.setSessionSupport(true);
    server.start();
    return server;
}
Also used : Server(org.apache.cxf.endpoint.Server) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) JettyHTTPServerEngine(org.apache.cxf.transport.http_jetty.JettyHTTPServerEngine) JettyHTTPDestination(org.apache.cxf.transport.http_jetty.JettyHTTPDestination) RuntimeDelegate(javax.ws.rs.ext.RuntimeDelegate)

Example 5 with RuntimeDelegate

use of javax.ws.rs.ext.RuntimeDelegate in project jbpm by kiegroup.

the class RestWorkItemHandlerTest method initialize.

@SuppressWarnings({ "rawtypes" })
@BeforeClass
public static void initialize() throws Exception {
    SimpleRESTApplication application = new SimpleRESTApplication();
    RuntimeDelegate delegate = RuntimeDelegate.getInstance();
    JAXRSServerFactoryBean bean = delegate.createEndpoint(application, JAXRSServerFactoryBean.class);
    bean.setProvider(new JAXBElementProvider());
    bean.setAddress("http://localhost:9998" + bean.getAddress());
    server = bean.create();
    server.start();
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) RuntimeDelegate(javax.ws.rs.ext.RuntimeDelegate) BeforeClass(org.junit.BeforeClass)

Aggregations

RuntimeDelegate (javax.ws.rs.ext.RuntimeDelegate)14 HashSet (java.util.HashSet)4 Map (java.util.Map)4 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)4 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)4 BeforeClass (org.junit.BeforeClass)4 IOException (java.io.IOException)3 OutputStream (java.io.OutputStream)3 Annotation (java.lang.annotation.Annotation)3 Type (java.lang.reflect.Type)3 URI (java.net.URI)3 ParseException (java.text.ParseException)3 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Locale (java.util.Locale)3 Set (java.util.Set)3 Function (java.util.function.Function)3