001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     matic
018 */
019package org.nuxeo.ecm.automation.client.jaxrs.spi.marshallers;
020
021import java.io.IOException;
022
023import org.codehaus.jackson.JsonGenerator;
024import org.codehaus.jackson.JsonParser;
025import org.codehaus.jackson.JsonToken;
026import org.nuxeo.ecm.automation.client.Constants;
027import org.nuxeo.ecm.automation.client.RemoteException;
028import org.nuxeo.ecm.automation.client.jaxrs.spi.JsonMarshaller;
029import org.nuxeo.ecm.automation.client.jaxrs.spi.JsonMarshalling;
030
031/**
032 * @author matic
033 */
034public class ExceptionMarshaller implements JsonMarshaller<RemoteException> {
035
036    @Override
037    public String getType() {
038        return "exception";
039    }
040
041    @Override
042    public Class<RemoteException> getJavaType() {
043        return RemoteException.class;
044    }
045
046    public static RemoteException readException(String content) throws IOException {
047        JsonParser jp = JsonMarshalling.getFactory().createJsonParser(content);
048        jp.nextToken(); // skip {
049        return _read(jp);
050    }
051
052    @Override
053    public RemoteException read(JsonParser jp) throws IOException {
054        return _read(jp);
055    }
056
057    public static RemoteException _read(JsonParser jp) throws IOException {
058        int status = 0;
059        String type = null, message = null;
060        Throwable cause = null;
061        JsonToken tok = jp.nextToken();
062        while (tok != null && tok != JsonToken.END_OBJECT) {
063            String key = jp.getCurrentName();
064            tok = jp.nextToken();
065            if ("status".equals(key)) {
066                if (tok == JsonToken.VALUE_NUMBER_INT) {
067                    status = jp.getIntValue();
068                } else {
069                    status = Integer.parseInt(jp.getText());
070                }
071            } else if (Constants.KEY_ENTITY_TYPE.equals(key) || "type".equals(key)) {
072                type = jp.getText();
073            } else if ("message".equals(key)) {
074                message = jp.getText();
075            } else if ("exception".equals(key) || "cause".equals(key)) {
076                cause = jp.readValueAs(Throwable.class);
077            }
078            tok = jp.nextToken();
079        }
080        if (tok == null) {
081            throw new IllegalArgumentException("Unexpected end of stream.");
082        }
083        return new RemoteException(status, type, message, cause);
084    }
085
086    @Override
087    public void write(JsonGenerator jg, Object value) {
088        throw new UnsupportedOperationException();
089    }
090
091}