diff options
author | Antoine Vinot <antoine.vinot@sonarsource.com> | 2023-04-13 16:57:56 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-04-24 20:04:23 +0000 |
commit | 68977b2bedc95c738b58bc8be83a00d21e13c32e (patch) | |
tree | 3c77243a1b04651f6cb7031e825901e40a37df79 /server/sonar-webserver-api | |
parent | f6aef43f6848a27861171cc8e757085326a334f2 (diff) | |
download | sonarqube-68977b2bedc95c738b58bc8be83a00d21e13c32e.tar.gz sonarqube-68977b2bedc95c738b58bc8be83a00d21e13c32e.zip |
SONAR-19045 Migrate from javaxi.servlet to framework agnostic plugin api classes
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>
Diffstat (limited to 'server/sonar-webserver-api')
4 files changed, 475 insertions, 0 deletions
diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/http/JavaxHttpRequest.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/http/JavaxHttpRequest.java new file mode 100644 index 00000000000..08332a24c0f --- /dev/null +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/http/JavaxHttpRequest.java @@ -0,0 +1,183 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.http; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.Arrays; +import java.util.Enumeration; +import javax.servlet.http.HttpServletRequest; +import org.sonar.api.server.http.Cookie; +import org.sonar.api.server.http.HttpRequest; + +/** + * Implementation of {@link HttpRequest} based on a delegate of {@link HttpServletRequest} from the Javax Servlet API. + */ +public class JavaxHttpRequest implements HttpRequest { + + private final HttpServletRequest delegate; + + public JavaxHttpRequest(HttpServletRequest delegate) { + this.delegate = delegate; + } + + public HttpServletRequest getDelegate() { + return delegate; + } + + @Override + public int getServerPort() { + return delegate.getServerPort(); + } + + @Override + public boolean isSecure() { + return delegate.isSecure(); + } + + @Override + public String getScheme() { + return delegate.getScheme(); + } + + @Override + public String getServerName() { + return delegate.getServerName(); + } + + @Override + public String getRequestURL() { + return delegate.getRequestURL().toString(); + } + + @Override + public String getRequestURI() { + return delegate.getRequestURI(); + } + + @Override + public String getQueryString() { + return delegate.getQueryString(); + } + + @Override + public String getContextPath() { + return delegate.getContextPath(); + } + + @Override + public String getParameter(String name) { + return delegate.getParameter(name); + } + + @Override + public String[] getParameterValues(String name) { + return delegate.getParameterValues(name); + } + + @Override + public String getHeader(String name) { + return delegate.getHeader(name); + } + + @Override + public Enumeration<String> getHeaderNames() { + return delegate.getHeaderNames(); + } + + @Override + public Enumeration<String> getHeaders(String name) { + return delegate.getHeaders(name); + } + + @Override + public String getMethod() { + return delegate.getMethod(); + } + + @Override + public String getRemoteAddr() { + return delegate.getRemoteAddr(); + } + + @Override + public void setAttribute(String name, Object value) { + delegate.setAttribute(name, value); + } + + @Override + public String getServletPath() { + return delegate.getServletPath(); + } + + @Override + public BufferedReader getReader() throws IOException { + return delegate.getReader(); + } + + @Override + public Cookie[] getCookies() { + javax.servlet.http.Cookie[] cookies = delegate.getCookies(); + if (cookies != null) { + return Arrays.stream(cookies) + .map(JavaxCookie::new) + .toArray(Cookie[]::new); + } + return new Cookie[0]; + } + + public static class JavaxCookie implements Cookie { + private final javax.servlet.http.Cookie delegate; + + public JavaxCookie(javax.servlet.http.Cookie delegate) { + this.delegate = delegate; + } + + @Override + public String getName() { + return delegate.getName(); + } + + @Override + public String getValue() { + return delegate.getValue(); + } + + @Override + public String getPath() { + return delegate.getPath(); + } + + @Override + public boolean isSecure() { + return delegate.getSecure(); + } + + @Override + public boolean isHttpOnly() { + return delegate.isHttpOnly(); + } + + @Override + public int getMaxAge() { + return delegate.getMaxAge(); + } + } +} diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/http/JavaxHttpResponse.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/http/JavaxHttpResponse.java new file mode 100644 index 00000000000..37f7cf9c977 --- /dev/null +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/http/JavaxHttpResponse.java @@ -0,0 +1,109 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.http; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.util.Collection; +import javax.servlet.http.HttpServletResponse; +import org.sonar.api.server.http.Cookie; +import org.sonar.api.server.http.HttpResponse; + +/** + * Implementation of {@link HttpResponse} based on a delegate of {@link HttpServletResponse} from the Javax Servlet API. + */ +public class JavaxHttpResponse implements HttpResponse { + + private final HttpServletResponse delegate; + + public JavaxHttpResponse(HttpServletResponse delegate) { + this.delegate = delegate; + } + + public HttpServletResponse getDelegate() { + return delegate; + } + + @Override + public void addHeader(String name, String value) { + delegate.addHeader(name, value); + } + + @Override + public String getHeader(String name) { + return delegate.getHeader(name); + } + + @Override + public Collection<String> getHeaders(String name) { + return delegate.getHeaders(name); + } + + @Override + public void setStatus(int status) { + delegate.setStatus(status); + } + + @Override + public int getStatus() { + return delegate.getStatus(); + } + + @Override + public void setContentType(String contentType) { + delegate.setContentType(contentType); + } + + @Override + public PrintWriter getWriter() throws IOException { + return delegate.getWriter(); + } + + @Override + public void setHeader(String name, String value) { + delegate.setHeader(name, value); + } + + @Override + public void sendRedirect(String location) throws IOException { + delegate.sendRedirect(location); + } + + @Override + public void addCookie(Cookie cookie) { + javax.servlet.http.Cookie javaxCookie = new javax.servlet.http.Cookie(cookie.getName(), cookie.getValue()); + javaxCookie.setPath(cookie.getPath()); + javaxCookie.setSecure(cookie.isSecure()); + javaxCookie.setHttpOnly(cookie.isHttpOnly()); + javaxCookie.setMaxAge(cookie.getMaxAge()); + delegate.addCookie(javaxCookie); + } + + @Override + public OutputStream getOutputStream() throws IOException { + return delegate.getOutputStream(); + } + + @Override + public void setCharacterEncoding(String charset) { + delegate.setCharacterEncoding(charset); + } +} diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/http/JavaxHttpRequestTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/http/JavaxHttpRequestTest.java new file mode 100644 index 00000000000..1415dd43a71 --- /dev/null +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/http/JavaxHttpRequestTest.java @@ -0,0 +1,106 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.http; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.Collections; +import java.util.Enumeration; +import javax.servlet.http.HttpServletRequest; +import org.junit.Test; +import org.sonar.api.server.http.Cookie; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class JavaxHttpRequestTest { + + @Test + public void delegate_methods() throws IOException { + HttpServletRequest requestMock = mock(HttpServletRequest.class); + Enumeration<String> enumeration = Collections.enumeration(Collections.emptySet()); + when(requestMock.getHeaderNames()).thenReturn(enumeration); + when(requestMock.getRemoteAddr()).thenReturn("192.168.0.1"); + when(requestMock.getServletPath()).thenReturn("/servlet-path"); + BufferedReader bufferedReader = mock(BufferedReader.class); + when(requestMock.getReader()).thenReturn(bufferedReader); + javax.servlet.http.Cookie[] cookies = new javax.servlet.http.Cookie[0]; + when(requestMock.getCookies()).thenReturn(cookies); + when(requestMock.getServerPort()).thenReturn(80); + when(requestMock.isSecure()).thenReturn(true); + when(requestMock.getScheme()).thenReturn("https"); + when(requestMock.getServerName()).thenReturn("hostname"); + when(requestMock.getRequestURL()).thenReturn(new StringBuffer("https://hostname:80/path")); + when(requestMock.getRequestURI()).thenReturn("/path"); + when(requestMock.getQueryString()).thenReturn("param1=value1"); + when(requestMock.getContextPath()).thenReturn("/path"); + when(requestMock.getMethod()).thenReturn("POST"); + when(requestMock.getParameter("param1")).thenReturn("value1"); + when(requestMock.getParameterValues("param1")).thenReturn(new String[] {"value1"}); + when(requestMock.getHeader("header1")).thenReturn("hvalue1"); + Enumeration<String> headers = mock(Enumeration.class); + when(requestMock.getHeaders("header1")).thenReturn(headers); + + JavaxHttpRequest underTest = new JavaxHttpRequest(requestMock); + + assertThat(underTest.getDelegate()).isSameAs(requestMock); + assertThat(underTest.getServerPort()).isEqualTo(80); + assertThat(underTest.isSecure()).isTrue(); + assertThat(underTest.getScheme()).isEqualTo("https"); + assertThat(underTest.getServerName()).isEqualTo("hostname"); + assertThat(underTest.getRequestURL()).isEqualTo("https://hostname:80/path"); + assertThat(underTest.getRequestURI()).isEqualTo("/path"); + assertThat(underTest.getQueryString()).isEqualTo("param1=value1"); + assertThat(underTest.getContextPath()).isEqualTo("/path"); + assertThat(underTest.getMethod()).isEqualTo("POST"); + assertThat(underTest.getParameter("param1")).isEqualTo("value1"); + assertThat(underTest.getParameterValues("param1")).containsExactly("value1"); + assertThat(underTest.getHeader("header1")).isEqualTo("hvalue1"); + assertThat(underTest.getHeaders("header1")).isEqualTo(headers); + assertThat(underTest.getHeaderNames()).isEqualTo(enumeration); + assertThat(underTest.getRemoteAddr()).isEqualTo("192.168.0.1"); + assertThat(underTest.getServletPath()).isEqualTo("/servlet-path"); + assertThat(underTest.getReader()).isEqualTo(bufferedReader); + assertThat(underTest.getCookies()).isEqualTo(cookies); + + underTest.setAttribute("name", "value"); + verify(requestMock).setAttribute("name", "value"); + } + + @Test + public void delegate_methods_for_cookie() { + javax.servlet.http.Cookie mockCookie = new javax.servlet.http.Cookie("name", "value"); + mockCookie.setSecure(true); + mockCookie.setPath("path"); + mockCookie.setHttpOnly(true); + mockCookie.setMaxAge(100); + + Cookie cookie = new JavaxHttpRequest.JavaxCookie(mockCookie); + assertThat(cookie.getName()).isEqualTo("name"); + assertThat(cookie.getValue()).isEqualTo("value"); + assertThat(cookie.getPath()).isEqualTo("path"); + assertThat(cookie.isSecure()).isTrue(); + assertThat(cookie.isHttpOnly()).isTrue(); + assertThat(cookie.getMaxAge()).isEqualTo(100); + } + +} diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/http/JavaxHttpResponseTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/http/JavaxHttpResponseTest.java new file mode 100644 index 00000000000..8c1ea1717b0 --- /dev/null +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/http/JavaxHttpResponseTest.java @@ -0,0 +1,77 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.http; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.List; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import org.junit.Test; +import org.sonar.api.server.http.Cookie; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class JavaxHttpResponseTest { + + @Test + public void delegate_methods() throws IOException { + HttpServletResponse responseMock = mock(HttpServletResponse.class); + when(responseMock.getHeader("h1")).thenReturn("hvalue1"); + when(responseMock.getHeaders("h1")).thenReturn(List.of("hvalue1")); + when(responseMock.getStatus()).thenReturn(200); + ServletOutputStream outputStream = mock(ServletOutputStream.class); + when(responseMock.getOutputStream()).thenReturn(outputStream); + PrintWriter writer = mock(PrintWriter.class); + when(responseMock.getWriter()).thenReturn(writer); + + JavaxHttpResponse underTest = new JavaxHttpResponse(responseMock); + + assertThat(underTest.getDelegate()).isSameAs(responseMock); + assertThat(underTest.getHeader("h1")).isEqualTo("hvalue1"); + assertThat(underTest.getHeaders("h1")).asList().containsExactly("hvalue1"); + assertThat(underTest.getStatus()).isEqualTo(200); + assertThat(underTest.getWriter()).isEqualTo(writer); + assertThat(underTest.getOutputStream()).isEqualTo(outputStream); + + underTest.addHeader("h2", "hvalue2"); + underTest.setHeader("h3", "hvalue3"); + underTest.setStatus(201); + underTest.setContentType("text/plain"); + underTest.sendRedirect("http://redirect"); + underTest.setCharacterEncoding("UTF-8"); + + Cookie cookie = mock(Cookie.class); + when(cookie.getName()).thenReturn("name"); + when(cookie.getValue()).thenReturn("value"); + underTest.addCookie(cookie); + verify(responseMock).addHeader("h2", "hvalue2"); + verify(responseMock).setHeader("h3", "hvalue3"); + verify(responseMock).setStatus(201); + verify(responseMock).setContentType("text/plain"); + verify(responseMock).sendRedirect("http://redirect"); + verify(responseMock).setCharacterEncoding("UTF-8"); + verify(responseMock).addCookie(any(javax.servlet.http.Cookie.class)); + } +} |