final Representation feedResource = remoteResponse.getEntity();
response.setStatus(Status.SUCCESS_OK);
final Identity identity = (Identity)request.getAttributes().get(Identity.IDENTITY_ATTR);
final Map<String,Object> attrs = request.getAttributes();
releaseScript = null;
Representation rep = new OutputRepresentation(script.getMediaType()) {
public void write(OutputStream os)
throws IOException
{
try {
Transformer xform = script.getTransformer();
xform.clearParameters();
if (identity!=null) {
xform.setParameter("user.session",identity.getSession());
xform.setParameter("user.id",identity.getId());
xform.setParameter("user.alias",identity.getAlias());
if (identity.getName()!=null) {
xform.setParameter("user.name",identity.getName());
}
if (identity.getEmail()!=null) {
xform.setParameter("user.email",identity.getEmail());
}
}
xform.setParameter("request.resource.path",request.getResourceRef().getPath());
xform.setParameter("request.resource.authority",request.getResourceRef().getAuthority());
xform.setParameter("request.resource.scheme",request.getResourceRef().getScheme());
xform.setParameter("request.resource.query",request.getResourceRef().getQuery());
xform.setParameter("request.resource.fragment",request.getResourceRef().getFragment());
for (String name : attrs.keySet()) {
Object value = attrs.get(name);
if (value instanceof String) {
xform.setParameter(name,value);
}
}
for (Parameter param : getContext().getParameters()) {
xform.setParameter(param.getName(),param.getValue());
}
xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
Reader input = null;
try {
input = feedResource.getReader();
try {
xform.transform(new StreamSource(input,resource.toString()),new StreamResult(new OutputStreamWriter(os,"UTF-8")));
} catch (TransformerException ex) {
getLogger().log(Level.SEVERE,"Cannot transform feed due to script error.",ex);
}
} finally {
if (input!=null) {
input.close();
}
}
} finally {
feedResource.release();
}
}
public void release() {
script.release();
}
};
rep.setCharacterSet(CharacterSet.UTF_8);
response.setEntity(rep);
response.setStatus(Status.SUCCESS_OK);
} else if (remoteResponse!=null) {
if (remoteResponse.isEntityAvailable()) {
remoteResponse.getEntity().release();