001/*
002 * (C) Copyright 2015 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 *      André Justo
018 */
019
020package org.nuxeo.ecm.media.publishing.wistia.rest;
021
022import com.sun.jersey.api.client.ClientResponse;
023import com.sun.jersey.api.client.WebResource;
024import com.sun.jersey.core.util.MultivaluedMapImpl;
025import com.sun.jersey.multipart.MultiPart;
026
027import javax.ws.rs.core.MediaType;
028import javax.ws.rs.core.MultivaluedMap;
029import java.util.HashMap;
030import java.util.Map;
031
032public class RestRequest {
033
034    protected WebResource service;
035
036    protected String path;
037
038    protected RequestType requestType = RequestType.GET;
039
040    protected String data;
041
042    protected Map<String, Object> headers = new HashMap<String, Object>();
043
044    protected MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
045
046    protected String contentType = MediaType.APPLICATION_JSON;
047
048    public RestRequest(WebResource service, String path) {
049        this.service = service;
050        this.path = path;
051    }
052
053    public RestRequest header(String key, String value) {
054        this.headers.put(key, value);
055        return this;
056    }
057
058    public RestRequest headers(Map<String, Object> headers) {
059        this.headers = headers;
060        return this;
061    }
062
063    public RestRequest queryParam(String key, String value) {
064        this.queryParams.add(key, value);
065        return this;
066    }
067
068    public RestRequest queryParams(MultivaluedMap<String, String> queryParams) {
069        if (queryParams != null) {
070            this.queryParams = queryParams;
071        }
072        return this;
073    }
074
075    public RestRequest requestType(RequestType requestType) {
076        this.requestType = requestType;
077        return this;
078    }
079
080    public RestRequest contentType(String contentType) {
081        this.contentType = contentType;
082        return this;
083    }
084
085    public String getPath() {
086        return this.path;
087    }
088
089    public RequestType getRequestType() {
090        return this.requestType;
091    }
092
093    public String getData() {
094        return this.data;
095    }
096
097    public Map<String, Object> getHeaders() {
098        return this.headers;
099    }
100
101    public MultivaluedMap<String, String> getQueryParams() {
102        return this.queryParams;
103    }
104
105    public String getContentType() {
106        return this.contentType;
107    }
108
109    public RestResponse execute(MultiPart multiPart) {
110        WebResource wr = service;
111        wr = wr.path(path);
112        if (queryParams != null && !queryParams.isEmpty()) {
113            wr = wr.queryParams(queryParams);
114        }
115
116        WebResource.Builder builder = wr.type(contentType);
117        for (Map.Entry<String, Object> header : headers.entrySet()) {
118            builder.header(header.getKey(), header.getValue());
119        }
120
121        ClientResponse response = null;
122        switch (requestType) {
123            case GET:
124                response = builder.get(ClientResponse.class);
125                break;
126            case POST:
127                if (multiPart != null) {
128                    response = builder.post(ClientResponse.class, multiPart);
129                }
130                else {
131                    response = builder.post(ClientResponse.class, data);
132                }
133                break;
134            case PUT:
135                response = builder.put(ClientResponse.class, data);
136                break;
137            case DELETE:
138                response = builder.delete(ClientResponse.class, data);
139                break;
140        }
141        return new RestResponse(response);
142    }
143
144    public RestResponse execute() {
145        return execute(null);
146    }
147}