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.sonar.api.server.ws;
021
022import com.google.common.annotations.Beta;
023import java.util.Collection;
024import javax.annotation.CheckForNull;
025
026/**
027 * This class allows a web service to call another web service through the sonar-ws library.
028 * The call is in-process, synchronous and does not involve the HTTP stack.
029 * <p>
030 * Example of a web service that loads some issues:
031 * <pre>
032 * import org.sonar.api.server.ws.RequestHandler;
033 * import org.sonarqube.ws.client.WsClientFactories;
034 *
035 * public class MyRequestHandler implements RequestHandler {
036 *   {@literal @}Override
037 *   public void handle(Request request, Response response) {
038 *     WsClient wsClient = WsClientFactories.getLocal().newClient(request.localConnector());
039 *     SearchWsResponse issues = wsClient.issues().search(new SearchWsRequest());
040 *     // ...
041 *   }
042 * }
043 * </pre>
044 *
045 * It requires to use the sonar-ws library which Maven ids are:
046 * <pre>
047 *   &lt;dependency&gt;
048 *     &lt;groupId&gt;org.sonarsource.sonarqube&lt;/groupId&gt;
049 *     &lt;artifactId&gt;sonar-ws&lt;/artifactId&gt;
050 *   &lt;/dependency&gt;
051 * </pre>
052 * 
053 * @since 5.5
054 */
055@Beta
056public interface LocalConnector {
057
058  LocalResponse call(LocalRequest request);
059
060  interface LocalRequest {
061    /**
062     * URL path, which is the concatenation of controller path and action key, for example "api/issues/search"
063     * @see org.sonar.api.server.ws.WebService.Controller#path
064     * @see org.sonar.api.server.ws.WebService.Action#key
065     */
066    String getPath();
067
068    /**
069     * @see Request#getMediaType()
070     */
071    String getMediaType();
072
073    /**
074     * HTTP method. Possible values are "GET" and "POST"
075     * @see Request#method()
076     */
077    String getMethod();
078
079    /**
080     * @see Request#hasParam(String)
081     */
082    boolean hasParam(String key);
083
084    /**
085     * @see Request#param(String)
086     */
087    @CheckForNull
088    String getParam(String key);
089  }
090
091  interface LocalResponse {
092    /**
093     * @see org.sonar.api.server.ws.Response.Stream#setStatus(int)
094     */
095    int getStatus();
096
097    /**
098     * @see org.sonar.api.server.ws.Response.Stream#setMediaType(String)
099     */
100    String getMediaType();
101
102    /**
103     * Response body
104     */
105    byte[] getBytes();
106
107    /**
108     * HTTP headers
109     * @see Response#setHeader(String, String)
110     */
111    Collection<String> getHeaderNames();
112
113    /**
114     * @see Response#setHeader(String, String)
115     */
116    @CheckForNull
117    String getHeader(String name);
118  }
119
120}