You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2023/01/18 20:17:18 UTC

[tomcat] branch 10.1.x updated (257125c5eb -> 81b91a4268)

This is an automated email from the ASF dual-hosted git repository.

markt pushed a change to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


    from 257125c5eb Add missing "Not implemented" exception.
     new 4983c7ebf5 Refactor to reduce duplicate code
     new 7fb42ee701 Add missing facade checks
     new 81b91a4268 Refactor to reduce code duplication

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../apache/catalina/connector/ResponseFacade.java  | 241 ++++++---------------
 .../apache/catalina/core/ApplicationContext.java   |  71 ++----
 2 files changed, 83 insertions(+), 229 deletions(-)


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[tomcat] 03/03: Refactor to reduce code duplication

Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 81b91a42683ff72b8d15d0bdaf90a92fb41e3ef0
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Wed Jan 18 20:02:14 2023 +0000

    Refactor to reduce code duplication
---
 .../apache/catalina/core/ApplicationContext.java   | 71 ++++++----------------
 1 file changed, 18 insertions(+), 53 deletions(-)

diff --git a/java/org/apache/catalina/core/ApplicationContext.java b/java/org/apache/catalina/core/ApplicationContext.java
index 5cd5ad23a1..6a28ae8cdc 100644
--- a/java/org/apache/catalina/core/ApplicationContext.java
+++ b/java/org/apache/catalina/core/ApplicationContext.java
@@ -90,10 +90,8 @@ import org.apache.tomcat.util.res.StringManager;
  */
 public class ApplicationContext implements ServletContext {
 
-
     // ----------------------------------------------------------- Constructors
 
-
     /**
      * Construct a new instance of this class, associated with the specified
      * Context instance.
@@ -735,12 +733,8 @@ public class ApplicationContext implements ServletContext {
                     "applicationContext.invalidFilterName", filterName));
         }
 
-        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
-            //TODO Spec breaking enhancement to ignore this restriction
-            throw new IllegalStateException(
-                    sm.getString("applicationContext.addFilter.ise",
-                            getContextPath()));
-        }
+        // TODO Spec breaking enhancement to ignore this restriction
+        checkState("applicationContext.addFilter.ise");
 
         FilterDef filterDef = context.findFilterDef(filterName);
 
@@ -856,12 +850,8 @@ public class ApplicationContext implements ServletContext {
                     "applicationContext.invalidServletName", servletName));
         }
 
-        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
-            //TODO Spec breaking enhancement to ignore this restriction
-            throw new IllegalStateException(
-                    sm.getString("applicationContext.addServlet.ise",
-                            getContextPath()));
-        }
+        // TODO Spec breaking enhancement to ignore this restriction
+        checkState("applicationContext.addServlet.ise");
 
         Wrapper wrapper = (Wrapper) context.findChild(servletName);
 
@@ -986,11 +976,7 @@ public class ApplicationContext implements ServletContext {
     @Override
     public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) {
 
-        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
-            throw new IllegalStateException(
-                    sm.getString("applicationContext.setSessionTracking.ise",
-                            getContextPath()));
-        }
+        checkState("applicationContext.setSessionTracking.ise");
 
         // Check that only supported tracking modes have been requested
         for (SessionTrackingMode sessionTrackingMode : sessionTrackingModes) {
@@ -1020,12 +1006,7 @@ public class ApplicationContext implements ServletContext {
         if (name == null) {
             throw new NullPointerException(sm.getString("applicationContext.setAttribute.namenull"));
         }
-        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
-            throw new IllegalStateException(
-                    sm.getString("applicationContext.setInitParam.ise",
-                            getContextPath()));
-        }
-
+        checkState("applicationContext.setInitParam.ise");
         return parameters.putIfAbsent(name, value) == null;
     }
 
@@ -1076,11 +1057,7 @@ public class ApplicationContext implements ServletContext {
 
     @Override
     public <T extends EventListener> void addListener(T t) {
-        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
-            throw new IllegalStateException(
-                    sm.getString("applicationContext.addListener.ise",
-                            getContextPath()));
-        }
+        checkState("applicationContext.addListener.ise");
 
         boolean match = false;
         if (t instanceof ServletContextAttributeListener ||
@@ -1146,12 +1123,8 @@ public class ApplicationContext implements ServletContext {
     @Override
     public void declareRoles(String... roleNames) {
 
-        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
-            //TODO Spec breaking enhancement to ignore this restriction
-            throw new IllegalStateException(
-                    sm.getString("applicationContext.addRole.ise",
-                            getContextPath()));
-        }
+        // TODO Spec breaking enhancement to ignore this restriction
+        checkState("applicationContext.addRole.ise");
 
         if (roleNames == null) {
             throw new IllegalArgumentException(
@@ -1256,12 +1229,7 @@ public class ApplicationContext implements ServletContext {
 
     @Override
     public void setSessionTimeout(int sessionTimeout) {
-        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
-            throw new IllegalStateException(
-                    sm.getString("applicationContext.setSessionTimeout.ise",
-                            getContextPath()));
-        }
-
+        checkState("applicationContext.setSessionTimeout.ise");
         context.setSessionTimeout(sessionTimeout);
     }
 
@@ -1274,12 +1242,7 @@ public class ApplicationContext implements ServletContext {
 
     @Override
     public void setRequestCharacterEncoding(String encoding) {
-        if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
-            throw new IllegalStateException(
-                    sm.getString("applicationContext.setRequestEncoding.ise",
-                            getContextPath()));
-        }
-
+        checkState("applicationContext.setRequestEncoding.ise");
         context.setRequestCharacterEncoding(encoding);
     }
 
@@ -1292,13 +1255,15 @@ public class ApplicationContext implements ServletContext {
 
     @Override
     public void setResponseCharacterEncoding(String encoding) {
+        checkState("applicationContext.setResponseEncoding.ise");
+        context.setResponseCharacterEncoding(encoding);
+    }
+
+
+    private void checkState(String messageKey) {
         if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
-            throw new IllegalStateException(
-                    sm.getString("applicationContext.setResponseEncoding.ise",
-                            getContextPath()));
+            throw new IllegalStateException(sm.getString(messageKey,getContextPath()));
         }
-
-        context.setResponseCharacterEncoding(encoding);
     }
 
 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[tomcat] 02/03: Add missing facade checks

Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 7fb42ee701056ae3b6ebc561b5ded5a2bd6f4517
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Wed Jan 18 19:29:37 2023 +0000

    Add missing facade checks
---
 .../apache/catalina/connector/ResponseFacade.java  | 23 +++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/catalina/connector/ResponseFacade.java b/java/org/apache/catalina/connector/ResponseFacade.java
index 1cc3e34501..8d97f83db4 100644
--- a/java/org/apache/catalina/connector/ResponseFacade.java
+++ b/java/org/apache/catalina/connector/ResponseFacade.java
@@ -110,7 +110,6 @@ public class ResponseFacade implements HttpServletResponse {
      * @param response The response to be wrapped
      */
     public ResponseFacade(Response response) {
-
          this.response = response;
     }
 
@@ -178,6 +177,7 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public ServletOutputStream getOutputStream() throws IOException {
+        checkFacade();
         ServletOutputStream sos = response.getOutputStream();
         if (isFinished()) {
             response.setSuspended(true);
@@ -188,6 +188,7 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public PrintWriter getWriter() throws IOException {
+        checkFacade();
         PrintWriter writer = response.getWriter();
         if (isFinished()) {
             response.setSuspended(true);
@@ -198,6 +199,7 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public void setContentLength(int len) {
+        checkFacade();
         if (isCommitted()) {
             return;
         }
@@ -207,6 +209,7 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public void setContentLengthLong(long length) {
+        checkFacade();
         if (isCommitted()) {
             return;
         }
@@ -216,6 +219,7 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public void setContentType(String type) {
+        checkFacade();
         if (isCommitted()) {
             return;
         }
@@ -244,6 +248,7 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public void flushBuffer() throws IOException {
+        checkFacade();
         if (isFinished()) {
             return;
         }
@@ -287,6 +292,7 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public void setLocale(Locale loc) {
+        checkFacade();
         if (isCommitted()) {
             return;
         }
@@ -303,6 +309,7 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public void addCookie(Cookie cookie) {
+        checkFacade();
         if (isCommitted()) {
             return;
         }
@@ -357,6 +364,7 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public void setDateHeader(String name, long date) {
+        checkFacade();
         if (isCommitted()) {
             return;
         }
@@ -371,6 +379,7 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public void addDateHeader(String name, long date) {
+        checkFacade();
         if (isCommitted()) {
             return;
         }
@@ -385,6 +394,7 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public void setHeader(String name, String value) {
+        checkFacade();
         if (isCommitted()) {
             return;
         }
@@ -394,6 +404,7 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public void addHeader(String name, String value) {
+        checkFacade();
         if (isCommitted()) {
             return;
         }
@@ -403,6 +414,7 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public void setIntHeader(String name, int value) {
+        checkFacade();
         if (isCommitted()) {
             return;
         }
@@ -412,6 +424,7 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public void addIntHeader(String name, int value) {
+        checkFacade();
         if (isCommitted()) {
             return;
         }
@@ -421,6 +434,7 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public void setStatus(int sc) {
+        checkFacade();
         if (isCommitted()) {
             return;
         }
@@ -443,33 +457,39 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public int getStatus() {
+        checkFacade();
         return response.getStatus();
     }
 
     @Override
     public String getHeader(String name) {
+        checkFacade();
         return response.getHeader(name);
     }
 
     @Override
     public Collection<String> getHeaderNames() {
+        checkFacade();
         return response.getHeaderNames();
     }
 
     @Override
     public Collection<String> getHeaders(String name) {
+        checkFacade();
         return response.getHeaders(name);
     }
 
 
     @Override
     public void setTrailerFields(Supplier<Map<String, String>> supplier) {
+        checkFacade();
         response.setTrailerFields(supplier);
     }
 
 
     @Override
     public Supplier<Map<String, String>> getTrailerFields() {
+        checkFacade();
         return response.getTrailerFields();
     }
 
@@ -482,6 +502,7 @@ public class ResponseFacade implements HttpServletResponse {
 
 
     private void checkCommitted(String messageKey) {
+        checkFacade();
         if (isCommitted()) {
             throw new IllegalStateException(sm.getString(messageKey));
         }


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[tomcat] 01/03: Refactor to reduce duplicate code

Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 4983c7ebf5fc542b712e159722a3ae46004a20b6
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Wed Jan 18 19:26:07 2023 +0000

    Refactor to reduce duplicate code
---
 .../apache/catalina/connector/ResponseFacade.java  | 218 ++++-----------------
 1 file changed, 43 insertions(+), 175 deletions(-)

diff --git a/java/org/apache/catalina/connector/ResponseFacade.java b/java/org/apache/catalina/connector/ResponseFacade.java
index fa795660fa..1cc3e34501 100644
--- a/java/org/apache/catalina/connector/ResponseFacade.java
+++ b/java/org/apache/catalina/connector/ResponseFacade.java
@@ -132,7 +132,6 @@ public class ResponseFacade implements HttpServletResponse {
 
     // --------------------------------------------------------- Public Methods
 
-
     /**
      * Clear facade.
      */
@@ -145,90 +144,55 @@ public class ResponseFacade implements HttpServletResponse {
      * Prevent cloning the facade.
      */
     @Override
-    protected Object clone()
-        throws CloneNotSupportedException {
+    protected Object clone() throws CloneNotSupportedException {
         throw new CloneNotSupportedException();
     }
 
 
     public void finish() {
-
-        if (response == null) {
-            throw new IllegalStateException(
-                            sm.getString("responseFacade.nullResponse"));
-        }
-
+        checkFacade();
         response.setSuspended(true);
     }
 
 
     public boolean isFinished() {
-
-        if (response == null) {
-            throw new IllegalStateException(
-                            sm.getString("responseFacade.nullResponse"));
-        }
-
+        checkFacade();
         return response.isSuspended();
     }
 
 
     public long getContentWritten() {
-
-        if (response == null) {
-            throw new IllegalStateException(
-                            sm.getString("responseFacade.nullResponse"));
-        }
-
+        checkFacade();
         return response.getContentWritten();
     }
 
-    // ------------------------------------------------ ServletResponse Methods
 
+    // ------------------------------------------------ ServletResponse Methods
 
     @Override
     public String getCharacterEncoding() {
-
-        if (response == null) {
-            throw new IllegalStateException(
-                            sm.getString("responseFacade.nullResponse"));
-        }
-
+        checkFacade();
         return response.getCharacterEncoding();
     }
 
 
     @Override
-    public ServletOutputStream getOutputStream()
-        throws IOException {
-
-        //        if (isFinished())
-        //            throw new IllegalStateException
-        //                (/*sm.getString("responseFacade.finished")*/);
-
+    public ServletOutputStream getOutputStream() throws IOException {
         ServletOutputStream sos = response.getOutputStream();
         if (isFinished()) {
             response.setSuspended(true);
         }
         return sos;
-
     }
 
 
     @Override
-    public PrintWriter getWriter()
-        throws IOException {
-
-        //        if (isFinished())
-        //            throw new IllegalStateException
-        //                (/*sm.getString("responseFacade.finished")*/);
-
+    public PrintWriter getWriter() throws IOException {
         PrintWriter writer = response.getWriter();
         if (isFinished()) {
             response.setSuspended(true);
         }
         return writer;
-
     }
 
 
@@ -252,7 +216,6 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public void setContentType(String type) {
-
         if (isCommitted()) {
             return;
         }
@@ -267,32 +230,20 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public void setBufferSize(int size) {
-
-        if (isCommitted()) {
-            throw new IllegalStateException
-                (sm.getString("coyoteResponse.setBufferSize.ise"));
-        }
-
+        checkCommitted("coyoteResponse.setBufferSize.ise");
         response.setBufferSize(size);
-
     }
 
 
     @Override
     public int getBufferSize() {
-
-        if (response == null) {
-            throw new IllegalStateException(
-                            sm.getString("responseFacade.nullResponse"));
-        }
-
+        checkFacade();
         return response.getBufferSize();
     }
 
 
     @Override
     public void flushBuffer() throws IOException {
-
         if (isFinished()) {
             return;
         }
@@ -315,275 +266,178 @@ public class ResponseFacade implements HttpServletResponse {
 
     @Override
     public void resetBuffer() {
-
-        if (isCommitted()) {
-            throw new IllegalStateException
-                (sm.getString("coyoteResponse.resetBuffer.ise"));
-        }
-
+        checkCommitted("coyoteResponse.resetBuffer.ise");
         response.resetBuffer();
-
     }
 
 
     @Override
     public boolean isCommitted() {
-
-        if (response == null) {
-            throw new IllegalStateException(
-                            sm.getString("responseFacade.nullResponse"));
-        }
-
+        checkFacade();
         return response.isAppCommitted();
     }
 
 
     @Override
     public void reset() {
-
-        if (isCommitted()) {
-            throw new IllegalStateException
-                (sm.getString("coyoteResponse.reset.ise"));
-        }
-
+        checkCommitted("coyoteResponse.reset.ise");
         response.reset();
-
     }
 
 
     @Override
     public void setLocale(Locale loc) {
-
         if (isCommitted()) {
             return;
         }
-
         response.setLocale(loc);
     }
 
 
     @Override
     public Locale getLocale() {
-
-        if (response == null) {
-            throw new IllegalStateException(
-                            sm.getString("responseFacade.nullResponse"));
-        }
-
+        checkFacade();
         return response.getLocale();
     }
 
 
     @Override
     public void addCookie(Cookie cookie) {
-
         if (isCommitted()) {
             return;
         }
-
         response.addCookie(cookie);
-
     }
 
 
     @Override
     public boolean containsHeader(String name) {
-
-        if (response == null) {
-            throw new IllegalStateException(
-                            sm.getString("responseFacade.nullResponse"));
-        }
-
+        checkFacade();
         return response.containsHeader(name);
     }
 
 
     @Override
     public String encodeURL(String url) {
-
-        if (response == null) {
-            throw new IllegalStateException(
-                            sm.getString("responseFacade.nullResponse"));
-        }
-
+        checkFacade();
         return response.encodeURL(url);
     }
 
 
     @Override
     public String encodeRedirectURL(String url) {
-
-        if (response == null) {
-            throw new IllegalStateException(
-                            sm.getString("responseFacade.nullResponse"));
-        }
-
+        checkFacade();
         return response.encodeRedirectURL(url);
     }
 
 
     @Override
-    public void sendError(int sc, String msg)
-        throws IOException {
-
-        if (isCommitted()) {
-            throw new IllegalStateException
-                (sm.getString("coyoteResponse.sendError.ise"));
-        }
-
+    public void sendError(int sc, String msg) throws IOException {
+        checkCommitted("coyoteResponse.sendError.ise");
         response.setAppCommitted(true);
-
         response.sendError(sc, msg);
-
     }
 
 
     @Override
-    public void sendError(int sc)
-        throws IOException {
-
-        if (isCommitted()) {
-            throw new IllegalStateException
-                (sm.getString("coyoteResponse.sendError.ise"));
-        }
-
+    public void sendError(int sc) throws IOException {
+        checkCommitted("coyoteResponse.sendError.ise");
         response.setAppCommitted(true);
-
         response.sendError(sc);
-
     }
 
 
     @Override
-    public void sendRedirect(String location)
-        throws IOException {
-
-        if (isCommitted()) {
-            throw new IllegalStateException
-                (sm.getString("coyoteResponse.sendRedirect.ise"));
-        }
-
+    public void sendRedirect(String location) throws IOException {
+        checkCommitted("coyoteResponse.sendRedirect.ise");
         response.setAppCommitted(true);
-
         response.sendRedirect(location);
-
     }
 
 
     @Override
     public void setDateHeader(String name, long date) {
-
         if (isCommitted()) {
             return;
         }
 
-        if(Globals.IS_SECURITY_ENABLED) {
-            AccessController.doPrivileged(new DateHeaderPrivilegedAction
-                                             (name, date, false));
+        if (Globals.IS_SECURITY_ENABLED) {
+            AccessController.doPrivileged(new DateHeaderPrivilegedAction(name, date, false));
         } else {
             response.setDateHeader(name, date);
         }
-
     }
 
 
     @Override
     public void addDateHeader(String name, long date) {
-
         if (isCommitted()) {
             return;
         }
 
-        if(Globals.IS_SECURITY_ENABLED) {
-            AccessController.doPrivileged(new DateHeaderPrivilegedAction
-                                             (name, date, true));
+        if (Globals.IS_SECURITY_ENABLED) {
+            AccessController.doPrivileged(new DateHeaderPrivilegedAction(name, date, true));
         } else {
             response.addDateHeader(name, date);
         }
-
     }
 
 
     @Override
     public void setHeader(String name, String value) {
-
         if (isCommitted()) {
             return;
         }
-
         response.setHeader(name, value);
-
     }
 
 
     @Override
     public void addHeader(String name, String value) {
-
         if (isCommitted()) {
             return;
         }
-
         response.addHeader(name, value);
-
     }
 
 
     @Override
     public void setIntHeader(String name, int value) {
-
         if (isCommitted()) {
             return;
         }
-
         response.setIntHeader(name, value);
-
     }
 
 
     @Override
     public void addIntHeader(String name, int value) {
-
         if (isCommitted()) {
             return;
         }
-
         response.addIntHeader(name, value);
-
     }
 
 
     @Override
     public void setStatus(int sc) {
-
         if (isCommitted()) {
             return;
         }
-
         response.setStatus(sc);
-
     }
 
 
     @Override
     public String getContentType() {
-
-        if (response == null) {
-            throw new IllegalStateException(
-                            sm.getString("responseFacade.nullResponse"));
-        }
-
+        checkFacade();
         return response.getContentType();
     }
 
 
     @Override
     public void setCharacterEncoding(String arg0) {
-
-        if (response == null) {
-            throw new IllegalStateException(
-                            sm.getString("responseFacade.nullResponse"));
-        }
-
+        checkFacade();
         response.setCharacterEncoding(arg0);
     }
 
@@ -618,4 +472,18 @@ public class ResponseFacade implements HttpServletResponse {
     public Supplier<Map<String, String>> getTrailerFields() {
         return response.getTrailerFields();
     }
+
+
+    private void checkFacade() {
+        if (response == null) {
+            throw new IllegalStateException(sm.getString("responseFacade.nullResponse"));
+        }
+    }
+
+
+    private void checkCommitted(String messageKey) {
+        if (isCommitted()) {
+            throw new IllegalStateException(sm.getString(messageKey));
+        }
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org