001/*
002 * (C) Copyright 2013 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 GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thomas Roger
016 */
017
018package org.nuxeo.ecm.automation.client.rest.api;
019
020import static javax.ws.rs.core.HttpHeaders.CONTENT_TYPE;
021import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
022import static org.nuxeo.ecm.automation.client.Constants.CTYPE_ENTITY;
023import static org.nuxeo.ecm.automation.client.Constants.HEADER_NX_SCHEMAS;
024import static org.nuxeo.ecm.automation.client.rest.api.RequestType.POSTREQUEST;
025
026import java.util.ArrayList;
027import java.util.HashMap;
028import java.util.List;
029import java.util.Map;
030
031import javax.ws.rs.core.MultivaluedMap;
032
033import org.apache.commons.lang.StringUtils;
034import org.nuxeo.ecm.automation.client.AutomationException;
035import org.nuxeo.ecm.automation.client.Constants;
036
037import com.sun.jersey.api.client.ClientResponse;
038import com.sun.jersey.api.client.WebResource;
039import com.sun.jersey.core.util.MultivaluedMapImpl;
040
041/**
042 * A REST request on Nuxeo REST API.
043 *
044 * @since 5.8
045 */
046public class RestRequest {
047
048    protected final WebResource service;
049
050    protected final String path;
051
052    protected RequestType requestType = RequestType.GET;
053
054    protected String data;
055
056    protected String repositoryName;
057
058    protected List<String> schemas = new ArrayList<String>();
059
060    protected Map<String, Object> headers = new HashMap<String, Object>();
061
062    protected MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
063
064    public RestRequest(WebResource service, String path) {
065        this.service = service;
066        this.path = path;
067    }
068
069    public RestRequest requestType(RequestType requestType) {
070        this.requestType = requestType;
071        return this;
072    }
073
074    public RestRequest data(String data) {
075        this.data = data;
076        return this;
077    }
078
079    public RestRequest repositoryName(String repositoryName) {
080        this.repositoryName = repositoryName;
081        return this;
082    }
083
084    public RestRequest schema(String schema) {
085        schemas.add(schema);
086        return this;
087    }
088
089    public RestRequest schemas(List<String> schemas) {
090        this.schemas.addAll(schemas);
091        return this;
092    }
093
094    public RestRequest header(String key, Object value) {
095        this.headers.put(key, value);
096        return this;
097    }
098
099    public RestRequest headers(Map<String, Object> headers) {
100        this.headers.putAll(headers);
101        return this;
102    }
103
104    public RestRequest queryParam(String key, String value) {
105        queryParams.add(key, value);
106        return this;
107    }
108
109    public RestRequest queryParams(MultivaluedMap<String, String> queryParams) {
110        this.queryParams.putAll(queryParams);
111        return this;
112    }
113
114    public String getPath() {
115        return path;
116    }
117
118    public RequestType getRequestType() {
119        return requestType;
120    }
121
122    public String getData() {
123        return data;
124    }
125
126    public String getRepositoryName() {
127        return repositoryName;
128    }
129
130    public List<String> getSchemas() {
131        return schemas;
132    }
133
134    public Map<String, Object> getHeaders() {
135        return headers;
136    }
137
138    public MultivaluedMap<String, String> getQueryParams() {
139        return queryParams;
140    }
141
142    public RestResponse execute() {
143        WebResource wr = service;
144        if (!StringUtils.isBlank(repositoryName)) {
145            wr = wr.path("repo").path(repositoryName);
146        }
147        wr = wr.path(path);
148        if (queryParams != null && !queryParams.isEmpty()) {
149            wr = wr.queryParams(queryParams);
150        }
151
152        WebResource.Builder builder = wr.accept(APPLICATION_JSON);
153        for (Map.Entry<String, Object> header : headers.entrySet()) {
154            builder.header(header.getKey(), header.getValue());
155        }
156
157        if (!schemas.isEmpty()) {
158            String documentPropertiesHeader = StringUtils.join(schemas, ",");
159            builder.header(HEADER_NX_SCHEMAS, documentPropertiesHeader);
160        }
161
162        if (requestType == POSTREQUEST) {
163            builder.header(CONTENT_TYPE, Constants.CTYPE_REQUEST);
164        } else {
165            builder.header(CONTENT_TYPE, CTYPE_ENTITY);
166        }
167
168        ClientResponse response;
169        switch (requestType) {
170        case GET:
171            response = builder.get(ClientResponse.class);
172            break;
173        case POST:
174        case POSTREQUEST:
175            response = builder.post(ClientResponse.class, data);
176            break;
177        case PUT:
178            response = builder.put(ClientResponse.class, data);
179            break;
180        case DELETE:
181            response = builder.delete(ClientResponse.class, data);
182            break;
183        default:
184            throw new AutomationException();
185        }
186
187        return new RestResponse(response);
188    }
189}