Skip to content

Commit 98c911e

Browse files
committed
PLUGINAPI-42 Create new custom types for http request and response
1 parent 717a0b0 commit 98c911e

File tree

8 files changed

+516
-3
lines changed

8 files changed

+516
-3
lines changed

plugin-api/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ dependencies {
1212
implementation project(':check-api')
1313

1414
compileOnly libs.jsr305
15-
compileOnly libs.servlet.api
15+
compileOnly libs.javax.servlet.api
16+
compileOnly libs.jakarta.servlet.api
1617

1718
testImplementation libs.junit4
1819
testImplementation libs.junit5
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Sonar Plugin API
3+
* Copyright (C) 2009-2023 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.api.server.http;
21+
22+
import java.util.Enumeration;
23+
24+
/**
25+
* Framework-agnostic definition of an HTTP request.
26+
*
27+
* @since 9.16
28+
*/
29+
public interface HttpRequest {
30+
31+
32+
/**
33+
* Returns the port number to which the request was sent.
34+
*/
35+
int getServerPort();
36+
37+
/**
38+
* Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.
39+
*/
40+
boolean isSecure();
41+
42+
/**
43+
* Returns the name of the scheme used to make this request, for example, http, https, or ftp.
44+
*/
45+
String getScheme();
46+
47+
/**
48+
* Returns the host name of the server to which the request was sent.
49+
*/
50+
String getServerName();
51+
52+
/**
53+
* Returns the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path,
54+
* but it does not include query string parameters.
55+
*/
56+
String getRequestURL();
57+
58+
/**
59+
* Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.
60+
*/
61+
String getRequestURI();
62+
63+
/**
64+
* Returns the query string that is contained in the request URL after the path. This method returns null if the URL does not have a
65+
* query string.
66+
*/
67+
String getQueryString();
68+
69+
/**
70+
* Returns the value of a request parameter as a String, or null if the parameter does not exist.
71+
* You should only use this method when you are sure the parameter has only one value. If the parameter might have more than one value,
72+
* use {@link #getParameterValues}.
73+
*/
74+
String getParameter(String name);
75+
76+
/**
77+
* Returns an array containing all of the values the given request parameter has, or null if the parameter does not exist.
78+
*/
79+
String[] getParameterValues(String name);
80+
81+
/**
82+
* Returns the value of the specified request header as a String. If the request did not include a header of the specified name, this
83+
* method returns null. If there are multiple headers with the same name, this method returns the first head in the request.
84+
*/
85+
String getHeader(String name);
86+
87+
/**
88+
* Returns all the values of the specified request header as an Enumeration of String objects.
89+
*/
90+
Enumeration<String> getHeaders(String name);
91+
92+
/**
93+
* Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.
94+
*/
95+
String getMethod();
96+
97+
/**
98+
* Returns the raw request object from the Servlet API that matches this request.
99+
*/
100+
Object getRawRequest();
101+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Sonar Plugin API
3+
* Copyright (C) 2009-2023 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.api.server.http;
21+
22+
import java.io.IOException;
23+
import java.util.Collection;
24+
25+
/**
26+
* Framework-agnostic definition of an HTTP response.
27+
*
28+
* @since 9.16
29+
*/
30+
public interface HttpResponse {
31+
32+
/**
33+
* Adds a response header with the given name and value. This method allows response headers to have multiple values.
34+
*/
35+
void addHeader(String name, String value);
36+
37+
/**
38+
* Gets the value of the response header with the given name.
39+
* If a response header with the given name exists and contains multiple values, the value that was added first will be returned.
40+
*/
41+
String getHeader(String name);
42+
43+
/**
44+
* Gets the values of the response header with the given name.
45+
*/
46+
Collection<String> getHeaders(String name);
47+
48+
/**
49+
* Sets the status code for this response.
50+
*/
51+
void setStatus(int sc);
52+
53+
/**
54+
* Gets the current status code of this response.
55+
*/
56+
int getStatus();
57+
58+
/**
59+
* Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer.
60+
* The buffer will be replaced with the data set by this method.
61+
*/
62+
void sendRedirect(String location) throws IOException;
63+
64+
/**
65+
* Returns the raw response object from the Servlet API that matches this response.
66+
*/
67+
Object getRawResponse();
68+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Sonar Plugin API
3+
* Copyright (C) 2009-2023 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.api.server.http;
21+
22+
import jakarta.servlet.http.HttpServletRequest;
23+
import java.util.Enumeration;
24+
25+
/**
26+
* Implementation of {@link HttpRequest} based on a delegate of {@link HttpServletRequest} from the Jakarta Servlet API.
27+
*/
28+
public class JakartaHttpRequest implements HttpRequest {
29+
30+
private final HttpServletRequest delegate;
31+
32+
public JakartaHttpRequest(HttpServletRequest delegate) {
33+
this.delegate = delegate;
34+
}
35+
36+
@Override
37+
public int getServerPort() {
38+
return delegate.getServerPort();
39+
}
40+
41+
@Override
42+
public boolean isSecure() {
43+
return delegate.isSecure();
44+
}
45+
46+
@Override
47+
public String getScheme() {
48+
return delegate.getScheme();
49+
}
50+
51+
@Override
52+
public String getServerName() {
53+
return delegate.getServerName();
54+
}
55+
56+
@Override
57+
public String getRequestURL() {
58+
return delegate.getRequestURL().toString();
59+
}
60+
61+
@Override
62+
public String getRequestURI() {
63+
return delegate.getRequestURI();
64+
}
65+
66+
@Override
67+
public String getQueryString() {
68+
return delegate.getQueryString();
69+
}
70+
71+
@Override
72+
public String getParameter(String name) {
73+
return delegate.getParameter(name);
74+
}
75+
76+
@Override
77+
public String[] getParameterValues(String name) {
78+
return delegate.getParameterValues(name);
79+
}
80+
81+
@Override
82+
public String getHeader(String name) {
83+
return delegate.getHeader(name);
84+
}
85+
86+
@Override
87+
public Enumeration<String> getHeaders(String name) {
88+
return delegate.getHeaders(name);
89+
}
90+
91+
@Override
92+
public String getMethod() {
93+
return delegate.getMethod();
94+
}
95+
96+
@Override
97+
public HttpServletRequest getRawRequest() {
98+
return delegate;
99+
}
100+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Sonar Plugin API
3+
* Copyright (C) 2009-2023 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.api.server.http;
21+
22+
import jakarta.servlet.http.HttpServletResponse;
23+
import java.io.IOException;
24+
import java.util.Collection;
25+
26+
/**
27+
* Implementation of {@link HttpResponse} based on a delegate of {@link HttpServletResponse} from the Jakarta Servlet API.
28+
*/
29+
public class JakartaHttpResponse implements HttpResponse {
30+
31+
private final HttpServletResponse delegate;
32+
33+
public JakartaHttpResponse(HttpServletResponse delegate) {
34+
this.delegate = delegate;
35+
}
36+
37+
@Override
38+
public void addHeader(String name, String value) {
39+
delegate.addHeader(name, value);
40+
}
41+
42+
@Override
43+
public String getHeader(String name) {
44+
return delegate.getHeader(name);
45+
}
46+
47+
@Override
48+
public Collection<String> getHeaders(String name) {
49+
return delegate.getHeaders(name);
50+
}
51+
52+
@Override
53+
public void setStatus(int sc) {
54+
delegate.setStatus(sc);
55+
}
56+
57+
@Override
58+
public int getStatus() {
59+
return delegate.getStatus();
60+
}
61+
62+
@Override
63+
public void sendRedirect(String location) throws IOException {
64+
delegate.sendRedirect(location);
65+
}
66+
67+
@Override
68+
public HttpServletResponse getRawResponse() {
69+
return delegate;
70+
}
71+
}

0 commit comments

Comments
 (0)