Search in sources :

Example 1 with ClientResourceException

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

the class StartSessionResource method createEvent.

@Override
@SuppressWarnings("unchecked")
protected TestEvent createEvent(final String sessionId, final String body) throws ClientResourceException {
    final Map<?, ?> values = newHashMap();
    try {
        if (!isNullOrEmpty(body)) {
            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 Map<String, String> properties = newHashMap();
    if (null != values.get(PROPERTIES)) {
        if (!(values.get(PROPERTIES) instanceof Map)) {
            throw new ClientResourceException(SC_UNPROCESSABLE_ENTITY);
        } else {
            ((Map<?, ?>) values.get(PROPERTIES)).entrySet().forEach(new Consumer<Entry<?, ?>>() {

                @Override
                public void accept(final Entry<?, ?> entry) {
                    properties.put(valueOf(entry.getKey()), valueOf(entry.getValue()));
                }
            });
        }
    }
    return new SessionStartedEvent(sessionId, properties);
}
Also used : IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) SessionStartedEvent(org.eclipse.n4js.tester.events.SessionStartedEvent) ClientResourceException(org.eclipse.n4js.tester.server.resources.ClientResourceException) Entry(java.util.Map.Entry) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Map(java.util.Map)

Example 2 with ClientResourceException

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

the class StartTestResource 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 Map<String, String> properties = newHashMap();
    if (null != values.get(PROPERTIES)) {
        if (!(values.get(PROPERTIES) instanceof Map)) {
            throw new ClientResourceException(SC_UNPROCESSABLE_ENTITY);
        } else {
            ((Map<?, ?>) values.get(PROPERTIES)).entrySet().forEach(new Consumer<Entry<?, ?>>() {

                @Override
                public void accept(final Entry<?, ?> entry) {
                    properties.put(valueOf(entry.getKey()), valueOf(entry.getValue()));
                }
            });
        }
    }
    try {
        final long timeout = parseLong(valueOf(value));
        return new TestStartedEvent(sessionId, testId, timeout, properties);
    } catch (final NumberFormatException e) {
        // although schema was valid the data was indeed invalid
        throw new ClientResourceException(SC_BAD_REQUEST);
    }
}
Also used : IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ClientResourceException(org.eclipse.n4js.tester.server.resources.ClientResourceException) TestStartedEvent(org.eclipse.n4js.tester.events.TestStartedEvent) Entry(java.util.Map.Entry) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Maps.newHashMap(com.google.common.collect.Maps.newHashMap)

Example 3 with ClientResourceException

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

the class SessionResource method doHandle.

@Override
protected int doHandle(final String body, final String pathInfo) throws ServletException {
    final Matcher matcher = SESSION_ID_PATTERN.matcher(pathInfo);
    String sessionId = null;
    if (matcher.find()) {
        sessionId = matcher.group();
    }
    if (sessionExists(sessionId)) {
        try {
            bus.post(createEvent(sessionId, body));
        } catch (final Exception e) {
            final Throwable rootCause = Throwables.getRootCause(e);
            if (rootCause instanceof ClientResourceException) {
                throw (ClientResourceException) rootCause;
            }
            LOGGER.error("Error while creating event for test session. [Session ID: " + sessionId + "]", e);
            throw new ServletException(e);
        }
        return SC_OK;
    }
    return SC_NOT_FOUND;
}
Also used : ServletException(javax.servlet.ServletException) ClientResourceException(org.eclipse.n4js.tester.server.resources.ClientResourceException) Matcher(java.util.regex.Matcher) ServletException(javax.servlet.ServletException) ClientResourceException(org.eclipse.n4js.tester.server.resources.ClientResourceException)

Example 4 with ClientResourceException

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

the class StartTestResource method handleStatusOk.

@Override
protected void handleStatusOk(final HttpServletRequest req, final HttpServletResponse resp, String escapedPathInfo) throws ClientResourceException {
    TestResourceParameters parameters = getParametersFromPathInfo(escapedPathInfo);
    if (null != parameters && !isNullOrEmpty(parameters.sessionId) && !isNullOrEmpty(parameters.testId)) {
        try {
            final String body = mapper.writeValueAsString(new StartTestResponse(parameters.sessionId, parameters.testId).data);
            try (final OutputStream os = resp.getOutputStream();
                final OutputStreamWriter osw = new OutputStreamWriter(os)) {
                osw.write(body);
                osw.flush();
            }
        } catch (final IOException e) {
            throw new ClientResourceException(SC_BAD_REQUEST);
        }
    }
}
Also used : ClientResourceException(org.eclipse.n4js.tester.server.resources.ClientResourceException) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException)

Example 5 with ClientResourceException

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

the class PingSessionResource method createEvent.

@Override
@SuppressWarnings("unchecked")
protected TestEvent createEvent(final String sessionId, 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 SessionPingedEvent(sessionId, 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) SessionPingedEvent(org.eclipse.n4js.tester.events.SessionPingedEvent) 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