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