diff --git a/core/src/main/java/com/predic8/membrane/core/interceptor/rewrite/HeaderInterceptor.java b/core/src/main/java/com/predic8/membrane/core/interceptor/rewrite/HeaderInterceptor.java new file mode 100644 index 0000000000..8c2807d656 --- /dev/null +++ b/core/src/main/java/com/predic8/membrane/core/interceptor/rewrite/HeaderInterceptor.java @@ -0,0 +1,95 @@ +/* Copyright 2023 predic8 GmbH, www.predic8.com + + Licensed 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 com.predic8.membrane.core.interceptor.rewrite; + +import com.predic8.membrane.annot.MCAttribute; +import com.predic8.membrane.annot.MCElement; +import com.predic8.membrane.annot.Required; +import com.predic8.membrane.core.exchange.Exchange; +import com.predic8.membrane.core.http.*; +import com.predic8.membrane.core.interceptor.AbstractInterceptor; +import com.predic8.membrane.core.interceptor.Outcome; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPathExpressionException; +import java.awt.font.TextHitInfo; +import java.io.IOException; + +import static com.predic8.membrane.core.interceptor.Interceptor.Flow.Set.REQUEST_RESPONSE; +import static com.predic8.membrane.core.interceptor.Outcome.CONTINUE; + +/** + * @description
+ * Adds or overwrites headers in requests and responses using a value or expression. + *
+ * @topic 4. Interceptors/Features + */ +@MCElement(name="header") +public class HeaderInterceptor extends AbstractInterceptor { + private static final Logger log = LoggerFactory.getLogger(HeaderInterceptor.class.getName()); + private String identifier; + private String value; + + public HeaderInterceptor() { + name = "Header Modifier"; + setFlow(REQUEST_RESPONSE); + } + + private Outcome handleInternal(Message msg) { + Header h = msg.getHeader(); + + if (h.contains(identifier)) + h.remove( + new HeaderField( + identifier, + h.getFirstValue(identifier) + ) + ); + + h.add(identifier, value); + return CONTINUE; + } + + @Override + public Outcome handleRequest(Exchange exc) throws Exception { + return handleInternal(exc.getRequest()); + } + + @Override + public Outcome handleResponse(Exchange exc) throws Exception { + return handleInternal(exc.getResponse()); + } + + /** + * @description The name of the header. + */ + @Required + @MCAttribute(attributeName="name") + public void setHeaderIdentifier(String identifier) { + this.identifier = identifier; + } + + /** + * @description The value of the header. + */ + @Required + @MCAttribute(attributeName="value") + public void setHeaderValue(String value) { + this.value = value; + } +} diff --git a/core/src/test/java/com/predic8/membrane/core/interceptor/rewrite/HeaderInterceptorTest.java b/core/src/test/java/com/predic8/membrane/core/interceptor/rewrite/HeaderInterceptorTest.java new file mode 100644 index 0000000000..3478fc2ac4 --- /dev/null +++ b/core/src/test/java/com/predic8/membrane/core/interceptor/rewrite/HeaderInterceptorTest.java @@ -0,0 +1,38 @@ +package com.predic8.membrane.core.interceptor.rewrite; + +import com.predic8.membrane.core.exchange.Exchange; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class HeaderInterceptorTest { + Exchange exc; + HeaderInterceptor hicpt1 = new HeaderInterceptor() {{ + setHeaderIdentifier("New"); + setHeaderValue("Header"); + }}; + HeaderInterceptor hicpt2 = new HeaderInterceptor() {{ + setHeaderIdentifier("Demo"); + setHeaderValue("New Value"); + }}; + + @BeforeEach + void initialize() throws Exception { + exc = new Exchange(null); + exc.getRequest().getHeader().add("Demo", "Test"); + } + + @Test + void addHeader() throws Exception { + hicpt1.handleRequest(exc); + assertTrue(exc.getRequest().getHeader().contains("New")); + } + + @Test + void overwriteHeader() throws Exception { + hicpt2.handleRequest(exc); + assertEquals("New Value", exc.getRequest().getHeader().getFirstValue("Demo")); + } +} \ No newline at end of file