I'm trying to set the 'Access-Control-Allow-Origin: *' header for the response. However, the header is not present. What am I doing wrong?
public class JsonApplication extends Application
{
private static final String SERVER_URL = "http://localhost:8111";
public static void main(String[] args) throws Exception
{
Server testServer = new Server(Protocol.HTTP, 8111);
JsonApplication jsonApplication = new JsonApplication();
CorsService corsService = new CorsService();
corsService.setAllowedOrigins( new HashSet(Arrays.asList("*")));
corsService.setAllowedCredentials(true);
jsonApplication.getServices().add( corsService );
testServer.setNext( jsonApplication );
testServer.start();
}
@Override
public Restlet createInboundRoot()
{
Router router = new Router( getContext() );
router.attach( SERVER_URL + "/", RootResource.class );
return router;
}
}
I checked the source code for org.restlet.engine.application.CorsResponseHelper and it contains the following code:
public void addCorsResponseHeaders(Request request, Response response) {
String origin = request.getHeaders().getFirstValue("Origin", true);
if (origin == null) {
// Not a CORS request
return;
}
...
}
So, it appears that the current CORS implementation does not support requests made from a local html-file, since in that case origin == null.
I added some logging to my servlet application:
JsonApplication::createInboundRoot()
16:26 MyCorsService()
16:26 myCorsService = wwwdbase.rest.cors.MyCorsService@6e1b241d
16:26 MyCorsService::setAllowedOrigins(), [*]
16:26 services: [org.restlet.service.TunnelService@4c88fe62,
org.restlet.service.StatusService@68349a5b,
org.restlet.service.DecoderService@77cfd8d3,
org.restlet.service.EncoderService@40c331fb,
org.restlet.service.RangeService@4bb3bc6f,
org.restlet.service.ConnectorService@7990100,
org.restlet.service.ConnegService@e194860,
org.restlet.service.ConverterService@578cfcb1,
org.restlet.service.MetadataService@18a62eb,
org.restlet.service.TaskService@4ed4f2db,
wwwdbase.rest.cors.MyCorsService@6e1b241d]
We can see that MyCorsService is available. However, it is never called by the servlet framework. On the other hand, if I run the service (the Java SE version) from IDE, MyCorsService is called. Why do these cases behave differently?
Partial solution: I managed to add the allow origin header by changing the code in org.restlet.engine.header.HeaderUtils
if (response.getAccessControlAllowOrigin() != null)
{
addHeader(HeaderConstants.HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
response.getAccessControlAllowOrigin(), headers);
}
to
if (response.getAccessControlAllowOrigin() != null)
{
addHeader(HeaderConstants.HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
response.getAccessControlAllowOrigin(), headers);
}
else
{
// --- Add in any case!
//
response.setAccessControlAllowOrigin( "*" );
addHeader(HeaderConstants.HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
response.getAccessControlAllowOrigin(), headers);
}
However, the answer to the question why the servlet framework in Tomcat did not call the cors service is still unknown...