blob: 76629fff5b948d4dcc3e531e01d29c28803736bf [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Apache Wink : 5.9 Assets</title>
<link rel="stylesheet" href="styles/site.css" type="text/css" />
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
<tr>
<td valign="top" class="pagebody">
<div class="pageheader">
<span class="pagetitle">
Apache Wink : 5.9 Assets
</span>
</div>
<div class="pagesubheading">
This page last changed on Oct 13, 2009 by <font color="#0050B2">michael</font>.
</div>
<h1><a name="5.9Assets-Assets"></a>Assets</h1>
<p>An Asset is a special entity that is returned by a resource method or is injected into a resource method as an entity parameter. The asset is used for retrieving the actual request entity or response entity.<br/>
The purpose of an asset is to act as a container of an entity data model while providing the transformation methods of the data model into data models of other representations.<br/>
Asset classes are POJOs, annotated with the @Asset annotation, that have any number of entity methods.</p>
<p>When an asset instance is returned from a resource method or is set as the entity on a Response instance, it is used by the Apache Wink runtime to retrieve the actual response entity by invoking the appropriate entity-producing method of the asset.</p>
<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>Reference</b><br />For more information on Entity-Producing Methods refer to section <a href="#5.9Assets-EntityProducingMethods">Entity Producing Methods</a>.</td></tr></table></div>
<p>When an asset is the entity parameter of a resource method, it is used by the Apache Wink runtime to set the actual request entity by invoking the appropriate entity-consuming method of the asset.</p>
<h2><a name="5.9Assets-AssetsOverview"></a>Assets Overview</h2>
<p>A typical application exposes each resource in a number of representations. Some form of data model usually backs the resource, and the application business logic relies on the manipulation of that data model. The application will most likely expose resource methods allowing the consumption of the data model in more than one representation (for example Atom and XML) and the production of the data model in other representation (for example Atom, XML and JSON).</p>
<p>According to the JAX-RS specification, the optimal method for implementing a resource is one that consumes and produces an application data model and makes use of a different provider for every media type.</p>
<p>For example, if a resource implements methods that consume and produce a "Defect" bean, then a provider must be implemented for each representation of the "Defect" (Atom, XML and JSON). However, there are times that the transformation of the application data model into a representation requires information that may only be available to the resource but is unavailable to a provider (for example, a connection to the Database).</p>
<p>There are several solutions for dealing with the problem of a provider not having sufficient information to perform application data transformations. The following is a description of two possible solutions:</p>
<ul>
<li>Passing the information as members on the resource and accessing the resource from the provider via the UriInfo context.</li>
</ul>
<p>This solution is only plausible if the resource scope is "per request" and does not work if the resource is a singleton.</p>
<ul>
<li>Passing the information from the resource to the provider via the attributes of the HttpServletRequest.</li>
</ul>
<p>This solution is only plausible when the application is deployed in a JEE container and is not the optimal solution.<br/>
In addition to the previously mentioned problem, the creation of a provider for every data model per media type may result in the inflation of providers in the system, causing the provider selection algorithm to consider a large set of potential providers.</p>
<p>As a result, the selection of the actual provider from the set of potential providers is non-deterministic, because the selection between them is undefined.</p>
<p><div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="images/icons/emoticons/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>Performance Degradation</b><br />An additional side effect of provider inflation is performance degradation.</td></tr></table></div>The use of an asset solves the problem of passing information between a resource and a provider and reduces the amount of registered providers in the system.</p>
<h2><a name="5.9Assets-Lifecycle"></a>Lifecycle</h2>
<p>Resource methods can use an asset as a response entity and as a request entity. The Apache Wink runtime applies different lifecycles for each case.</p>
<h3><a name="5.9Assets-ResponseEntityAsset"></a>Response Entity Asset</h3>
<p>The lifecycle of an asset as a response entity is as follows:</p>
<ol>
<li>The application creates and returns the asset from the resource method.</li>
<li>The appropriate entity-producing method is invoked by the Apache Wink runtime to retrieve the actual response entity.</li>
<li>The appropriate message body writer as obtained from the Providers#getMessageBodyWriter() method serializes the entity obtained at the previous step.</li>
<li>The asset is made available for garbage collection.</li>
</ol>
<h3><a name="5.9Assets-RequestEntityAsset"></a>Request Entity Asset</h3>
<p>The lifecycle of an asset as a request entity is as follows:</p>
<ol>
<li>An asset class is instantiated by the Apache Wink runtime by invoking the asset default constructor. Note that this implies that the asset class must have a public default constructor.</li>
<li>The appropriate message body reader as obtained from the Providers#getMessageBodyReader() method is invoked by the Apache Wink runtime to read the request entity.</li>
<li>The appropriate entity-consuming method is invoked on the asset to populate the asset with the request entity.</li>
<li>The asset is injected into the resource method as the entity parameter.</li>
<li>The asset is made available for garbage collection after returning from the resource method.</li>
</ol>
<h3><a name="5.9Assets-AssetEntityMethods"></a>Asset Entity Methods</h3>
<p>Asset Entity methods are the public methods of an asset annotated with either @Consumes or @Produces annotation. Annotating a method with both @Consumes and @Produces annotations is not supported and may result in unexpected behavior.</p>
<h3><a name="5.9Assets-EntityProducingMethods"></a>Entity Producing Methods</h3>
<p>An Entity Producing Method is a public asset method annotated with the @Produces annotation, designating it to produce the actual response entity. Such methods produce an entity only for the media types declared in the @Produces annotation. Note that under this definition, wildcard ("<b>/</b>") is allowed.<br/>
The Apache Wink runtime will not invoke an entity-producing method whose effective value of @Produces does not match the request Accept header</p>
<h3><a name="5.9Assets-EntityConsumingMethods"></a>Entity Consuming Methods</h3>
<p>An Entity Consuming Method is a public asset method annotated with the @Consumes annotation, designating it to consume the actual request entity for populating the asset. Such methods consume an entity only for the media types declared in the @Consumes annotation. Note that under this definition, wildcard ("<b>/</b>") is allowed.<br/>
The Apache Wink runtime will not invoke an entity-consuming method whose effective value of @Consumes does not match the request Content-Type header.</p>
<h2><a name="5.9Assets-Parameters"></a>Parameters</h2>
<p>Asset Entity methods support the same parameter types as JAX-RS specifies for a resource method.</p>
<h2><a name="5.9Assets-ReturnType"></a>Return Type</h2>
<p>Entity methods may return any type that is permissible to return from a resource method.</p>
<h2><a name="5.9Assets-Exceptions"></a>Exceptions</h2>
<p>Exceptions thrown from an entity method are treated as exceptions thrown from a resource method.</p>
<h2><a name="5.9Assets-AnnotationInheritance"></a>Annotation Inheritance</h2>
<p>The @Produces and @Consumes annotations are not inherited when an asset sub-class overrides an asset entity method. Asset sub-classes must re-declare the @Produces and @Consumes annotations for the overriding method to be an entity method.</p>
<h2><a name="5.9Assets-EntityMethodMatching"></a>Entity Method Matching</h2>
<p>Asset classes are handled by the AssetProvider which is a JAX-RS provider that is capable of consuming and producing all media types.</p>
<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>Reference</b><br />For more information on Asset Providers refer to section 7.7 Assets Provider.</td></tr></table></div>
<h3><a name="5.9Assets-RequestEntityMatching"></a>Request Entity Matching</h3>
<p>The following points describe the process of selecting the asset entity-consuming method to handle the request entity. This process occurs during the invocation of the <b>AssetProvider#isReadable()</b> method.</p>
<ol>
<li>Collect all the entity-consuming methods of the asset. These are the public methods annotated with @Consumes annotation.</li>
<li>Sort the collected entity-consuming methods in descending order, where methods with more specific media types precede methods with less specific media types, following the rule n/m &gt; n/&#42; &gt; <b>/</b>.</li>
<li>Select the first method that supports the media type of the request entity body as provided to the AssetProvider#isReadable() method, and return true.</li>
<li>If no entity-consuming method supports the media type of the request entity body, return false. The Apache Wink runtime continues searching for a different provider to handle the asset as a regular entity.</li>
</ol>
<h3><a name="5.9Assets-ResponseEntityMatching"></a>Response Entity Matching</h3>
<p>The following points describe the process of selecting an entity-producing method to produce the actual response entity. The following process occurs during the invocation of the <b>AssetProvider#isWriteable()</b> method.</p>
<ol>
<li>Collect all the entity-producing methods of the asset. These are the public methods annotated with @Produces annotation.</li>
<li>Sort the collected entity-producing methods in descending order, where methods with more specific media types precede methods with less specific media types, following the rule n/m &gt; n/&#42; &gt; <b>/</b>.</li>
<li>Select the first method that supports the media type of the response entity body as provided to the AssetProvider#isWriteable()method and return true.</li>
<li>&nbsp;If no entity-producing method supports the media type of the response entity body, return false. The Apache Wink runtime continues searching for a different provider to handle the asset as a regular entity.</li>
</ol>
<h2><a name="5.9Assets-AssetExample"></a>Asset Example</h2>
<p>The following example illustrates the use of an asset. The "<b>Defect</b>" bean is a JAXB annotated class.</p>
<h3><a name="5.9Assets-DefectAssetClass"></a>DefectAsset Class</h3>
<p>The DefectAsset class is the asset backed by an instance of a "Defect" bean.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">@Asset
public class DefectAsset {
public Defect defect;
public DefectAsset(Defect defect) {
this.defect = defect;
}
@Produces(<span class="code-quote">"application/xml"</span>)
public Defect getDefect() {
return this.defect;
}
@Produces(<span class="code-quote">"text/html"</span>)
public String getDefectAsHtml() {
String html = ...;
return html;
}
@Produces(<span class="code-quote">"application/atom+xml"</span>)
public AtomEntry getDefectAsAtom() {
AtomEntry entry = ...;
return entry;
}
@Consumes(<span class="code-quote">"application/xml"</span>)
public void setDefect(Defect defect) {
this.defect = defect;
}
}
</pre>
</div></div>
<h3><a name="5.9Assets-DefectResourceClass"></a>DefectResource Class</h3>
<p>&nbsp;The DefectResource class is a resource that is anchored to the URI path "<b>defects/{id}</b>" within the Apache Wink runtime.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">@Path(<span class="code-quote">"defects/{id}"</span>)
public class DefectResource {
@GET
public DefectAsset getDefect(@PathParam(<span class="code-quote">"id"</span>) String id) {
return new DefectAsset(defects.get(id));
}
@PUT
public DefectAsset updateDefect(DefectAsset defectAsset,
@PathParam(<span class="code-quote">"id"</span>) String id) {
defects.put(id, defectAsset.getDefect());
return defectAsset;
}
}
</pre>
</div></div>
<h4><a name="5.9Assets-ScenarioExplanation1"></a>Scenario Explanation 1</h4>
<ol>
<li>A client issues an HTTP GET request with a URI="/defects/1" and Accept Header= "application/xm</li>
<li>The Apache Wink runtime analyzes the request and invokes the DefectResource#getDefect() resource method.</li>
<li>The DefectResource#getDefect() resource method creates an instance of DefectAsset and populates it with defect "1" data</li>
<li>The DefectResource#getDefect() resource method returns the DefectAsset instance back to Apache Wink runtim</li>
<li>The Apache Wink runtime analyzes the asset and invokes the DefectAsset#getDefect() entity-producing method to obtain the reference to the "Defect" bean.</li>
<li>The "Defect" bean is serialized by Apache Wink runtime as an XML using the appropriate provider.</li>
</ol>
<h4><a name="5.9Assets-ScenarioExplanation2"></a>Scenario Explanation 2</h4>
<ol>
<li>A Client issues an HTTP GET request with a URI="/defects/1" and Accept Header= "text/html"</li>
<li>The Apache Wink runtime analyzes the request and invokes the DefectResource#getDefect() resource metho</li>
<li>The DefectResource#getDefect() resource method creates an instance of DefectAsset and populates it with defect "1" data.</li>
<li>The DefectResource#getDefect() method returns the populated asset back to the Apache Wink runtime.</li>
<li>The Apache Wink runtime analyzes the asset and invokes the DefectAsset#getDefectAsHtml() entity-producing method in order to obtain the reference to the "Defect" bean.</li>
<li>The "Defect" is serialized by Apache Wink runtime as an HTML using the appropriate provider.</li>
</ol>
<h4><a name="5.9Assets-ScenarioExplanation3"></a>Scenario Explanation 3</h4>
<ol>
<li>A Client issues an HTTP PUT request with a URI="/defects/1" and Accept Header= "text/html"</li>
<li>The Apache Wink runtime analyzes the request and invokes the DefectResource#updateDefect() method with an instance of DefectAsset populated with the request entity.&#42; A DefectAsset is instantiated by the Apache Wink runtime</li>
<li>The DefectAsset#setDefect() entity-consuming method is invoked in order to populate the DefectAsset with the defect data.</li>
</ol>
</td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td height="12" background="http://cwiki.apache.org/confluence/images/border/border_bottom.gif"><img src="images/border/spacer.gif" width="1" height="1" border="0"/></td>
</tr>
<tr>
<td align="center"><font color="grey">Document generated by Confluence on Nov 11, 2009 06:57</font></td>
</tr>
</table>
</body>
</html>