-
-
Notifications
You must be signed in to change notification settings - Fork 756
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1165 from sobolewsk/cors-interceptor
Add CORS interceptor
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
modules/cpr/src/main/java/org/atmosphere/interceptor/CorsInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.atmosphere.interceptor; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import org.atmosphere.cpr.Action; | ||
import org.atmosphere.cpr.AtmosphereConfig; | ||
import org.atmosphere.cpr.AtmosphereInterceptor; | ||
import org.atmosphere.cpr.AtmosphereRequest; | ||
import org.atmosphere.cpr.AtmosphereResource; | ||
import org.atmosphere.cpr.AtmosphereResponse; | ||
|
||
/** | ||
* CORS support | ||
* | ||
* @author Janusz Sobolewski | ||
*/ | ||
public class CorsInterceptor implements AtmosphereInterceptor { | ||
|
||
@Override | ||
public void configure(AtmosphereConfig arg0) { | ||
} | ||
|
||
@Override | ||
public Action inspect(AtmosphereResource resource) { | ||
|
||
AtmosphereRequest req = resource.getRequest(); | ||
AtmosphereResponse res = resource.getResponse(); | ||
|
||
if(req.getHeader("Origin") != null){ | ||
res.addHeader("Access-Control-Allow-Origin", req.getHeader("Origin")); | ||
res.addHeader("Access-Control-Expose-Headers", "X-Cache-Date, X-Atmosphere-tracking-id"); | ||
res.setHeader("Access-Control-Allow-Credentials", "true"); | ||
} | ||
|
||
if("OPTIONS".equals(req.getMethod())){ | ||
res.setHeader("Access-Control-Allow-Methods", "OPTIONS, GET, POST"); | ||
res.setHeader("Access-Control-Allow-Headers", | ||
"Origin, Content-Type, X-Atmosphere-Framework, X-Cache-Date, X-Atmosphere-tracking-id, X-Atmosphere-Transport"); | ||
res.setHeader("Access-Control-Max-Age", "-1"); | ||
|
||
final AtomicReference<String> emptyMessage = new AtomicReference<String>(""); | ||
res.write(emptyMessage.get()); | ||
|
||
return Action.SKIP_ATMOSPHEREHANDLER; | ||
} | ||
|
||
return Action.CONTINUE; | ||
} | ||
|
||
@Override | ||
public void postInspect(AtmosphereResource resource) { | ||
|
||
} | ||
|
||
} |