Search in sources :

Example 1 with ConvertException

use of org.bimserver.shared.json.ConvertException in project BIMserver by opensourceBIM.

the class Channel method checkinSync.

public SLongCheckinActionState checkinSync(String baseAddress, String token, long poid, String comment, long deserializerOid, boolean merge, long fileSize, String filename, InputStream inputStream, Long topicId) throws ServerException, UserException {
    String address = baseAddress + "/upload";
    HttpPost httppost = new HttpPost(address);
    try {
        if (topicId == null) {
            topicId = getServiceInterface().initiateCheckin(poid, deserializerOid);
        }
        // TODO find some GzipInputStream variant that _compresses_ instead
        // of _decompresses_ using deflate for now
        InputStreamBody data = new InputStreamBody(new DeflaterInputStream(inputStream), filename);
        MultipartEntityBuilder multipartEntityBuilder = createMultiPart();
        multipartEntityBuilder.addPart("topicId", new StringBody("" + topicId, ContentType.DEFAULT_TEXT));
        multipartEntityBuilder.addPart("token", new StringBody(token, ContentType.DEFAULT_TEXT));
        multipartEntityBuilder.addPart("deserializerOid", new StringBody("" + deserializerOid, ContentType.DEFAULT_TEXT));
        multipartEntityBuilder.addPart("merge", new StringBody("" + merge, ContentType.DEFAULT_TEXT));
        multipartEntityBuilder.addPart("poid", new StringBody("" + poid, ContentType.DEFAULT_TEXT));
        multipartEntityBuilder.addPart("comment", new StringBody("" + comment, ContentType.DEFAULT_TEXT));
        multipartEntityBuilder.addPart("sync", new StringBody("" + true, ContentType.DEFAULT_TEXT));
        multipartEntityBuilder.addPart("compression", new StringBody("deflate", ContentType.DEFAULT_TEXT));
        multipartEntityBuilder.addPart("data", data);
        httppost.setEntity(multipartEntityBuilder.build());
        HttpResponse httpResponse = closeableHttpClient.execute(httppost);
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            ObjectMapper objectMapper = new ObjectMapper();
            InputStreamReader in = new InputStreamReader(httpResponse.getEntity().getContent());
            try {
                ObjectNode result = objectMapper.readValue(in, ObjectNode.class);
                if (result.has("exception")) {
                    ObjectNode exceptionJson = (ObjectNode) result.get("exception");
                    String exceptionType = exceptionJson.get("__type").asText();
                    String message = exceptionJson.has("message") ? exceptionJson.get("message").asText() : "unknown";
                    if (exceptionType.equals(UserException.class.getSimpleName())) {
                        throw new UserException(message);
                    } else if (exceptionType.equals(ServerException.class.getSimpleName())) {
                        throw new ServerException(message);
                    }
                } else {
                    SServicesMap sServicesMap = getSServicesMap();
                    try {
                        return (SLongCheckinActionState) getJsonConverter().fromJson(sServicesMap.getSType("SLongCheckinState"), null, result);
                    } catch (ConvertException e) {
                        throw new ServerException(e);
                    }
                }
            } finally {
                in.close();
            }
        } else {
            throw new ServerException("HTTP Status Code " + httpResponse.getStatusLine().getStatusCode() + " " + httpResponse.getStatusLine().getReasonPhrase());
        }
    } catch (ClientProtocolException e) {
        throw new ServerException(e);
    } catch (IOException e) {
        throw new ServerException(e);
    } catch (PublicInterfaceNotFoundException e) {
        throw new ServerException(e);
    } catch (Exception e) {
        throw new ServerException(e);
    } finally {
        httppost.releaseConnection();
    }
    throw new ServerException("Null result");
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) ServerException(org.bimserver.shared.exceptions.ServerException) InputStreamReader(java.io.InputStreamReader) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ConvertException(org.bimserver.shared.json.ConvertException) DeflaterInputStream(java.util.zip.DeflaterInputStream) SLongCheckinActionState(org.bimserver.interfaces.objects.SLongCheckinActionState) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ChannelConnectionException(org.bimserver.shared.ChannelConnectionException) PublicInterfaceNotFoundException(org.bimserver.shared.exceptions.PublicInterfaceNotFoundException) IOException(java.io.IOException) ConvertException(org.bimserver.shared.json.ConvertException) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) ClientProtocolException(org.apache.http.client.ClientProtocolException) SServicesMap(org.bimserver.shared.meta.SServicesMap) StringBody(org.apache.http.entity.mime.content.StringBody) PublicInterfaceNotFoundException(org.bimserver.shared.exceptions.PublicInterfaceNotFoundException) InputStreamBody(org.apache.http.entity.mime.content.InputStreamBody) UserException(org.bimserver.shared.exceptions.UserException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with ConvertException

use of org.bimserver.shared.json.ConvertException in project BIMserver by opensourceBIM.

the class WebSocketImpl method onMessage.

@OnWebSocketMessage
public void onMessage(String msg) {
    try {
        ObjectNode parse = new ObjectMapper().readValue(msg, ObjectNode.class);
        // LOGGER.info(parse.toString());
        if (parse instanceof ObjectNode) {
            ObjectNode object = (ObjectNode) parse;
            if (object.has("welcome")) {
                String token = socketNotificationsClient.getBimServerClient().getToken();
                session.getRemote().sendString("{\"token\":\"" + token + "\"}");
            } else if (object.has("endpointid")) {
                socketNotificationsClient.setEndpointId(object.get("endpointid").asLong());
                countDownLatch.countDown();
            // LOGGER.info("Counted down to " + countDownLatch.getCount());
            } else {
                try {
                    socketNotificationsClient.handleIncoming((ObjectNode) object.get("request"));
                } catch (UserException e) {
                    LOGGER.error("", e);
                } catch (ConvertException e) {
                    LOGGER.error("", e);
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("", e);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ConvertException(org.bimserver.shared.json.ConvertException) UserException(org.bimserver.shared.exceptions.UserException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) EofException(org.eclipse.jetty.io.EofException) ConvertException(org.bimserver.shared.json.ConvertException) UserException(org.bimserver.shared.exceptions.UserException) OnWebSocketMessage(org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 UserException (org.bimserver.shared.exceptions.UserException)2 ConvertException (org.bimserver.shared.json.ConvertException)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 DeflaterInputStream (java.util.zip.DeflaterInputStream)1 HttpResponse (org.apache.http.HttpResponse)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 HttpPost (org.apache.http.client.methods.HttpPost)1 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)1 InputStreamBody (org.apache.http.entity.mime.content.InputStreamBody)1 StringBody (org.apache.http.entity.mime.content.StringBody)1 SLongCheckinActionState (org.bimserver.interfaces.objects.SLongCheckinActionState)1 ChannelConnectionException (org.bimserver.shared.ChannelConnectionException)1 PublicInterfaceNotFoundException (org.bimserver.shared.exceptions.PublicInterfaceNotFoundException)1 ServerException (org.bimserver.shared.exceptions.ServerException)1 SServicesMap (org.bimserver.shared.meta.SServicesMap)1 EofException (org.eclipse.jetty.io.EofException)1 OnWebSocketMessage (org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage)1