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 = Request.buildMultipartInput(input, content);
095            req = new Request(Request.POST, request.getUrl(), mpinput);
096            ctype = mpinput.getContentType();
097        } else {
098            req = new Request(Request.POST, request.getUrl(), content);
099            ctype = CTYPE_REQUEST_NOCHARSET;
100        }
101        // set headers
102        for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
103            req.put(entry.getKey(), entry.getValue());
104        }
105        req.put("Accept", REQUEST_ACCEPT_HEADER);
106        req.put("Content-Type", ctype);
107        if (req.get(HEADER_NX_SCHEMAS) == null && defaultSchemas != null) {
108            req.put(HEADER_NX_SCHEMAS, defaultSchemas);
109        }
110        return connector.execute(req);
111    }
112
113    @Override
114    public Blob getFile(String path) throws IOException {
115        Request req = new Request(Request.GET, path);
116        return (Blob) connector.execute(req);
117    }
118
119    @Override
120    public Blobs getFiles(String path) throws IOException {
121        Request req = new Request(Request.GET, client.getBaseUrl() + path);
122        return (Blobs) connector.execute(req);
123    }
124
125    @Override
126    public OperationRequest newRequest(String id) {
127        return newRequest(id, new HashMap<String, Object>());
128    }
129
130    @Override
131    public OperationRequest newRequest(String id, Map<String, Object> ctx) {
132        OperationDocumentation op = getOperation(id);
133        if (op == null) {
134            throw new IllegalArgumentException("No such operation: " + id);
135        }
136        return new DefaultOperationRequest(this, op, ctx);
137    }
138
139    @Override
140    public OperationDocumentation getOperation(String id) {
141        return client.getRegistry().getOperation(id);
142    }
143
144    @Override
145    public Map<String, OperationDocumentation> getOperations() {
146        return client.getRegistry().getOperations();
147    }
148
149    @Override
150    public void close() {
151        // do nothing
152    }
153}