Search in sources :

Example 1 with DefaultHttpHeaders

use of org.apache.struts2.rest.DefaultHttpHeaders in project onebusaway-application-modules by camsys.

the class CurrentTimeActionTest method test.

@Test
public void test() throws ParseException {
    CurrentTimeAction action = new CurrentTimeAction();
    long t = System.currentTimeMillis();
    DefaultHttpHeaders headers = action.index();
    assertEquals(200, headers.getStatus());
    ResponseBean response = action.getModel();
    assertEquals(200, response.getCode());
    assertEquals(2, response.getVersion());
    @SuppressWarnings("unchecked") EntryWithReferencesBean<TimeBean> entry = (EntryWithReferencesBean<TimeBean>) response.getData();
    TimeBean time = entry.getEntry();
    assertNotNull(time);
    long delta = Math.abs(time.getTime() - t);
    assertTrue("check that time delta is within limits: delta=" + delta, delta < 100);
    String readableTime = DateLibrary.getTimeAsIso8601String(new Date(time.getTime()));
    assertEquals(readableTime, time.getReadableTime());
}
Also used : EntryWithReferencesBean(org.onebusaway.api.model.transit.EntryWithReferencesBean) DefaultHttpHeaders(org.apache.struts2.rest.DefaultHttpHeaders) ResponseBean(org.onebusaway.api.model.ResponseBean) TimeBean(org.onebusaway.api.model.TimeBean) Date(java.util.Date) Test(org.junit.Test)

Example 2 with DefaultHttpHeaders

use of org.apache.struts2.rest.DefaultHttpHeaders in project onebusaway-application-modules by camsys.

the class ApiActionSupport method setValidationErrorsResponse.

protected DefaultHttpHeaders setValidationErrorsResponse() {
    ValidationErrorBean bean = new ValidationErrorBean(new ArrayList<String>(getActionErrors()), getFieldErrors());
    _response = new ResponseBean(getReturnVersion(), ResponseCodes.RESPONSE_INVALID_ARGUMENT, "validation error", bean);
    return new DefaultHttpHeaders().withStatus(_response.getCode());
}
Also used : DefaultHttpHeaders(org.apache.struts2.rest.DefaultHttpHeaders) ResponseBean(org.onebusaway.api.model.ResponseBean)

Example 3 with DefaultHttpHeaders

use of org.apache.struts2.rest.DefaultHttpHeaders 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)

Aggregations

DefaultHttpHeaders (org.apache.struts2.rest.DefaultHttpHeaders)3 ResponseBean (org.onebusaway.api.model.ResponseBean)3 ActionProxy (com.opensymphony.xwork2.ActionProxy)1 Date (java.util.Date)1 Test (org.junit.Test)1 TimeBean (org.onebusaway.api.model.TimeBean)1 EntryWithReferencesBean (org.onebusaway.api.model.transit.EntryWithReferencesBean)1