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