Search in sources :

Example 6 with ClientResourceException

use of org.eclipse.n4js.tester.server.resources.ClientResourceException in project n4js by eclipse.

the class PingTestResource method createEvent.

@Override
@SuppressWarnings("unchecked")
protected TestEvent createEvent(final String sessionId, final String testId, final String body) throws ClientResourceException {
    if (isNullOrEmpty(body))
        throw new ClientResourceException(SC_BAD_REQUEST);
    final Map<?, ?> values = newHashMap();
    try {
        values.putAll(mapper.readValue(body, Map.class));
    } catch (JsonMappingException | JsonParseException e) {
        throw new ClientResourceException(SC_UNPROCESSABLE_ENTITY);
    } catch (final IOException e) {
        throw new ClientResourceException(SC_BAD_REQUEST);
    }
    final Object value = values.get(TIMEOUT_KEY);
    // incorrect schema
    if (null == value) {
        throw new ClientResourceException(SC_UNPROCESSABLE_ENTITY);
    }
    final Object comment = values.get(COMMENT_KEY);
    try {
        final long timeout = parseLong(Objects.toString(value));
        return new TestPingedEvent(sessionId, testId, timeout, null == comment ? null : valueOf(comment));
    } catch (final NumberFormatException e) {
        // although schema was valid the data was indeed invalid
        throw new ClientResourceException(SC_BAD_REQUEST);
    }
}
Also used : ClientResourceException(org.eclipse.n4js.tester.server.resources.ClientResourceException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) TestPingedEvent(org.eclipse.n4js.tester.events.TestPingedEvent) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Map(java.util.Map)

Aggregations

ClientResourceException (org.eclipse.n4js.tester.server.resources.ClientResourceException)6 IOException (java.io.IOException)5 JsonParseException (com.fasterxml.jackson.core.JsonParseException)4 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)4 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)4 Map (java.util.Map)4 Entry (java.util.Map.Entry)2 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Collections.singletonMap (java.util.Collections.singletonMap)1 Matcher (java.util.regex.Matcher)1 ServletException (javax.servlet.ServletException)1 SessionPingedEvent (org.eclipse.n4js.tester.events.SessionPingedEvent)1 SessionStartedEvent (org.eclipse.n4js.tester.events.SessionStartedEvent)1 TestPingedEvent (org.eclipse.n4js.tester.events.TestPingedEvent)1 TestStartedEvent (org.eclipse.n4js.tester.events.TestStartedEvent)1