Skip to content

Commit

Permalink
Added Header interceptor and test
Browse files Browse the repository at this point in the history
  • Loading branch information
t-burch committed Oct 23, 2023
1 parent 2c2ab44 commit 35d84fc
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 <p>
* Adds or overwrites headers in requests and responses using a value or expression.
* </p>
* @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;
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit 35d84fc

Please sign in to comment.