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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations