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