Search code examples
javaalfrescoalfresco-webscripts

How to redirect an Alfresco webscript to a different response page


I've defined an Alfresco Repo Webscript, implemented in Java, which copies a template folder structure into a site's document repository.

The Freemarker HTML template for this webscript is trivial

<html>
    <body>
        <p>Your request was successful</p>
    </body>
</html>

But I don't actually want this to be displayed to the user. I want the response page to be the site's document repository.

So in my Java webscript code, I added this line

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    ...     
    status.setLocation("http://localhost:8081/share/page/site/my-site/documentlibrary");
    ...
}

which I'd hoped would redirect the response to the url, but the page above is still being rendered.

Looking at the code for DeclarativeWebScript (which my implementation class inherits from), I see this

String location = status.getLocation();
if (location != null && location.length() > 0)
{
  if (logger.isDebugEnabled())
  logger.debug("Setting location to " + location);
  res.setHeader(WebScriptResponse.HEADER_LOCATION, location);
}

What am I missing to get the redirect to work?


Solution

  • Promoting a comment to an answer - you need to set a status redirect code too. This is covered in this somewhat hard to find Alfresco document on Java WebScripts and error handling

    You should add something like:

      status.setCode(Status.STATUS_MOVED_PERMANENTLY);
    

    See also the Status object javadocs and DeclarativeWebScript