hsiaomichiu
hsiaomichiu

Reputation: 602

Set response header in Google App Engine Endpoints (Java)

I'm implementing a RESTful service using GAE Endpoints in Java. I'm trying to implement token-based authentication method and I wish to add a HTTP response header that contains the token string. However, I'm unable to find documentations on how to change the response header inside a @ApiMethod.

Anybody can shed some light on this? Any help very much appreciated.

Upvotes: 4

Views: 1382

Answers (4)

matt burns
matt burns

Reputation: 25370

I was able to do it with cache-control header. For example, if you want to serve an image from a servlet for some reason, then you can set the cache-control response header like this with Jersey:

@GET
@Path("/{url}/{maxHeight}")
@Produces("image/jpeg")
public Response view(
        @Context HttpServletRequest req,
        @PathParam("url") String url,
        @PathParam("maxHeight") int maxHeight
) throws IOException, URISyntaxException {
    ... [code to generate imageData]
    return Response
            .ok(imageData)
            .cacheControl(CacheControl.valueOf("max-age=2592000"))
            .build();
}

```

Upvotes: 0

Claytronicon
Claytronicon

Reputation: 1456

I don't know if it will work for your case, but you can set response headers using UrlRewriteFilter:

<urlrewrite>
    <rule>
        <from>.*</from>
        <set type="response-header" name="X-Frame-Options">DENY</set>
        <set type="response-header" name="Cache-Control">no-cache, no-store, max-age=0, must-revalidate</set>
    </rule>
</urlrewrite>

http://tuckey.org/urlrewrite/

I've set headers successfully on an App Engine project, although I have not tried it with a token string

Upvotes: 1

sratatata
sratatata

Reputation: 55

I've checked there is no way to change response header. More over @Nick has right that there is more layers - on the servlet container there is code which wraps response.

Some layers above - evidence is that error messages are packaged for endpoints. SystemServletService is putting into body this:

{
  "error_message": "Some message"
}

Bu in practice response you get contains:

{
    "error": {
        "message": "Some messaged",
        "code": 401,
        "errors": [
            {
                "domain": "global",
                "reason": "required",
                "message": "Some message"
            }
        ]
    }
}

For endpoints (SystemServiceServlet) only, for my custom servlets it's not. This is evidence that there is some layer above. ;)

Upvotes: 1

Nick
Nick

Reputation: 3591

From the looks of things, it isn't possible. There is another Stack Overflow Q&A on this question which answered the same way. You can make a Feature Request at the App Engine Public Issue Tracker, of course.

Note: Attempts to use javax.servlet.Filter to intercept the ServletResponse, cast it to an HttpServletResponse and call .setHeader() on it yielded no success, so it appears there are other layers which are intercepting the response and wrapping up one without any headers added which aren't "meant" to be there, according to Endpoints. I didn't experiment with subclassing SystemServiceServlet, as the linked Q&A suggested, although that seems fraught with difficulties as well, and undocumented.

Upvotes: 1

Related Questions