-
-
Save xfyre/dd84087929b1bb4627bcf3fe6df9a6cb to your computer and use it in GitHub Desktop.
T5 Atmosphere sidecar
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
public class AppModule { | |
// ..... // | |
@Contribute(HttpServletRequestHandler.class) | |
public void contributeHttpServletRequestHandler(OrderedConfiguration<HttpServletRequestFilter> configuration, | |
@Inject @Symbol(ResteasySymbols.MAPPING_PREFIX) final String restFilterPath, | |
@Inject @Symbol(DcnetSymbolConstants.DCNET_WS_PATH_PREFIX) final String wsFilterPath, | |
@Inject @Symbol(SymbolConstants.CHARSET) final String applicationCharset, | |
@Primary final SessionPersistedObjectAnalyzer<?> analyzer, | |
final PerthreadManager perThreadManager, | |
final RequestGlobals requestGlobals) { | |
final HttpServletRequestFilter propagateRequestGlobals = (servletRequest, servletResponse, handler) -> { | |
final boolean isRestApiRequest = servletRequest.getServletPath().startsWith(restFilterPath + "/"); | |
final boolean isWsRequest = servletRequest.getServletPath().startsWith(wsFilterPath + "/"); | |
if (isRestApiRequest || isWsRequest) { | |
final TapestrySessionFactoryImpl sessionFactory = | |
new TapestrySessionFactoryImpl(false, analyzer, servletRequest, perThreadManager, false); | |
final Request request = new RequestImpl(servletRequest, applicationCharset, sessionFactory); | |
final Response response = new ResponseImpl(servletRequest, servletResponse); | |
if (isRestApiRequest) { | |
response.setHeader("Access-Control-Allow-Origin", "*"); // all REST services are public | |
} | |
requestGlobals.storeRequestResponse(request, response); | |
} | |
setupMdc(servletRequest); | |
return handler.service(servletRequest, servletResponse); | |
}; | |
configuration.add("SetupRestSession", propagateRequestGlobals, | |
"before:ResteasyRequestFilter", "before:AtmosphereRequestFilter"); | |
} | |
// ..... // | |
} |
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
public class AtmosphereRequestFilter implements HttpServletRequestFilter { | |
private static final Logger log = LoggerFactory.getLogger(AtmosphereRequestFilter.class); | |
private final AtmosphereFramework framework; | |
private final String wsPrefix; | |
public AtmosphereRequestFilter(final AtmosphereFramework framework, | |
@Inject @Symbol(DCNET_WS_PATH_PREFIX) final String wsPathPrefix) { | |
this.framework = framework; | |
this.wsPrefix = wsPathPrefix; | |
} | |
@Override | |
public boolean service(final HttpServletRequest request, | |
final HttpServletResponse response, | |
final HttpServletRequestHandler handler) throws IOException { | |
final String requestPath = Optional.ofNullable(request.getPathInfo()) | |
.map(pathInfo -> request.getServletPath() + pathInfo) | |
.orElseGet(request::getServletPath); | |
if (requestPath.startsWith(wsPrefix)) { | |
final AtmosphereRequest atmosphereRequest = AtmosphereRequestImpl.wrap(request); | |
try { | |
log.debug("Atmosphere request: {}", atmosphereRequest.getRequestURL().toString()); | |
final Action action = | |
framework.doCometSupport(atmosphereRequest, AtmosphereResponseImpl.wrap(response)); | |
log.debug("Atmosphere action: {}", action.toString()); | |
} catch (ServletException e) { | |
e.printStackTrace(); | |
return true; | |
} | |
return true; | |
} | |
return handler.service(request, response); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment