Skip to content
Permalink
Browse files
CXF-8657: Headers are copied as case sensitive in MessageContextImpl (#…
  • Loading branch information
reta committed Feb 16, 2022
1 parent 33f2e5c commit b03bfd13355a2058ea7b072f73558360fab12101
Showing 2 changed files with 55 additions and 1 deletion.
@@ -218,7 +218,7 @@ private void convertToAttachments(Object value) {
Attachment root = (Attachment)handlers.get(0);

String rootContentType = root.getContentType().toString();
MultivaluedMap<String, String> rootHeaders = new MetadataMap<>(root.getHeaders());
MultivaluedMap<String, String> rootHeaders = new MetadataMap<>(root.getHeaders(), true, false, true);
if (!AttachmentUtil.isMtomEnabled(outMessage)) {
rootHeaders.putSingle(Message.CONTENT_TYPE, rootContentType);
}
@@ -19,20 +19,31 @@

package org.apache.cxf.jaxrs.ext;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import java.util.UUID;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Providers;
import javax.xml.bind.JAXBContext;

import org.apache.cxf.binding.Binding;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
import org.apache.cxf.jaxrs.impl.HttpHeadersImpl;
import org.apache.cxf.jaxrs.impl.HttpServletRequestFilter;
import org.apache.cxf.jaxrs.impl.HttpServletResponseFilter;
@@ -46,15 +57,20 @@
import org.apache.cxf.message.ExchangeImpl;
import org.apache.cxf.message.Message;
import org.apache.cxf.message.MessageImpl;
import org.apache.cxf.phase.PhaseInterceptorChain;
import org.apache.cxf.transport.http.AbstractHTTPDestination;

import org.easymock.Capture;
import org.easymock.EasyMock;
import org.easymock.IAnswer;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;

public class MessageContextImplTest {

@@ -197,6 +213,42 @@ public void testNoContext() {
MessageContext mc = new MessageContextImpl(createMessage());
assertNull(mc.getContext(Message.class));
}

@Test
public void testAttachments() throws IOException {
final Message in = createMessage();
final MessageContext mc = new MessageContextImpl(in);

try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
final Message out = new MessageImpl();
out.put(Message.CONTENT_TYPE, "image/png");
out.setContent(OutputStream.class, output);
out.setInterceptorChain(new PhaseInterceptorChain(Collections.emptySortedSet()));
in.getExchange().setOutMessage(out);

final Binding binding = in.getExchange().getEndpoint().getBinding();
final Capture<Message> capture = Capture.newInstance();
EasyMock.expect(binding.createMessage(EasyMock.capture(capture)))
.andAnswer(
new IAnswer<Message>() {
@Override
public Message answer() throws Throwable {
return capture.getValue();
}
}
).anyTimes();

final String id = UUID.randomUUID().toString();
final MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
// Headers should be case-insensitive
headers.add("Content-Id", id);
mc.put(MultipartBody.OUTBOUND_MESSAGE_ATTACHMENTS,
Collections.singletonList(new Attachment(headers, new byte[0])));

output.flush();
assertThat(new String(output.toByteArray()), containsString("Content-ID: <" + id + ">"));
}
}

private Message createMessage() {
ProviderFactory factory = ServerProviderFactory.getInstance();
@@ -205,12 +257,14 @@ private Message createMessage() {
Exchange e = new ExchangeImpl();
m.setExchange(e);
e.setInMessage(m);
Binding binding = EasyMock.mock(Binding.class);
Endpoint endpoint = EasyMock.mock(Endpoint.class);
EasyMock.expect(endpoint.getEndpointInfo()).andReturn(null).anyTimes();
EasyMock.expect(endpoint.get(Application.class.getName())).andReturn(null);
EasyMock.expect(endpoint.size()).andReturn(0).anyTimes();
EasyMock.expect(endpoint.isEmpty()).andReturn(true).anyTimes();
EasyMock.expect(endpoint.get(ServerProviderFactory.class.getName())).andReturn(factory).anyTimes();
EasyMock.expect(endpoint.getBinding()).andReturn(binding).anyTimes();
EasyMock.replay(endpoint);
e.put(Endpoint.class, endpoint);
return m;

0 comments on commit b03bfd1

Please sign in to comment.