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);
}
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);
}
}
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;
}
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);
}
}
}
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);
}
}
Aggregations