use of org.bimserver.shared.reflector.ReflectorException in project BIMserver by opensourceBIM.
the class JsonReflector method callMethod.
@Override
public Object callMethod(String interfaceName, String methodName, Class<?> definedReturnType, KeyValuePair... args) throws ServerException, UserException, ReflectorException {
try {
JsonObject request = new JsonObject();
request.add("interface", new JsonPrimitive(interfaceName));
request.add("method", new JsonPrimitive(methodName));
JsonObject parameters = new JsonObject();
for (KeyValuePair arg : args) {
parameters.add(arg.getFieldName(), converter.toJson(arg.getValue()));
}
request.add("parameters", parameters);
JsonObject requestObject = new JsonObject();
requestObject.add("request", request);
JsonObject jsonResult = call(requestObject);
if (!isOneWay()) {
if (jsonResult == null) {
return null;
}
JsonObject response = jsonResult.getAsJsonObject("response");
if (response.has("exception")) {
JsonObject exceptionJson = response.getAsJsonObject("exception");
String exceptionType = exceptionJson.get("__type").getAsString();
String message = exceptionJson.has("message") ? exceptionJson.get("message").getAsString() : "unknown";
if (exceptionType.equals(UserException.class.getSimpleName())) {
if (exceptionJson.has("errorCode")) {
throw new UserException(message, ErrorCode.parse(exceptionJson.get("errorCode").getAsInt()));
} else {
throw new UserException(message);
}
} else if (exceptionType.equals(ServerException.class.getSimpleName())) {
if (exceptionJson.has("errorCode")) {
throw new ServerException(message, ErrorCode.parse(exceptionJson.get("errorCode").getAsInt()));
} else {
throw new ServerException(message);
}
} else {
if (exceptionJson.has("errorCode")) {
throw new ServerException(message, ErrorCode.parse(exceptionJson.get("errorCode").getAsInt()));
} else {
throw new ServerException(message);
}
}
} else if (response.has("result")) {
Object result = response.get("result");
SMethod method = servicesMap.getBySimpleName(interfaceName).getSMethod(methodName);
return converter.fromJson(method.getReturnType(), method.getGenericReturnType(), result);
} else {
return null;
}
} else {
return null;
}
} catch (ReflectorException e) {
throw e;
} catch (UserException e) {
throw e;
} catch (ServerException e) {
throw e;
} catch (Exception e) {
throw new ReflectorException(e);
}
}
use of org.bimserver.shared.reflector.ReflectorException in project BIMserver by opensourceBIM.
the class JsonSocketReflector method call.
public JsonObject call(JsonObject request) throws ReflectorException {
try {
if (tokenHolder.getToken() != null) {
request.addProperty("token", tokenHolder.getToken());
}
HttpPost httppost = new HttpPost(remoteAddress);
httppost.setHeader("Content-Type", "application/json");
httppost.setEntity(new StringEntity(request.toString(), Charsets.UTF_8));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(request.toString());
}
HttpResponse response = httpclient.execute(httppost, context);
try {
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity resultEntity = response.getEntity();
JsonParser parser = new JsonParser();
if (LOGGER.isDebugEnabled()) {
InputStream inputStream = resultEntity.getContent();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IOUtils.copy(inputStream, byteArrayOutputStream);
LOGGER.debug(new String(byteArrayOutputStream.toByteArray(), Charsets.UTF_8));
JsonObject resultObject = (JsonObject) parser.parse(new InputStreamReader(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()), Charsets.UTF_8));
return resultObject;
} else {
return (JsonObject) parser.parse(new InputStreamReader(resultEntity.getContent(), Charsets.UTF_8));
}
} else {
throw new ReflectorException("Call unsuccessful, status code: " + response.getStatusLine().getStatusCode() + " (" + response.getStatusLine().getReasonPhrase() + "), " + remoteAddress);
}
} finally {
httppost.releaseConnection();
}
} catch (Exception e) {
throw new ReflectorException(e);
}
}
use of org.bimserver.shared.reflector.ReflectorException in project BIMserver by opensourceBIM.
the class NotificationsManager method handleIncoming.
public void handleIncoming(JsonObject request) throws UserException, ConvertException, IOException {
// TODO copied code from JsonHandler
String interfaceName = request.get("interface").getAsString();
String methodName = request.get("method").getAsString();
SService sService = servicesMap.getByName(interfaceName);
if (sService == null) {
sService = servicesMap.getBySimpleName(interfaceName);
}
if (sService == null) {
throw new UserException("No service found with name " + interfaceName);
}
SMethod method = sService.getSMethod(methodName);
if (method == null) {
SMethod alternative = servicesMap.findMethod(methodName);
if (alternative == null) {
throw new UserException("Method " + methodName + " not found on " + interfaceName);
} else {
throw new UserException("Method " + methodName + " not found on " + interfaceName + " (suggestion: " + alternative.getService().getSimpleName() + ")");
}
}
KeyValuePair[] parameters = new KeyValuePair[method.getParameters().size()];
if (request.has("parameters")) {
JsonObject parametersJson = request.getAsJsonObject("parameters");
for (int i = 0; i < method.getParameters().size(); i++) {
SParameter parameter = method.getParameter(i);
if (parametersJson.has(parameter.getName())) {
parameters[i] = new KeyValuePair(parameter.getName(), converter.fromJson(parameter.getType(), parameter.getGenericType(), parametersJson.get(parameter.getName())));
} else {
LOGGER.error("Missing parameters: " + method.getName() + " -> " + parameter.getName());
}
}
} else {
throw new UserException("Missing 'parameters' field");
}
try {
method.invoke(sService.getInterfaceClass(), service, parameters);
} catch (ServiceException e) {
LOGGER.error("", e);
} catch (ReflectorException e) {
LOGGER.error("", e);
}
}
Aggregations