[java-opensaml] 02/02: OSJ-368 Deprecate HttpServletMessageErrorHandler and HTTPRequestValidationHandler

Rod Widdowson rdw at steadingsoftware.com
Sat Jan 7 14:23:27 UTC 2023


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch main
in repository java-opensaml.

View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=ff4294c7b4c545a010885d9ad73fe381dd677d5f

commit ff4294c7b4c545a010885d9ad73fe381dd677d5f
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Sat Jan 7 14:21:50 2023 +0000

    OSJ-368 Deprecate HttpServletMessageErrorHandler and HTTPRequestValidationHandler
    
    https://shibboleth.atlassian.net/browse/OSJ-368
    
    Remove from V5
---
 .../AbstractHttpServletMessageErrorHandler.java    |  56 ------
 .../servlet/HttpServletMessageErrorHandler.java    |  61 -------
 .../handler/impl/HTTPRequestValidationHandler.java | 198 ---------------------
 .../impl/HTTPRequestValidationHandlerTest.java     | 113 ------------
 4 files changed, 428 deletions(-)

diff --git a/opensaml-messaging-api/src/main/java/org/opensaml/messaging/error/servlet/AbstractHttpServletMessageErrorHandler.java b/opensaml-messaging-api/src/main/java/org/opensaml/messaging/error/servlet/AbstractHttpServletMessageErrorHandler.java
deleted file mode 100644
index d5267674b..000000000
--- a/opensaml-messaging-api/src/main/java/org/opensaml/messaging/error/servlet/AbstractHttpServletMessageErrorHandler.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Licensed to the University Corporation for Advanced Internet Development,
- * Inc. (UCAID) under one or more contributor license agreements.  See the
- * NOTICE file distributed with this work for additional information regarding
- * copyright ownership. The UCAID licenses this file to You under the Apache
- * License, Version 2.0 (the "License"); you may not use this file except in
- * compliance with the License.  You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.opensaml.messaging.error.servlet;
-
-import javax.annotation.Nullable;
-import jakarta.servlet.http.HttpServletRequest;
-import jakarta.servlet.http.HttpServletResponse;
-
-
-/**
- * Abstract implementation of {@link HttpServletMessageErrorHandler}.
- */
-public abstract class AbstractHttpServletMessageErrorHandler implements HttpServletMessageErrorHandler {
-    
-    /** The HTTP servlet request. */
-    private HttpServletRequest request;
-    
-    /** The HTTP servlet response. */
-    private HttpServletResponse response;
-
-    /** {@inheritDoc} */
-    @Nullable public HttpServletRequest getHttpServletRequest() {
-        return request;
-    }
-
-    /** {@inheritDoc} */
-    @Nullable public HttpServletResponse getHttpServletResponse() {
-        return response;
-    }
-
-    /** {@inheritDoc} */
-    public void setHttpServletRequest(@Nullable final HttpServletRequest servletRequest) {
-        request = servletRequest;
-    }
-
-    /** {@inheritDoc} */
-    public void setHttpServletResponse(@Nullable final HttpServletResponse servletResponse) {
-        response = servletResponse;
-    }
-
-}
diff --git a/opensaml-messaging-api/src/main/java/org/opensaml/messaging/error/servlet/HttpServletMessageErrorHandler.java b/opensaml-messaging-api/src/main/java/org/opensaml/messaging/error/servlet/HttpServletMessageErrorHandler.java
deleted file mode 100644
index 9d73aa451..000000000
--- a/opensaml-messaging-api/src/main/java/org/opensaml/messaging/error/servlet/HttpServletMessageErrorHandler.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Licensed to the University Corporation for Advanced Internet Development,
- * Inc. (UCAID) under one or more contributor license agreements.  See the
- * NOTICE file distributed with this work for additional information regarding
- * copyright ownership. The UCAID licenses this file to You under the Apache
- * License, Version 2.0 (the "License"); you may not use this file except in
- * compliance with the License.  You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.opensaml.messaging.error.servlet;
-
-import javax.annotation.Nullable;
-
-import org.opensaml.messaging.error.MessageErrorHandler;
-
-import jakarta.servlet.http.HttpServletRequest;
-import jakarta.servlet.http.HttpServletResponse;
-
-
-/**
- * A specialization of message error handler for HTTP servlet container environments.
- */
-public interface HttpServletMessageErrorHandler extends MessageErrorHandler {
-    
-    /**
-     * Get the HTTP servlet request.
-     * 
-     * @return the HTTP servlet request
-     */
-    @Nullable HttpServletRequest getHttpServletRequest();
-    
-    /**
-     * Set the HTTP servlet request.
-     * 
-     * @param request the HTTP servlet request
-     */
-    void setHttpServletRequest(@Nullable final HttpServletRequest request);
-    
-    /**
-     * Get the HTTP servlet response.
-     * 
-     * @return the HTTP servlet response
-     */
-    @Nullable HttpServletResponse getHttpServletResponse();
-    
-    /**
-     * Set the HTTP servlet response.
-     * 
-     * @param response the HTTP servlet response
-     */
-    void setHttpServletResponse(@Nullable final HttpServletResponse response);
-
-}
diff --git a/opensaml-messaging-impl/src/main/java/org/opensaml/messaging/handler/impl/HTTPRequestValidationHandler.java b/opensaml-messaging-impl/src/main/java/org/opensaml/messaging/handler/impl/HTTPRequestValidationHandler.java
deleted file mode 100644
index 8f1044991..000000000
--- a/opensaml-messaging-impl/src/main/java/org/opensaml/messaging/handler/impl/HTTPRequestValidationHandler.java
+++ /dev/null
@@ -1,198 +0,0 @@
-/*
- * Licensed to the University Corporation for Advanced Internet Development,
- * Inc. (UCAID) under one or more contributor license agreements.  See the
- * NOTICE file distributed with this work for additional information regarding
- * copyright ownership. The UCAID licenses this file to You under the Apache
- * License, Version 2.0 (the "License"); you may not use this file except in
- * compliance with the License.  You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.opensaml.messaging.handler.impl;
-
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
-
-import org.opensaml.messaging.context.MessageContext;
-import org.opensaml.messaging.handler.AbstractMessageHandler;
-import org.opensaml.messaging.handler.MessageHandlerException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import jakarta.servlet.http.HttpServletRequest;
-import net.shibboleth.shared.annotation.constraint.NonnullAfterInit;
-import net.shibboleth.shared.component.ComponentInitializationException;
-import net.shibboleth.shared.logic.Constraint;
-import net.shibboleth.shared.primitive.StringSupport;
-
-/**
- * A message handler that checks basic HTTP request properties.
- */
-public class HTTPRequestValidationHandler extends AbstractMessageHandler {
-
-    /** Class logger. */
-    @Nonnull private final Logger log = LoggerFactory.getLogger(HTTPRequestValidationHandler.class);
-
-    /** Expected content type of the request. */
-    @Nullable private String requiredContentType;
-
-    /** Expected method of the request. */
-    @Nullable private String requiredRequestMethod;
-
-    /** Whether the request must be secure. */
-    private boolean requireSecured;
-    
-    /** The HTTP servlet request being evaluated. */
-    @NonnullAfterInit private HttpServletRequest httpServletRequest;
-
-    /**
-     * Get the required content type.
-     * 
-     * @return the required content type
-     */
-    public String getRequiredContentType() {
-        return requiredContentType;
-    }
-
-    /**
-     * Set the required content type.
-     * 
-     * @param contentType the content type
-     */
-    public void setRequiredContentType(final String contentType) {
-        checkSetterPreconditions();
-        requiredContentType = contentType;
-    }
-
-    /**
-     * Get the required request method.
-     * 
-     * @return the required request method
-     */
-    public String getRequiredRequestMethod() {
-        return requiredRequestMethod;
-    }
-
-    /**
-     * Set the required request method.
-     * 
-     * @param requestMethod the required request method
-     */
-    public void setRequiredRequestMethod(final String requestMethod) {
-        checkSetterPreconditions();
-        requiredRequestMethod = StringSupport.trimOrNull(requestMethod);
-    }
-
-    /**
-     * Get whether request is required to be secure.
-     * 
-     * @return true if required to be secure, false otherwise
-     */
-    public boolean isRequireSecured() {
-        return requireSecured;
-    }
-
-    /**
-     * Set whether request is required to be secure. 
-     * 
-     * @param secured true if required to be secure, false otherwise
-     */
-    public void setRequireSecured(final boolean secured) {
-        checkSetterPreconditions();
-        requireSecured = secured;
-    }
-
-    /**
-     * Get the HTTP servlet request instance being evaluated.
-     * 
-     * @return returns the request instance
-     */
-    public HttpServletRequest getHttpServletRequest() {
-        return httpServletRequest;
-    }
-
-    /**
-     * Set the HTTP servlet request instance being evaluated.
-     * 
-     * @param request the request instance
-     */
-    public void setHttpServletRequest(final HttpServletRequest request) {
-        checkSetterPreconditions();
-        httpServletRequest = Constraint.isNotNull(request, "HttpServletRequest may not be null");
-    }
-
-    /** {@inheritDoc} */
-    protected void doInitialize() throws ComponentInitializationException {
-        super.doInitialize();
-        
-        Constraint.isNotNull(getHttpServletRequest(), "HttpServletRequest may not be null");
-    }
-
-    /**
-     * Evaluates whether the specified HTTP servlet request meets all requirements.
-     * 
-     * @param messageContext message context being evaluated
-     * 
-     * @throws MessageHandlerException thrown if the request does not meet the requirements of the handler
-     */
-    protected void doInvoke(final MessageContext messageContext) throws MessageHandlerException {
-        evaluateContentType(getHttpServletRequest());
-        evaluateRequestMethod(getHttpServletRequest());
-        evaluateSecured(getHttpServletRequest());
-    }
-
-    /**
-     * Checks if the request is of the correct content type.
-     * 
-     * @param request the request being evaluated
-     * 
-     * @throws MessageHandlerException thrown if the content type was an unexpected value
-     */
-    protected void evaluateContentType(final HttpServletRequest request) throws MessageHandlerException {
-        final String transportContentType = request.getHeader("Content-Type");
-        if (getRequiredContentType() != null && !transportContentType.startsWith(getRequiredContentType())) {
-            log.error("Invalid content type, expected '{}' but was '{}'", getRequiredContentType(), 
-                    transportContentType);
-            throw new MessageHandlerException("Invalid content type, expected " + getRequiredContentType() 
-                    + " but was " + transportContentType);
-        }
-    }
-
-    /**
-     * Checks if the request contains the correct request method.
-     * 
-     * @param request the request being evaluated
-     * 
-     * @throws MessageHandlerException thrown if the request method was an unexpected value
-     */
-    protected void evaluateRequestMethod(final HttpServletRequest request) throws MessageHandlerException {
-        final String transportMethod = request.getMethod();
-        if (getRequiredRequestMethod() != null && !transportMethod.equalsIgnoreCase(getRequiredRequestMethod())) {
-            log.error("Invalid request method, expected '{}' but was '{}'", getRequiredRequestMethod(), 
-                    transportMethod);
-            throw new MessageHandlerException("Invalid request method, expected " + getRequiredRequestMethod() 
-                    + " but was " + transportMethod);
-        }
-    }
-
-    /**
-     * Checks if the request is secured.
-     * 
-     * @param request the request being evaluated
-     * 
-     * @throws MessageHandlerException thrown if the request is not secure and was required to be
-     */
-    protected void evaluateSecured(final HttpServletRequest request) throws MessageHandlerException {
-        if (isRequireSecured() && !request.isSecure()) {
-            log.error("Request was required to be secured but was not");
-            throw new MessageHandlerException("Request was required to be secured but was not");
-        }
-    }
-}
diff --git a/opensaml-messaging-impl/src/test/java/org/opensaml/messaging/handler/impl/HTTPRequestValidationHandlerTest.java b/opensaml-messaging-impl/src/test/java/org/opensaml/messaging/handler/impl/HTTPRequestValidationHandlerTest.java
deleted file mode 100644
index b7f7bbcd2..000000000
--- a/opensaml-messaging-impl/src/test/java/org/opensaml/messaging/handler/impl/HTTPRequestValidationHandlerTest.java
+++ /dev/null
@@ -1,113 +0,0 @@
-/*
- * Licensed to the University Corporation for Advanced Internet Development,
- * Inc. (UCAID) under one or more contributor license agreements.  See the
- * NOTICE file distributed with this work for additional information regarding
- * copyright ownership. The UCAID licenses this file to You under the Apache
- * License, Version 2.0 (the "License"); you may not use this file except in
- * compliance with the License.  You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.opensaml.messaging.handler.impl;
-
-import org.opensaml.messaging.context.MessageContext;
-import org.opensaml.messaging.handler.MessageHandlerException;
-import org.springframework.mock.web.MockHttpServletRequest;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.Test;
-
-import jakarta.servlet.http.HttpServletRequest;
-
-/**
- * Test the HTTP request validation message handler.
- */
-public class HTTPRequestValidationHandlerTest {
-    
-    private MockHttpServletRequest httpRequest;
-    
-    private String contentType = "text/html";
-    private String method = "POST";
-    private boolean requireSecured = true;
-    
-    private HTTPRequestValidationHandler handler;
-    
-    private MessageContext messageContext;
-
-    @BeforeMethod
-    protected void setUp() throws Exception {
-        httpRequest = buildServletRequest();
-        
-        messageContext = new MessageContext();
-        
-        handler = new HTTPRequestValidationHandler();
-        handler.setHttpServletRequest(httpRequest);
-        handler.setRequiredRequestMethod(method);
-        handler.setRequiredContentType(contentType);
-        handler.setRequireSecured(true);
-        handler.initialize();
-    }
-    
-    /**
-     * Builds a mock {@link HttpServletRequest}.
-     * 
-     * @return the mock request
-     */
-    protected MockHttpServletRequest buildServletRequest() {
-        final MockHttpServletRequest request =  new MockHttpServletRequest();
-        request.setContentType(contentType);
-        request.setMethod(method);
-        request.setSecure(requireSecured);
-        return request;
-    }
-
-    /**
-     * Test all parameters valid.
-     * 
-     * @throws MessageHandlerException ...
-     */
-    @Test
-    public void testAllGood() throws MessageHandlerException {
-        handler.invoke(messageContext);
-    }
-
-    /**
-     * Bad request content type.
-     * 
-     * @throws MessageHandlerException ...
-     */
-    @Test(expectedExceptions=MessageHandlerException.class)
-    public void testContentTypeBad() throws MessageHandlerException {
-        httpRequest.setContentType("GARBAGE");
-        handler.invoke(messageContext);
-    }
-
-    /**
-     * Bad request method.
-     * 
-     * @throws MessageHandlerException ...
-     */
-    @Test(expectedExceptions=MessageHandlerException.class)
-    public void testRequestMethodBad() throws MessageHandlerException {
-        httpRequest.setMethod("GARBAGE");
-        handler.invoke(messageContext);
-    }
-    
-    /**
-     * Bad request secure flag.
-     * 
-     * @throws MessageHandlerException ...
-     */
-    @Test(expectedExceptions=MessageHandlerException.class)
-    public void testRequireSecureBad() throws MessageHandlerException {
-        httpRequest.setSecure(!requireSecured);
-        handler.invoke(messageContext);
-    }
-    
-}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list