users@glassfish.java.net

Re: Access-Control-Allow-Origin

From: Manuel Blechschmidt <blechschmidt_at_apaxo.de>
Date: Tue, 5 Nov 2013 19:57:09 -0500

Hi Charu,
if you are using jersey use the following:

import java.util.List;

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;
/**
 * Adds CORS headers to be able to read the
 * data from other servers.
 *
 * http://stackoverflow.com/questions/10343892/glassfish-how-to-set-access-control-allow-origin-header
 * @author Manuel Blechschmidt <blechschmidt_at_apaxo.de>
 *
 */
public class CrossOriginResourceSharingFilter implements ContainerResponseFilter {

    @Override
    public ContainerResponse filter(ContainerRequest creq, ContainerResponse cresp) {
        List<String> originList = creq.getRequestHeader("Origin");
        String origin = (originList != null && originList.size() > 0) ? originList.get(0) : "*";
        // Chrome does not allow origin * when alllow credentials
        cresp.getHttpHeaders().putSingle("Access-Control-Allow-Origin", origin);
        cresp.getHttpHeaders().putSingle("Access-Control-Allow-Credentials", "true");
        cresp.getHttpHeaders().putSingle("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS");
        cresp.getHttpHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type, Depth, User-Agent, If-Modified-Since, Cache-Control, Accept, Cookie, Referer");
        return cresp;
    }
}


Am 05.11.2013 um 12:11 schrieb charu verma:

> How can we add the Access-Control-Allow-Origin HTTP header to the service response in glassfish?
>
> -Charu

Am 05.11.2013 um 12:11 schrieb charu verma:

> How can we add the Access-Control-Allow-Origin HTTP header to the service response in glassfish?
>
> -Charu

Mit freundlichen Grüßen / Best Regards
 
Manuel Blechschmidt
Chief Technology Officer
 
Apaxo GmbH
Office Potsdam
Weinbergstr. 16
14482 Potsdam
Germany
 
Fon +49 (0)6204 9180 593
Fax +49 (0)6204 9180 594
Mobil: +49 173/6322621

Email: blechschmidt_at_apaxo.de
Skype: Manuel_B1986
Twitter: http://twitter.com/Manuel_B
 



 
Sitz der Gesellschaft: Viernheim
Handelsregister HRB 87159
Ust-IdNr. DE261368874
Amtsgericht Darmstadt