}
}
final Response resourceResponse = remoteRequest==null ? null : client.handle(remoteRequest);
if (resourceResponse==null || resourceResponse.getStatus().isSuccess() && resourceResponse.isEntityAvailable()) {
getResponse().setStatus(Status.SUCCESS_OK);
Representation rep = new OutputRepresentation(type) {
public void write(OutputStream os)
throws IOException
{
try {
xform.clearParameters();
final Identity identity = (Identity)getRequest().getAttributes().get(Identity.IDENTITY_ATTR);
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",getRequest().getResourceRef().getPath());
xform.setParameter("request.resource.authority",getRequest().getResourceRef().getAuthority());
xform.setParameter("request.resource.scheme",getRequest().getResourceRef().getScheme());
xform.setParameter("request.resource.query",getRequest().getResourceRef().getQuery());
xform.setParameter("request.resource.fragment",getRequest().getResourceRef().getFragment());
Map<String,Object> attrs = getRequest().getAttributes();
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 {
if (resourceResponse==null) {
input = resource==null ? new StringReader("<doc/>") : new InputStreamReader(resource.toURL().openStream());
} else {
input = resourceResponse.getEntity().getReader();
}
try {
xform.transform(new StreamSource(input,resource==null ? null : 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 {
if (resourceResponse!=null) {
resourceResponse.getEntity().release();
}
}
}
public void release() {
scriptCache.release(script, xform);
}
};
rep.setCharacterSet(CharacterSet.UTF_8);
return rep;
} else {
scriptCache.release(script, xform);
getResponse().setStatus(resourceResponse.getStatus());
getResponse().setEntity(resourceResponse.getEntity());