Search in sources :

Example 1 with AnnotatedProxy

use of org.atmosphere.handler.AnnotatedProxy in project atmosphere by Atmosphere.

the class PathParamIntrospector method injectable.

@Override
public String injectable(AtmosphereResource r) {
    String named = pathLocal.get();
    String[] paths = (String[]) r.getRequest().getAttribute(PathParam.class.getName());
    if (paths == null || paths.length != 2) {
        AtmosphereFramework.AtmosphereHandlerWrapper w = (AtmosphereFramework.AtmosphereHandlerWrapper) r.getRequest().getAttribute(FrameworkConfig.ATMOSPHERE_HANDLER_WRAPPER);
        if (w != null) {
            if (AnnotatedProxy.class.isAssignableFrom(w.atmosphereHandler.getClass())) {
                AnnotatedProxy ap = AnnotatedProxy.class.cast(w.atmosphereHandler);
                if (ap.target().getClass().isAnnotationPresent(ManagedService.class)) {
                    String targetPath = ap.target().getClass().getAnnotation(ManagedService.class).path();
                    if (targetPath.indexOf("{") != -1 && targetPath.indexOf("}") != -1) {
                        paths = new String[] { Utils.pathInfo(r.getRequest()), targetPath };
                    }
                }
            }
            if (paths == null || paths.length != 2) {
                return null;
            }
        }
    }
    /* first, split paths at slashes and map {{parameter names}} to values from pathLocal */
    logger.debug("Path: {}, targetPath: {}", pathLocal, paths[1]);
    String[] inParts = paths[0].split("/");
    String[] outParts = paths[1].split("/");
    Map<String, String> annotatedPathVars = new HashMap<>();
    int len = Math.min(outParts.length, inParts.length);
    for (int i = 0; i < len; i++) {
        String s = outParts[i];
        if (s.startsWith("{") && s.endsWith("}")) {
            /* we remove braces from string and put it to our map and also pathLocal that regex like room: [a-zA-Z][a-zA-Z_0-9]* */
            int end = s.contains(":") ? s.indexOf(":") : s.length() - 1;
            annotatedPathVars.put(s.substring(1, end), inParts[i]);
            logger.debug("Putting PathVar pair: {} -> {}", s.substring(1, s.length() - 1), inParts[i]);
        }
    }
    return annotatedPathVars.get(named);
}
Also used : ManagedService(org.atmosphere.config.service.ManagedService) HashMap(java.util.HashMap) AnnotatedProxy(org.atmosphere.handler.AnnotatedProxy) AtmosphereFramework(org.atmosphere.runtime.AtmosphereFramework)

Example 2 with AnnotatedProxy

use of org.atmosphere.handler.AnnotatedProxy in project atmosphere by Atmosphere.

the class ManagedServiceInterceptor method mapAnnotatedService.

protected void mapAnnotatedService(boolean reMap, String path, AtmosphereRequest request, AtmosphereFramework.AtmosphereHandlerWrapper w) {
    synchronized (config.handlers()) {
        if (config.handlers().get(path) == null) {
            // ManagedService
            if (AnnotatedProxy.class.isAssignableFrom(w.atmosphereHandler.getClass())) {
                AnnotatedProxy ap = AnnotatedProxy.class.cast(w.atmosphereHandler);
                ManagedAnnotation a = managed(ap, request.resource());
                if (a != null) {
                    String targetPath = a.path();
                    if (targetPath.indexOf("{") != -1 && targetPath.indexOf("}") != -1) {
                        try {
                            boolean singleton = ap.target().getClass().getAnnotation(Singleton.class) != null;
                            if (!singleton) {
                                ap = proxyHandler();
                                final Object o = config.framework().newClassInstance(Object.class, AnnotatedProxy.class.cast(w.atmosphereHandler).target().getClass());
                                ap.configure(config, o);
                            }
                            request.localAttributes().put(Named.class.getName(), path.substring(targetPath.indexOf("{")));
                            if (ap.pathParams()) {
                                request.localAttributes().put(PathParam.class.getName(), new String[] { path, targetPath });
                            }
                            AtmosphereResourceImpl.class.cast(request.resource()).atmosphereHandler(ap);
                            config.framework().addAtmosphereHandler(path, ap, config.getBroadcasterFactory().lookup(a.broadcaster(), path, true), w.interceptors);
                            request.setAttribute(FrameworkConfig.NEW_MAPPING, "true");
                        } catch (Throwable e) {
                            logger.warn("Unable to create AtmosphereHandler", e);
                        }
                    }
                }
            }
        } else if (reMap) {
            request.setAttribute(FrameworkConfig.NEW_MAPPING, "true");
        }
    }
}
Also used : Named(javax.inject.Named) AnnotatedProxy(org.atmosphere.handler.AnnotatedProxy) Singleton(org.atmosphere.config.service.Singleton) AtmosphereResourceImpl(org.atmosphere.runtime.AtmosphereResourceImpl) PathParam(org.atmosphere.config.service.PathParam)

Aggregations

AnnotatedProxy (org.atmosphere.handler.AnnotatedProxy)2 HashMap (java.util.HashMap)1 Named (javax.inject.Named)1 ManagedService (org.atmosphere.config.service.ManagedService)1 PathParam (org.atmosphere.config.service.PathParam)1 Singleton (org.atmosphere.config.service.Singleton)1 AtmosphereFramework (org.atmosphere.runtime.AtmosphereFramework)1 AtmosphereResourceImpl (org.atmosphere.runtime.AtmosphereResourceImpl)1