Search in sources :

Example 1 with Encoded

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

the class Parameter method create.

/**
     * Create a parameter model.
     *
     * @param concreteClass   concrete resource method handler implementation class.
     * @param declaringClass  declaring class of the method the parameter belongs to or field that this parameter represents.
     * @param encodeByDefault flag indicating whether the parameter should be encoded by default or not. Note that a presence
     *                        of {@link Encoded} annotation in the list of the parameter {@code annotations} will override any
     *                        value set in the flag to {@code true}.
     * @param rawType         raw Java parameter type.
     * @param type            generic Java parameter type.
     * @param annotations     parameter annotations.
     * @return new parameter model.
     */
@SuppressWarnings("unchecked")
public static Parameter create(Class concreteClass, Class declaringClass, boolean encodeByDefault, Class<?> rawType, Type type, Annotation[] annotations) {
    if (null == annotations) {
        return null;
    }
    Annotation paramAnnotation = null;
    Parameter.Source paramSource = null;
    String paramName = null;
    boolean paramEncoded = encodeByDefault;
    String paramDefault = null;
    /**
         * Create a parameter from the list of annotations. Unknown annotated
         * parameters are also supported, and in such a cases the last
         * unrecognized annotation is taken to be that associated with the
         * parameter.
         */
    for (Annotation annotation : annotations) {
        if (ANNOTATION_HELPER_MAP.containsKey(annotation.annotationType())) {
            ParamAnnotationHelper helper = ANNOTATION_HELPER_MAP.get(annotation.annotationType());
            paramAnnotation = annotation;
            paramSource = helper.getSource();
            paramName = helper.getValueOf(annotation);
        } else if (Encoded.class == annotation.annotationType()) {
            paramEncoded = true;
        } else if (DefaultValue.class == annotation.annotationType()) {
            paramDefault = ((DefaultValue) annotation).value();
        } else {
            // Take latest unknown annotation, but don't override known annotation
            if ((paramAnnotation == null) || (paramSource == Source.UNKNOWN)) {
                paramAnnotation = annotation;
                paramSource = Source.UNKNOWN;
                paramName = getValue(annotation);
            }
        }
    }
    if (paramAnnotation == null) {
        paramSource = Parameter.Source.ENTITY;
    }
    ClassTypePair ct = ReflectionHelper.resolveGenericType(concreteClass, declaringClass, rawType, type);
    if (paramSource == Source.BEAN_PARAM) {
        return new BeanParameter(annotations, paramAnnotation, paramName, ct.rawClass(), ct.type(), paramEncoded, paramDefault);
    } else {
        return new Parameter(annotations, paramAnnotation, paramSource, paramName, ct.rawClass(), ct.type(), paramEncoded, paramDefault);
    }
}
Also used : Encoded(javax.ws.rs.Encoded) ClassTypePair(org.glassfish.jersey.internal.util.collection.ClassTypePair) Annotation(java.lang.annotation.Annotation)

Example 2 with Encoded

use of javax.ws.rs.Encoded in project pulsar by yahoo.

the class DestinationLookup method lookupDestinationAsync.

@GET
@Path("persistent/{property}/{cluster}/{namespace}/{dest}")
@Produces(MediaType.APPLICATION_JSON)
public void lookupDestinationAsync(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace, @PathParam("dest") @Encoded String dest, @QueryParam("authoritative") @DefaultValue("false") boolean authoritative, @Suspended AsyncResponse asyncResponse) {
    dest = Codec.decode(dest);
    DestinationName topic = DestinationName.get("persistent", property, cluster, namespace, dest);
    if (!pulsar().getBrokerService().getLookupRequestSemaphore().tryAcquire()) {
        log.warn("No broker was found available for topic {}", topic);
        asyncResponse.resume(new WebApplicationException(Response.Status.SERVICE_UNAVAILABLE));
        return;
    }
    try {
        validateClusterOwnership(topic.getCluster());
        checkConnect(topic);
        validateReplicationSettingsOnNamespace(pulsar(), topic.getNamespaceObject());
    } catch (WebApplicationException we) {
        // Validation checks failed
        log.error("Validation check failed: {}", we.getMessage());
        completeLookupResponseExceptionally(asyncResponse, we);
        return;
    } catch (Throwable t) {
        // Validation checks failed with unknown error
        log.error("Validation check failed: {}", t.getMessage(), t);
        completeLookupResponseExceptionally(asyncResponse, new RestException(t));
        return;
    }
    CompletableFuture<LookupResult> lookupFuture = pulsar().getNamespaceService().getBrokerServiceUrlAsync(topic, authoritative);
    lookupFuture.thenAccept(result -> {
        if (result == null) {
            log.warn("No broker was found available for topic {}", topic);
            completeLookupResponseExceptionally(asyncResponse, new WebApplicationException(Response.Status.SERVICE_UNAVAILABLE));
            return;
        }
        if (result.isRedirect()) {
            boolean newAuthoritative = this.isLeaderBroker();
            URI redirect;
            try {
                String redirectUrl = isRequestHttps() ? result.getLookupData().getHttpUrlTls() : result.getLookupData().getHttpUrl();
                redirect = new URI(String.format("%s%s%s?authoritative=%s", redirectUrl, "/lookup/v2/destination/", topic.getLookupName(), newAuthoritative));
            } catch (URISyntaxException e) {
                log.error("Error in preparing redirect url for {}: {}", topic, e.getMessage(), e);
                completeLookupResponseExceptionally(asyncResponse, e);
                return;
            }
            if (log.isDebugEnabled()) {
                log.debug("Redirect lookup for topic {} to {}", topic, redirect);
            }
            completeLookupResponseExceptionally(asyncResponse, new WebApplicationException(Response.temporaryRedirect(redirect).build()));
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Lookup succeeded for topic {} -- broker: {}", topic, result.getLookupData());
            }
            completeLookupResponseSuccessfully(asyncResponse, result.getLookupData());
        }
    }).exceptionally(exception -> {
        log.warn("Failed to lookup broker for topic {}: {}", topic, exception.getMessage(), exception);
        completeLookupResponseExceptionally(asyncResponse, exception);
        return null;
    });
}
Also used : PathParam(javax.ws.rs.PathParam) RestException(com.yahoo.pulsar.broker.web.RestException) ServerError(com.yahoo.pulsar.common.api.proto.PulsarApi.ServerError) Encoded(javax.ws.rs.Encoded) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) URISyntaxException(java.net.URISyntaxException) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) LookupData(com.yahoo.pulsar.common.lookup.data.LookupData) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) ClusterData(com.yahoo.pulsar.common.policies.data.ClusterData) ByteBuf(io.netty.buffer.ByteBuf) DefaultValue(javax.ws.rs.DefaultValue) PulsarService(com.yahoo.pulsar.broker.PulsarService) URI(java.net.URI) Codec(com.yahoo.pulsar.common.util.Codec) LookupType(com.yahoo.pulsar.common.api.proto.PulsarApi.CommandLookupTopicResponse.LookupType) Status(javax.ws.rs.core.Response.Status) DestinationName(com.yahoo.pulsar.common.naming.DestinationName) Logger(org.slf4j.Logger) AsyncResponse(javax.ws.rs.container.AsyncResponse) NoSwaggerDocumentation(com.yahoo.pulsar.broker.web.NoSwaggerDocumentation) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Commands.newLookupResponse(com.yahoo.pulsar.common.api.Commands.newLookupResponse) Suspended(javax.ws.rs.container.Suspended) PulsarWebResource(com.yahoo.pulsar.broker.web.PulsarWebResource) Response(javax.ws.rs.core.Response) WebApplicationException(javax.ws.rs.WebApplicationException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) WebApplicationException(javax.ws.rs.WebApplicationException) DestinationName(com.yahoo.pulsar.common.naming.DestinationName) RestException(com.yahoo.pulsar.broker.web.RestException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

Encoded (javax.ws.rs.Encoded)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 PulsarService (com.yahoo.pulsar.broker.PulsarService)1 NoSwaggerDocumentation (com.yahoo.pulsar.broker.web.NoSwaggerDocumentation)1 PulsarWebResource (com.yahoo.pulsar.broker.web.PulsarWebResource)1 RestException (com.yahoo.pulsar.broker.web.RestException)1 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)1 Commands.newLookupResponse (com.yahoo.pulsar.common.api.Commands.newLookupResponse)1 LookupType (com.yahoo.pulsar.common.api.proto.PulsarApi.CommandLookupTopicResponse.LookupType)1 ServerError (com.yahoo.pulsar.common.api.proto.PulsarApi.ServerError)1 LookupData (com.yahoo.pulsar.common.lookup.data.LookupData)1 DestinationName (com.yahoo.pulsar.common.naming.DestinationName)1 ClusterData (com.yahoo.pulsar.common.policies.data.ClusterData)1 Codec (com.yahoo.pulsar.common.util.Codec)1 ByteBuf (io.netty.buffer.ByteBuf)1 Annotation (java.lang.annotation.Annotation)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 DefaultValue (javax.ws.rs.DefaultValue)1