Skip to content

Commit

Permalink
Fix for #949
Browse files Browse the repository at this point in the history
  • Loading branch information
jfarcand committed Mar 18, 2013
1 parent 82ce422 commit 7032b59
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.atmosphere.util.DefaultEndpointMapper;
import org.atmosphere.util.EndpointMapper;
import org.atmosphere.util.IntrospectionUtils;
import org.atmosphere.util.ServletProxyFactory;
import org.atmosphere.util.Version;
import org.atmosphere.websocket.DefaultWebSocketProcessor;
import org.atmosphere.websocket.WebSocket;
Expand Down Expand Up @@ -466,8 +467,7 @@ public ServletContext getServletContext() {
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
logger.trace("Method {} not supported", method.getName());
return null;
return ServletProxyFactory.getDefault().proxy(proxy, method, args);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.atmosphere.cpr;

import org.atmosphere.websocket.WebSocket;
import org.atmosphere.util.ServletProxyFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -70,8 +71,7 @@ public class AtmosphereResponse extends HttpServletResponseWrapper {
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
logger.trace("Method {} not supported", method.getName());
return null;
return ServletProxyFactory.getDefault().proxy(proxy, method, args);
}
});
private final AtomicBoolean writeStatusAndHeader = new AtomicBoolean(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2012 Jeanfrancois Arcand
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.atmosphere.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
* A Factory class that can be used to handle the Servlet API internal proxy.
*
* @author Jeanfrancois Arcand
*/
public class ServletProxyFactory {
private final static Logger logger = LoggerFactory.getLogger(ServletProxyFactory.class);
private static ServletProxyFactory factory;
private final Map<String, MethodHandler> methods = new HashMap<String, MethodHandler>();
private final static MethodHandler voidMethodHandler = new EchoMethodHandler();

private ServletProxyFactory() {
addMethodHandler("encodeURL", voidMethodHandler).addMethodHandler("encodeRedirectURL", voidMethodHandler);
}

public final Object proxy(Object clazz, Method method, Object[] methodObjects) {
MethodHandler m = methods.get(method.getName());
if (m != null) {
logger.trace("Method {} handled by MethodHandler {}", method.getName(), m);
return m.handle(clazz, method, methodObjects);
}
logger.trace("Method {} not supported", method.getName());
return null;
}

public static ServletProxyFactory getDefault() {
if (factory == null) {
factory = new ServletProxyFactory();
}
return factory;
}

public ServletProxyFactory addMethodHandler(String method, MethodHandler m) {
methods.put(method, m);
return this;
}

/**
* A MethodHandler can be added to allow Frameworks using Atmosphere to customize internal behavior.
*/
public static interface MethodHandler {
/**
* Same API as the {@link java.lang.reflect.Proxy} class
* @param clazz
* @param method
* @param methodObjects
* @return this
*/
public Object handle(Object clazz, Method method, Object[] methodObjects);
}

public static class EchoMethodHandler implements MethodHandler{
@Override
public Object handle(Object clazz, Method method, Object[] methodObjects) {
return methodObjects[0];
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,37 @@ public void destroy() {

}

@Test
public void encodeURLProxyTest() throws IOException, ServletException, ExecutionException, InterruptedException {
ByteArrayOutputStream b = new ByteArrayOutputStream();
final WebSocket w = new ArrayBaseWebSocket(b);
final WebSocketProcessor processor = WebSocketProcessorFactory.getDefault()
.getWebSocketProcessor(framework);
final AtomicReference<String> url = new AtomicReference<String>();

framework.addAtmosphereHandler("/*", new AtmosphereHandler() {

@Override
public void onRequest(AtmosphereResource resource) throws IOException {
url.set(resource.getResponse().encodeRedirectURL("http://127.0.0.1:8080"));
}

@Override
public void onStateChange(AtmosphereResourceEvent event) throws IOException {
}

@Override
public void destroy() {
}
});

AtmosphereRequest request = new AtmosphereRequest.Builder().destroyable(false).body("yoComet").pathInfo("/a").build();
processor.open(w, request);
processor.invokeWebSocketProtocol(w, "yoWebSocket");

assertEquals(url.get(), "http://127.0.0.1:8080");

}

@Test
public void basicBackwardCompatbileWorkflow() throws IOException, ServletException, ExecutionException, InterruptedException {
Expand Down

0 comments on commit 7032b59

Please sign in to comment.