Skip to content

Commit dcf517d

Browse files
ericg138alain-kermis-sonarsourceantoine-vinot-sonarsourcejacek-poreda-sonarsource
committed
PLUGINAPI-42 Introduce framework agnostic types for Http extensions
- Use new types for identity providers, deprecate javax methods - Introduce HttpFilter class and deprecate ServletFilter which is using javax.servlet.* - Add new methods required by filters in SonarQube - Deprecate javax.servlet usage in org.sonar.api.security package Co-authored-by: Eric Giffon <eric.giffon@sonarsource.com> Co-authored-by: Alain Kermis <alain.kermis@sonarsource.com> Co-authored-by: Antoine Vinot <antoine.vinot@sonarsource.com> Co-authored-by: Jacek Poreda <jacek.poreda@sonarsource.com>
1 parent f2fff9a commit dcf517d

20 files changed

+1091
-21
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
## 9.16
44

55
* Extension point `org.sonar.api.resources.Language` now supports `filenamePatterns` to detect files' language based on more complex filename patterns than only filename extensions.
6+
* Usage of `javax-servlet-api` is now deprecated in favor of custom, framework agnostic API:
7+
* Replace ~~`org.sonar.api.web.ServletFilter`~~ by `org.sonar.api.server.web.HttpFilter`
8+
* Replace ~~`javax.servlet.http.HttpServletRequest`~~ by `org.sonar.api.server.http.HttpRequest`
9+
* Replace ~~`javax.servlet.http.HttpServletResponse`~~ by `org.sonar.api.server.http.HttpResponse`
10+
* Other added classes: `org.sonar.api.web.FilterChain`, `org.sonar.api.web.UrlPattern` and `org.sonar.api.server.http.Cookie`
611

712
## 9.15
813

@@ -12,4 +17,4 @@
1217
* Utility classes used to test logs have been moved to a separate artifact `org.sonarsource.api.plugin:sonar-plugin-api-test-fixtures` and moved to a new package:
1318
* `org.sonar.api.utils.log.LogTester` &rarr; `org.sonar.api.testfixtures.log.LogTester`
1419
* `org.sonar.api.utils.log.LogTesterJUnit5` &rarr; `org.sonar.api.testfixtures.log.LogTesterJUnit5`
15-
* **Breaking change for tests**: the default log level when using `LogTester` is now `INFO`. This is consistent with the default behavior of Sonar products. If you want to assert `DEBUG` or `TRACE` logs in your tests, you should first change the log level by using for example `logTester.setLevel(Level.DEBUG)`.
20+
* **Breaking change for tests**: the default log level when using `LogTester` is now `INFO`. This is consistent with the default behavior of Sonar products. If you want to assert `DEBUG` or `TRACE` logs in your tests, you should first change the log level by using for example `logTester.setLevel(Level.DEBUG)`.

plugin-api/build.gradle

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

1414
compileOnly libs.jsr305
15-
compileOnly libs.servlet.api
15+
compileOnly libs.javax.servlet.api
1616

1717
testImplementation libs.junit4
1818
testImplementation libs.junit5

plugin-api/src/main/java/org/sonar/api/security/Authenticator.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import javax.servlet.http.HttpServletRequest;
2424
import org.sonar.api.ExtensionPoint;
2525
import org.sonar.api.server.ServerSide;
26+
import org.sonar.api.server.http.HttpRequest;
2627

2728
import static java.util.Objects.requireNonNull;
2829

@@ -44,12 +45,17 @@ public static final class Context {
4445
private String username;
4546
private String password;
4647
private HttpServletRequest request;
48+
private HttpRequest httpRequest;
4749

48-
public Context(@Nullable String username, @Nullable String password, HttpServletRequest request) {
50+
/**
51+
* This class is not meant to be instantiated by plugins, except for tests.
52+
*/
53+
public Context(@Nullable String username, @Nullable String password, HttpRequest httpRequest, HttpServletRequest request) {
4954
requireNonNull(request);
50-
this.request = request;
5155
this.username = username;
5256
this.password = password;
57+
this.httpRequest = httpRequest;
58+
this.request = request;
5359
}
5460

5561
/**
@@ -66,8 +72,19 @@ public String getPassword() {
6672
return password;
6773
}
6874

75+
/**
76+
* @deprecated since 9.16. Use {@link #getHttpRequest()} instead.
77+
*/
78+
@Deprecated(since = "9.16", forRemoval = true)
6979
public HttpServletRequest getRequest() {
7080
return request;
7181
}
82+
83+
/**
84+
* @since 9.16
85+
*/
86+
public HttpRequest getHttpRequest() {
87+
return httpRequest;
88+
}
7289
}
7390
}

plugin-api/src/main/java/org/sonar/api/security/ExternalGroupsProvider.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Collection;
2323
import javax.annotation.CheckForNull;
2424
import javax.servlet.http.HttpServletRequest;
25+
import org.sonar.api.server.http.HttpRequest;
2526

2627
/**
2728
* Note that prefix "do" for names of methods is reserved for future enhancements, thus should not be used in subclasses.
@@ -46,18 +47,34 @@ public Collection<String> doGetGroups(Context context) {
4647
public static final class Context {
4748
private String username;
4849
private HttpServletRequest request;
50+
private HttpRequest httpRequest;
4951

50-
public Context(String username, HttpServletRequest request) {
52+
/**
53+
* This class is not meant to be instantiated by plugins, except for tests.
54+
*/
55+
public Context(String username, HttpRequest httpRequest, HttpServletRequest request) {
5156
this.username = username;
57+
this.httpRequest = httpRequest;
5258
this.request = request;
5359
}
5460

5561
public String getUsername() {
5662
return username;
5763
}
5864

65+
/**
66+
* @deprecated since 9.16. Use {@link #getHttpRequest()} instead.
67+
*/
68+
@Deprecated(since = "9.16", forRemoval = true)
5969
public HttpServletRequest getRequest() {
6070
return request;
6171
}
72+
73+
/**
74+
* @since 9.16
75+
*/
76+
public HttpRequest getHttpRequest() {
77+
return httpRequest;
78+
}
6279
}
6380
}

plugin-api/src/main/java/org/sonar/api/security/ExternalUsersProvider.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import javax.annotation.Nullable;
2323
import javax.servlet.http.HttpServletRequest;
24+
import org.sonar.api.server.http.HttpRequest;
2425

2526
/**
2627
* Note that prefix "do" for names of methods is reserved for future enhancements, thus should not be used in subclasses.
@@ -43,18 +44,34 @@ public UserDetails doGetUserDetails(Context context) {
4344
public static final class Context {
4445
private String username;
4546
private HttpServletRequest request;
47+
private HttpRequest httpRequest;
4648

47-
public Context(@Nullable String username, HttpServletRequest request) {
49+
/**
50+
* This class is not meant to be instantiated by plugins, except for tests.
51+
*/
52+
public Context(@Nullable String username, HttpRequest httpRequest, HttpServletRequest request) {
4853
this.username = username;
54+
this.httpRequest = httpRequest;
4955
this.request = request;
5056
}
5157

5258
public String getUsername() {
5359
return username;
5460
}
5561

62+
/**
63+
* @deprecated since 9.16. Use {@link #getHttpRequest()} instead.
64+
*/
65+
@Deprecated(since = "9.16", forRemoval = true)
5666
public HttpServletRequest getRequest() {
5767
return request;
5868
}
69+
70+
/**
71+
* @since 9.16
72+
*/
73+
public HttpRequest getHttpRequest() {
74+
return httpRequest;
75+
}
5976
}
6077
}

plugin-api/src/main/java/org/sonar/api/server/authentication/BaseIdentityProvider.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import javax.servlet.http.HttpServletRequest;
2323
import javax.servlet.http.HttpServletResponse;
24+
import org.sonar.api.server.http.HttpRequest;
25+
import org.sonar.api.server.http.HttpResponse;
2426

2527
/**
2628
* @since 5.4
@@ -35,16 +37,36 @@ public interface BaseIdentityProvider extends IdentityProvider {
3537

3638
interface Context {
3739

40+
/**
41+
* Get the received HTTP request.
42+
*
43+
* @since 9.16
44+
*/
45+
HttpRequest getHttpRequest();
46+
47+
/**
48+
* Get the HTTP response to send.
49+
*
50+
* @since 9.16
51+
*/
52+
HttpResponse getHttpResponse();
53+
3854
/**
3955
* Get the received HTTP request.
4056
* Note - {@code getRequest().getSession()} must not be used in order to support
4157
* future clustering of web servers without stateful server sessions.
58+
*
59+
* @deprecated since 9.16. Use {@link #getHttpRequest()} instead.
4260
*/
61+
@Deprecated(since = "9.16", forRemoval = true)
4362
HttpServletRequest getRequest();
4463

4564
/**
4665
* Get the HTTP response to send
66+
*
67+
* @deprecated since 9.16. Use {@link #getHttpResponse()} instead.
4768
*/
69+
@Deprecated(since = "9.16", forRemoval = true)
4870
HttpServletResponse getResponse();
4971

5072
/**

plugin-api/src/main/java/org/sonar/api/server/authentication/OAuth2IdentityProvider.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import javax.servlet.http.HttpServletRequest;
2323
import javax.servlet.http.HttpServletResponse;
24+
import org.sonar.api.server.http.HttpRequest;
25+
import org.sonar.api.server.http.HttpResponse;
2426

2527
/**
2628
* @since 5.4
@@ -45,16 +47,36 @@ interface OAuth2Context {
4547
*/
4648
String getCallbackUrl();
4749

50+
/**
51+
* Get the received HTTP request.
52+
*
53+
* @since 9.16
54+
*/
55+
HttpRequest getHttpRequest();
56+
57+
/**
58+
* Get the HTTP response to send.
59+
*
60+
* @since 9.16
61+
*/
62+
HttpResponse getHttpResponse();
63+
4864
/**
4965
* Get the received HTTP request.
5066
* Note - {@code getRequest().getSession()} must not be used in order to support
5167
* future clustering of web servers without stateful server sessions.
68+
*
69+
* @deprecated since 9.16. Use {@link #getHttpRequest()} instead.
5270
*/
71+
@Deprecated(since = "9.16", forRemoval = true)
5372
HttpServletRequest getRequest();
5473

5574
/**
5675
* Get the HTTP response to send
76+
*
77+
* @deprecated since 9.16. Use {@link #getHttpResponse()} instead.
5778
*/
79+
@Deprecated(since = "9.16", forRemoval = true)
5880
HttpServletResponse getResponse();
5981
}
6082

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
/**
23+
* Framework-agnostic definition of a cookie.
24+
* Creates a cookie, a small amount of information sent by a servlet to
25+
* a Web browser, saved by the browser, and later sent back to the server.
26+
* A cookie's value can uniquely
27+
* identify a client, so cookies are commonly used for session management.
28+
*
29+
* @since 9.16
30+
**/
31+
public interface Cookie {
32+
33+
/**
34+
* Returns the name of the cookie. The name cannot be changed after
35+
* creation.
36+
*/
37+
String getName();
38+
39+
/**
40+
* Gets the current value of this Cookie.
41+
*/
42+
String getValue();
43+
44+
/**
45+
* Returns the path on the server
46+
* to which the browser returns this cookie. The
47+
* cookie is visible to all subpaths on the server.
48+
*/
49+
String getPath();
50+
51+
/**
52+
* Returns <code>true</code> if the browser is sending cookies
53+
* only over a secure protocol, or <code>false</code> if the
54+
* browser can send cookies using any protocol.
55+
*/
56+
boolean isSecure();
57+
58+
/**
59+
* Checks whether this Cookie has been marked as <i>HttpOnly</i>.
60+
*/
61+
boolean isHttpOnly();
62+
63+
/**
64+
* Gets the maximum age in seconds of this Cookie.
65+
*/
66+
int getMaxAge();
67+
68+
69+
}

0 commit comments

Comments
 (0)