use of java.net.HttpURLConnection.HTTP_INTERNAL_ERROR in project oap by oaplatform.
the class WsService method handleInternal.
private void handleInternal(Request request, Response response, Reflection.Method method, Name name, Pair<String, Session> session) {
log.trace("{}: Internal session status: [{}]", service(), session);
Optional<WsMethod> wsMethod = method.findAnnotation(WsMethod.class);
Function<Reflection.Parameter, Object> func = (p) -> {
val ret = getValue(session, request, wsMethod, p).orElse(Optional.empty());
if (ret instanceof Optional)
return ((Optional<?>) ret).orElse(null);
return ret;
};
HttpResponse interceptorResponse = session != null ? runInterceptors(request, session._2, method, func) : null;
if (interceptorResponse != null) {
response.respond(interceptorResponse);
} else {
Metrics.measureTimer(name, () -> {
List<Reflection.Parameter> parameters = method.parameters;
LinkedHashMap<Reflection.Parameter, Object> originalValues = getOriginalValues(session, parameters, request, wsMethod);
ValidationErrors paramValidation = ValidationErrors.empty();
originalValues.forEach((parameter, value) -> paramValidation.merge(Validators.forParameter(method, parameter, impl, true).validate(value, originalValues)));
paramValidation.throwIfInvalid();
Validators.forMethod(method, impl, true).validate(originalValues.values().toArray(new Object[originalValues.size()]), originalValues).throwIfInvalid();
LinkedHashMap<Reflection.Parameter, Object> values = getValues(originalValues);
Object[] paramValues = values.values().toArray(new Object[values.size()]);
values.forEach((parameter, value) -> paramValidation.merge(Validators.forParameter(method, parameter, impl, false).validate(value, values)));
paramValidation.throwIfInvalid();
Validators.forMethod(method, impl, false).validate(paramValues, values).throwIfInvalid();
Object result = method.invoke(impl, paramValues);
Boolean isRaw = wsMethod.map(WsMethod::raw).orElse(false);
ContentType produces = wsMethod.map(wsm -> ContentType.create(wsm.produces()).withCharset(UTF_8)).orElse(APPLICATION_JSON);
String cookie = session != null ? new HttpResponse.CookieBuilder().withSID(session._1).withPath(sessionManager.cookiePath).withExpires(DateTime.now().plusMinutes(sessionManager.cookieExpiration)).withDomain(sessionManager.cookieDomain).withDomain(sessionManager.cookieDomain).build() : null;
if (method.isVoid())
response.respond(NO_CONTENT);
else if (result instanceof HttpResponse)
response.respond(((HttpResponse) result).withCookie(cookie));
else if (result instanceof Optional<?>) {
response.respond(((Optional<?>) result).map(r -> HttpResponse.ok(runPostInterceptors(r, session, method), isRaw, produces).withCookie(cookie)).orElse(NOT_FOUND));
} else if (result instanceof Result<?, ?>) {
Result<HttpResponse, HttpResponse> resp = ((Result<?, ?>) result).mapSuccess(r -> HttpResponse.ok(r, isRaw, produces).withCookie(cookie)).mapFailure(r -> HttpResponse.status(HTTP_INTERNAL_ERROR, "", r).withCookie(cookie));
response.respond(resp.isSuccess() ? ((Result<?, ?>) result).mapSuccess(r -> HttpResponse.ok(runPostInterceptors(r, session, method), isRaw, produces).withCookie(cookie)).successValue : ((Result<?, ?>) result).mapFailure(r -> HttpResponse.status(HTTP_INTERNAL_ERROR, "", r).withCookie(cookie)).failureValue);
} else if (result instanceof Stream<?>) {
response.respond(HttpResponse.stream(((Stream<?>) result).map(v -> runPostInterceptors(v, session, method)), isRaw, produces).withCookie(cookie));
} else
response.respond(HttpResponse.ok(runPostInterceptors(result, session, method), isRaw, produces).withCookie(cookie));
});
}
}
Aggregations