Search in sources :

Example 6 with ActionProxy

use of com.opensymphony.xwork2.ActionProxy in project onebusaway-application-modules by camsys.

the class ApiKeyInterceptor method unauthorized.

// package private for unit tests
String unauthorized(ActionInvocation invocation, ApiKeyPermissionService.Status reason) throws IOException {
    ActionProxy proxy = invocation.getProxy();
    int httpCode = ResponseCodes.RESPONSE_UNAUTHORIZED;
    String message = "permission denied";
    switch(reason) {
        case UNAUTHORIZED:
            httpCode = ResponseCodes.RESPONSE_UNAUTHORIZED;
            break;
        case RATE_EXCEEDED:
            httpCode = ResponseCodes.RESPONSE_TOO_MANY_REQUESTS;
            message = "rate limit exceeded";
            break;
        case AUTHORIZED:
            // this should never happen!
            throw new IllegalStateException("Valid status code " + reason + " in unauthorized response");
        default:
            httpCode = ResponseCodes.RESPONSE_UNAUTHORIZED;
    }
    ResponseBean response = new ResponseBean(1, httpCode, message, null);
    DefaultHttpHeaders methodResult = new DefaultHttpHeaders().withStatus(response.getCode());
    return _handlerSelector.handleResult(proxy.getConfig(), methodResult, response);
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) DefaultHttpHeaders(org.apache.struts2.rest.DefaultHttpHeaders) ResponseBean(org.onebusaway.api.model.ResponseBean)

Example 7 with ActionProxy

use of com.opensymphony.xwork2.ActionProxy in project onebusaway-application-modules by camsys.

the class ExceptionInterceptor method getActionAsUrl.

private String getActionAsUrl(ActionInvocation invocation) {
    ActionProxy proxy = invocation.getProxy();
    ActionContext context = invocation.getInvocationContext();
    StringBuilder b = new StringBuilder();
    b.append(proxy.getNamespace());
    b.append("/");
    b.append(proxy.getActionName());
    b.append("!");
    b.append(proxy.getMethod());
    Map<String, Object> params = context.getParameters();
    if (!params.isEmpty()) {
        b.append("?");
        boolean seenFirst = false;
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            // Prune out any identifying information
            if ("app_uid".equals(entry.getKey()))
                continue;
            Object value = entry.getValue();
            String[] values = (value instanceof String[]) ? (String[]) value : new String[] { value.toString() };
            for (String v : values) {
                if (seenFirst)
                    b.append("&");
                else
                    seenFirst = true;
                b.append(entry.getKey());
                b.append("=");
                b.append(v);
            }
        }
    }
    return b.toString();
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) ActionContext(com.opensymphony.xwork2.ActionContext) Map(java.util.Map)

Example 8 with ActionProxy

use of com.opensymphony.xwork2.ActionProxy in project onebusaway-application-modules by camsys.

the class IndexActionTest method testExecute.

@Test
public void testExecute() throws Exception {
    ActionProxy proxy = Mockito.mock(ActionProxy.class);
    Mockito.when(proxy.getActionName()).thenReturn("index");
    Mockito.when(proxy.getNamespace()).thenReturn("/");
    ActionInvocation invocation = Mockito.mock(ActionInvocation.class);
    Mockito.when(invocation.getProxy()).thenReturn(proxy);
    Map<String, Object> c = new HashMap<String, Object>();
    ActionContext ac = new ActionContext(c);
    ac.setActionInvocation(invocation);
    ActionContext.setContext(ac);
    IndexAction action = new IndexAction();
    String response = action.execute();
    assertEquals("successful response", "success", response);
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ActionContext(com.opensymphony.xwork2.ActionContext) Test(org.junit.Test)

Aggregations

ActionProxy (com.opensymphony.xwork2.ActionProxy)8 ActionContext (com.opensymphony.xwork2.ActionContext)6 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)5 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 DateParseException (org.apache.commons.httpclient.util.DateParseException)1 DefaultHttpHeaders (org.apache.struts2.rest.DefaultHttpHeaders)1 ResponseBean (org.onebusaway.api.model.ResponseBean)1