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 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.client.jaxrs.spi;
013
014import static org.nuxeo.ecm.automation.client.Constants.CTYPE_REQUEST_NOCHARSET;
015import static org.nuxeo.ecm.automation.client.Constants.REQUEST_ACCEPT_HEADER;
016import static org.nuxeo.ecm.automation.client.Constants.HEADER_NX_SCHEMAS;
017
018import java.io.IOException;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.nuxeo.ecm.automation.client.AutomationClient;
023import org.nuxeo.ecm.automation.client.LoginInfo;
024import org.nuxeo.ecm.automation.client.OperationRequest;
025import org.nuxeo.ecm.automation.client.Session;
026import org.nuxeo.ecm.automation.client.jaxrs.util.MultipartInput;
027import org.nuxeo.ecm.automation.client.model.Blob;
028import org.nuxeo.ecm.automation.client.model.Blobs;
029import org.nuxeo.ecm.automation.client.model.OperationDocumentation;
030import org.nuxeo.ecm.automation.client.model.OperationInput;
031
032/**
033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
034 */
035public class DefaultSession implements Session {
036
037    protected final AbstractAutomationClient client;
038
039    protected final Connector connector;
040
041    protected final LoginInfo login;
042
043    protected String defaultSchemas = null;
044
045    public DefaultSession(AbstractAutomationClient client, Connector connector, LoginInfo login) {
046        this.client = client;
047        this.connector = connector;
048        this.login = login;
049    }
050
051    @Override
052    public AutomationClient getClient() {
053        return client;
054    }
055
056    public Connector getConnector() {
057        return connector;
058    }
059
060    @Override
061    public LoginInfo getLogin() {
062        return login;
063    }
064
065    @Override
066    public <T> T getAdapter(Class<T> type) {
067        return client.getAdapter(this, type);
068    }
069
070    @Override
071    public String getDefaultSchemas() {
072        return defaultSchemas;
073    }
074
075    @Override
076    public void setDefaultSchemas(String defaultSchemas) {
077        this.defaultSchemas = defaultSchemas;
078    }
079
080    @Override
081    public Object execute(OperationRequest request) throws IOException {
082        Request req;
083        String content = JsonMarshalling.writeRequest(request);
084        String ctype;
085        Object input = request.getInput();
086        if (input instanceof OperationInput && ((OperationInput) input).isBinary()) {
087            MultipartInput mpinput = new MultipartInput();
088            mpinput.setRequest(content);
089            ctype = mpinput.getContentType();
090            if (input instanceof Blob) {
091                Blob blob = (Blob) input;
092                mpinput.setBlob(blob);
093            } else if (input instanceof Blobs) {
094                mpinput.setBlobs((Blobs) input);
095            } else {
096                throw new IllegalArgumentException("Unsupported binary input object: " + input);
097            }
098            req = new Request(Request.POST, request.getUrl(), mpinput);
099        } else {
100            req = new Request(Request.POST, request.getUrl(), content);
101            ctype = CTYPE_REQUEST_NOCHARSET;
102        }
103        // set headers
104        for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
105            req.put(entry.getKey(), entry.getValue());
106        }
107        req.put("Accept", REQUEST_ACCEPT_HEADER);
108        req.put("Content-Type", ctype);
109        if (req.get(HEADER_NX_SCHEMAS) == null && defaultSchemas != null) {
110            req.put(HEADER_NX_SCHEMAS, defaultSchemas);
111        }
112        return connector.execute(req);
113    }
114
115    @Override
116    public Blob getFile(String path) throws IOException {
117        Request req = new Request(Request.GET, path);
118        return (Blob) connector.execute(req);
119    }
120
121    @Override
122    public Blobs getFiles(String path) throws IOException {
123        Request req = new Request(Request.GET, client.getBaseUrl() + path);
124        return (Blobs) connector.execute(req);
125    }
126
127    @Override
128    public OperationRequest newRequest(String id) {
129        return newRequest(id, new HashMap<String, Object>());
130    }
131
132    @Override
133    public OperationRequest newRequest(String id, Map<String, Object> ctx) {
134        OperationDocumentation op = getOperation(id);
135        if (op == null) {
136            throw new IllegalArgumentException("No such operation: " + id);
137        }
138        return new DefaultOperationRequest(this, op, ctx);
139    }
140
141    @Override
142    public OperationDocumentation getOperation(String id) {
143        return client.getRegistry().getOperation(id);
144    }
145
146    @Override
147    public Map<String, OperationDocumentation> getOperations() {
148        return client.getRegistry().getOperations();
149    }
150
151    @Override
152    public void close() {
153        // do nothing
154    }
155}