001/* 002 * SonarQube 003 * Copyright (C) 2009-2016 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020package org.sonarqube.ws.client; 021 022import com.google.common.base.Joiner; 023import com.google.protobuf.Message; 024import com.google.protobuf.Parser; 025import java.io.InputStream; 026import java.util.List; 027import javax.annotation.CheckForNull; 028import javax.annotation.Nullable; 029import org.apache.commons.io.IOUtils; 030import org.sonarqube.ws.MediaTypes; 031 032import static com.google.common.base.Preconditions.checkArgument; 033import static com.google.common.base.Strings.isNullOrEmpty; 034 035public abstract class BaseService { 036 037 private static final Joiner MULTI_VALUES_JOINER = Joiner.on(","); 038 039 private final WsConnector wsConnector; 040 protected final String controller; 041 042 public BaseService(WsConnector wsConnector, String controllerPath) { 043 checkArgument(!isNullOrEmpty(controllerPath)); 044 this.wsConnector = wsConnector; 045 this.controller = controllerPath; 046 } 047 048 protected <T extends Message> T call(BaseRequest request, Parser<T> parser) { 049 request.setMediaType(MediaTypes.PROTOBUF); 050 WsResponse response = call(request); 051 return convert(response, parser); 052 } 053 054 protected WsResponse call(WsRequest request) { 055 return wsConnector.call(request).failIfNotSuccessful(); 056 } 057 058 public <T extends Message> T convert(WsResponse response, Parser<T> parser) { 059 try (InputStream byteStream = response.contentStream()) { 060 byte[] bytes = IOUtils.toByteArray(byteStream); 061 // HTTP header "Content-Type" is not verified. It may be different than protobuf. 062 return parser.parseFrom(bytes); 063 } catch (Exception e) { 064 throw new IllegalStateException("Fail to parse protobuf response of " + response.requestUrl(), e); 065 } 066 } 067 068 protected String path(String action) { 069 return String.format("%s/%s", controller, action); 070 } 071 072 @CheckForNull 073 protected static String inlineMultipleParamValue(@Nullable List<String> values) { 074 return values == null ? null : MULTI_VALUES_JOINER.join(values); 075 } 076}