Search in sources :

Example 31 with ProcessingException

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

the class WadlBuilder method generate.

/**
     * Generate WADL for a resource.
     *
     * @param resource    the resource
     * @param description the overall application description so we can
     * @return the JAXB WADL application bean
     */
public Application generate(ApplicationDescription description, org.glassfish.jersey.server.model.Resource resource) {
    try {
        Application wadlApplication = _wadlGenerator.createApplication();
        Resources wadlResources = _wadlGenerator.createResources();
        Resource wadlResource = generateResource(resource, null);
        if (wadlResource == null) {
            return null;
        }
        wadlResources.getResource().add(wadlResource);
        wadlApplication.getResources().add(wadlResources);
        addVersion(wadlApplication);
        // Attach the data to the parts of the model
        _wadlGenerator.attachTypes(description);
        return wadlApplication;
    } catch (Exception e) {
        throw new ProcessingException(LocalizationMessages.ERROR_WADL_BUILDER_GENERATION_RESOURCE(resource), e);
    }
}
Also used : Resource(com.sun.research.ws.wadl.Resource) Resources(com.sun.research.ws.wadl.Resources) Application(com.sun.research.ws.wadl.Application) ProcessingException(javax.ws.rs.ProcessingException) ProcessingException(javax.ws.rs.ProcessingException)

Example 32 with ProcessingException

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

the class WadlBuilder method generateRequest.

private Request generateRequest(org.glassfish.jersey.server.model.Resource parentResource, final org.glassfish.jersey.server.model.ResourceMethod resourceMethod, Map<String, Param> wadlResourceParams) {
    try {
        final List<Parameter> requestParams = new LinkedList<>(resourceMethod.getInvocable().getParameters());
        // Adding handler instance parameters to the list of potential request parameters.
        requestParams.addAll(resourceMethod.getInvocable().getHandler().getParameters());
        if (requestParams.isEmpty()) {
            return null;
        }
        Request wadlRequest = _wadlGenerator.createRequest(parentResource, resourceMethod);
        processRequestParameters(parentResource, resourceMethod, wadlResourceParams, requestParams, wadlRequest);
        if (wadlRequest.getRepresentation().size() + wadlRequest.getParam().size() == 0) {
            return null;
        } else {
            return wadlRequest;
        }
    } catch (Exception e) {
        throw new ProcessingException(LocalizationMessages.ERROR_WADL_BUILDER_GENERATION_REQUEST(resourceMethod, parentResource), e);
    }
}
Also used : Request(com.sun.research.ws.wadl.Request) Parameter(org.glassfish.jersey.server.model.Parameter) LinkedList(java.util.LinkedList) ProcessingException(javax.ws.rs.ProcessingException) ProcessingException(javax.ws.rs.ProcessingException)

Example 33 with ProcessingException

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

the class WadlBuilder method generateResource.

private Resource generateResource(final org.glassfish.jersey.server.model.Resource resource, String path, Set<org.glassfish.jersey.server.model.Resource> visitedResources) {
    try {
        if (!detailedWadl && resource.isExtended()) {
            return null;
        }
        Resource wadlResource = _wadlGenerator.createResource(resource, path);
        // prevent infinite recursion
        if (visitedResources.contains(resource)) {
            return wadlResource;
        } else {
            visitedResources = new HashSet<>(visitedResources);
            visitedResources.add(resource);
        }
        // if the resource contains subresource locator create new resource for this locator and return it instead
        // of this resource
        final ResourceMethod locator = resource.getResourceLocator();
        if (locator != null) {
            try {
                org.glassfish.jersey.server.model.Resource.Builder builder = org.glassfish.jersey.server.model.Resource.builder(locator.getInvocable().getRawResponseType());
                if (builder == null) {
                    // for example in the case the return type of the sub resource locator is Object
                    builder = org.glassfish.jersey.server.model.Resource.builder().path(resource.getPath());
                }
                org.glassfish.jersey.server.model.Resource subResource = builder.build();
                Resource wadlSubResource = generateResource(subResource, resource.getPath(), visitedResources);
                if (wadlSubResource == null) {
                    return null;
                }
                if (locator.isExtended()) {
                    wadlSubResource.getAny().add(WadlApplicationContextImpl.EXTENDED_ELEMENT);
                }
                for (Parameter param : locator.getInvocable().getParameters()) {
                    Param wadlParam = generateParam(resource, locator, param);
                    if (wadlParam != null && wadlParam.getStyle() == ParamStyle.TEMPLATE) {
                        wadlSubResource.getParam().add(wadlParam);
                    }
                }
                return wadlSubResource;
            } catch (RuntimeException e) {
                throw new ProcessingException(LocalizationMessages.ERROR_WADL_BUILDER_GENERATION_RESOURCE_LOCATOR(locator, resource), e);
            }
        }
        Map<String, Param> wadlResourceParams = new HashMap<>();
        // for each resource method
        for (org.glassfish.jersey.server.model.ResourceMethod method : resource.getResourceMethods()) {
            if (!detailedWadl && method.isExtended()) {
                continue;
            }
            com.sun.research.ws.wadl.Method wadlMethod = generateMethod(resource, wadlResourceParams, method);
            wadlResource.getMethodOrResource().add(wadlMethod);
        }
        // add method parameters that are associated with the resource PATH template
        for (Param wadlParam : wadlResourceParams.values()) {
            wadlResource.getParam().add(wadlParam);
        }
        // for each sub-resource method
        Map<String, Resource> wadlSubResources = new HashMap<>();
        Map<String, Map<String, Param>> wadlSubResourcesParams = new HashMap<>();
        for (org.glassfish.jersey.server.model.Resource childResource : resource.getChildResources()) {
            Resource childWadlResource = generateResource(childResource, childResource.getPath(), visitedResources);
            if (childWadlResource == null) {
                continue;
            }
            wadlResource.getMethodOrResource().add(childWadlResource);
        }
        return wadlResource;
    } catch (Exception e) {
        throw new ProcessingException(LocalizationMessages.ERROR_WADL_BUILDER_GENERATION_RESOURCE_PATH(resource, path), e);
    }
}
Also used : HashMap(java.util.HashMap) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod) ProcessingException(javax.ws.rs.ProcessingException) Resource(com.sun.research.ws.wadl.Resource) ProcessingException(javax.ws.rs.ProcessingException) Param(com.sun.research.ws.wadl.Param) FormParam(javax.ws.rs.FormParam) Parameter(org.glassfish.jersey.server.model.Parameter) HashMap(java.util.HashMap) Map(java.util.Map) ResourceMethod(org.glassfish.jersey.server.model.ResourceMethod)

Example 34 with ProcessingException

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

the class WadlBuilder method setRepresentationForMediaType.

/**
     * Create the wadl {@link Representation} for the specified {@link MediaType} if not yet existing for the wadl {@link Request}
     * and return it.
     *
     * @param r           the resource
     * @param m           the resource method
     * @param mediaType   an accepted media type of the resource method
     * @param wadlRequest the wadl request the wadl representation is to be created for (if not yet existing).
     * @return the wadl request representation for the specified {@link MediaType}.
     */
private Representation setRepresentationForMediaType(org.glassfish.jersey.server.model.Resource r, final org.glassfish.jersey.server.model.ResourceMethod m, MediaType mediaType, Request wadlRequest) {
    try {
        Representation wadlRepresentation = getRepresentationByMediaType(wadlRequest.getRepresentation(), mediaType);
        if (wadlRepresentation == null) {
            wadlRepresentation = _wadlGenerator.createRequestRepresentation(r, m, mediaType);
            wadlRequest.getRepresentation().add(wadlRepresentation);
        }
        return wadlRepresentation;
    } catch (Exception e) {
        throw new ProcessingException(LocalizationMessages.ERROR_WADL_BUILDER_GENERATION_REQUEST_MEDIA_TYPE(mediaType, m, r), e);
    }
}
Also used : Representation(com.sun.research.ws.wadl.Representation) ProcessingException(javax.ws.rs.ProcessingException) ProcessingException(javax.ws.rs.ProcessingException)

Example 35 with ProcessingException

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

the class WadlResource method getWadl.

@Produces({ "application/vnd.sun.wadl+xml", "application/xml" })
@GET
public synchronized Response getWadl(@Context UriInfo uriInfo) {
    try {
        if (!wadlContext.isWadlGenerationEnabled()) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        final boolean detailedWadl = WadlUtils.isDetailedWadlRequested(uriInfo);
        if ((wadlXmlRepresentation == null) || (!isCached(uriInfo, detailedWadl))) {
            this.lastBaseUri = uriInfo.getBaseUri();
            lastDetailedWadl = detailedWadl;
            this.lastModified = new SimpleDateFormat(HTTPDATEFORMAT).format(new Date());
            ApplicationDescription applicationDescription = wadlContext.getApplication(uriInfo, detailedWadl);
            Application application = applicationDescription.getApplication();
            try {
                final Marshaller marshaller = wadlContext.getJAXBContext().createMarshaller();
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                final ByteArrayOutputStream os = new ByteArrayOutputStream();
                marshaller.marshal(application, os);
                wadlXmlRepresentation = os.toByteArray();
                os.close();
            } catch (Exception e) {
                throw new ProcessingException("Could not marshal the wadl Application.", e);
            }
        }
        return Response.ok(new ByteArrayInputStream(wadlXmlRepresentation)).header("Last-modified", lastModified).build();
    } catch (Exception e) {
        throw new ProcessingException("Error generating /application.wadl.", e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SimpleDateFormat(java.text.SimpleDateFormat) Application(com.sun.research.ws.wadl.Application) Date(java.util.Date) ProcessingException(javax.ws.rs.ProcessingException) ProcessingException(javax.ws.rs.ProcessingException) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

ProcessingException (javax.ws.rs.ProcessingException)91 Test (org.junit.Test)32 IOException (java.io.IOException)26 Response (javax.ws.rs.core.Response)20 WebTarget (javax.ws.rs.client.WebTarget)19 JerseyTest (org.glassfish.jersey.test.JerseyTest)16 WebApplicationException (javax.ws.rs.WebApplicationException)11 CountDownLatch (java.util.concurrent.CountDownLatch)9 Client (javax.ws.rs.client.Client)9 ResponseProcessingException (javax.ws.rs.client.ResponseProcessingException)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 ClientRequest (org.glassfish.jersey.client.ClientRequest)8 ClientResponse (org.glassfish.jersey.client.ClientResponse)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 URI (java.net.URI)6 NoContentException (javax.ws.rs.core.NoContentException)6 OutputStream (java.io.OutputStream)5 Map (java.util.Map)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 EventSource (org.glassfish.jersey.media.sse.EventSource)5