001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     matic
011 */
012package org.nuxeo.ecm.automation.client.jaxrs.spi.marshallers;
013
014import java.io.IOException;
015
016import org.codehaus.jackson.JsonGenerator;
017import org.codehaus.jackson.JsonParser;
018import org.codehaus.jackson.JsonToken;
019import org.nuxeo.ecm.automation.client.Constants;
020import org.nuxeo.ecm.automation.client.RemoteException;
021import org.nuxeo.ecm.automation.client.jaxrs.spi.JsonMarshaller;
022import org.nuxeo.ecm.automation.client.jaxrs.spi.JsonMarshalling;
023
024/**
025 * @author matic
026 */
027public class ExceptionMarshaller implements JsonMarshaller<RemoteException> {
028
029    @Override
030    public String getType() {
031        return "exception";
032    }
033
034    @Override
035    public Class<RemoteException> getJavaType() {
036        return RemoteException.class;
037    }
038
039    public static RemoteException readException(String content) throws IOException {
040        JsonParser jp = JsonMarshalling.getFactory().createJsonParser(content);
041        jp.nextToken(); // skip {
042        return _read(jp);
043    }
044
045    @Override
046    public RemoteException read(JsonParser jp) throws IOException {
047        return _read(jp);
048    }
049
050    public static RemoteException _read(JsonParser jp) throws IOException {
051        int status = 0;
052        String type = null, message = null;
053        Throwable cause = null;
054        JsonToken tok = jp.nextToken();
055        while (tok != null && tok != JsonToken.END_OBJECT) {
056            String key = jp.getCurrentName();
057            tok = jp.nextToken();
058            if ("status".equals(key)) {
059                if (tok == JsonToken.VALUE_NUMBER_INT) {
060                    status = jp.getIntValue();
061                } else {
062                    status = Integer.parseInt(jp.getText());
063                }
064            } else if (Constants.KEY_ENTITY_TYPE.equals(key) || "type".equals(key)) {
065                type = jp.getText();
066            } else if ("message".equals(key)) {
067                message = jp.getText();
068            } else if ("exception".equals(key) || "cause".equals(key)) {
069                cause = jp.readValueAs(Throwable.class);
070            }
071            tok = jp.nextToken();
072        }
073        if (tok == null) {
074            throw new IllegalArgumentException("Unexpected end of stream.");
075        }
076        return new RemoteException(status, type, message, cause);
077    }
078
079    @Override
080    public void write(JsonGenerator jg, Object value) {
081        throw new UnsupportedOperationException();
082    }
083
084}