Skip to content

Commit

Permalink
Get media type fix (#5282)
Browse files Browse the repository at this point in the history
* Fixed caching of content_type

Signed-off-by: jansupol <[email protected]>
  • Loading branch information
jansupol committed Mar 23, 2023
1 parent 6bd50c8 commit 4964825
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -170,14 +170,17 @@ public Set<String> getGuards() {
*/
public boolean isObservedAndReset(String key) {
Boolean observed = guards.get(key);
guards.put(key, false);
if (observed != null) {
guards.put(key, false);
}
return observed != null && observed;
}

private void observe(String key) {
for (Map.Entry<String, Boolean> guard : guards.entrySet()) {
if (guard.getKey().equals(key)) {
guard.setValue(true);
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -114,7 +114,7 @@ public OutboundMessageContext(Configuration configuration) {
this.runtimeDelegateDecorator = RuntimeDelegateDecorator.configured(configuration);
this.mediaTypeCache = mediaTypeCache();

headers.setGuard(HttpHeaders.CONTENT_LENGTH);
headers.setGuard(HttpHeaders.CONTENT_TYPE);
}

/**
Expand All @@ -125,7 +125,7 @@ public OutboundMessageContext(Configuration configuration) {
*/
public OutboundMessageContext(OutboundMessageContext original) {
this.headers = new GuardianStringKeyMultivaluedMap<>(HeaderUtils.createOutbound());
this.headers.setGuard(HttpHeaders.CONTENT_LENGTH);
this.headers.setGuard(HttpHeaders.CONTENT_TYPE);
this.headers.putAll(original.headers);
this.committingOutputStream = new CommittingOutputStream();
this.entityStream = committingOutputStream;
Expand All @@ -135,7 +135,7 @@ public OutboundMessageContext(OutboundMessageContext original) {
this.entityAnnotations = original.entityAnnotations;
this.configuration = original.configuration;
this.runtimeDelegateDecorator = original.runtimeDelegateDecorator;
this.mediaTypeCache = original.mediaTypeCache();
this.mediaTypeCache = mediaTypeCache();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -19,6 +19,7 @@
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
Expand Down Expand Up @@ -183,12 +184,19 @@ private static byte[] readAllBytes(InputStream inputStream) throws IOException {
Math.min(buf.length - nread, remaining))) > 0) {
nread += n;
remaining -= n;

if (nread == BUFFER_SIZE) { // This differs from JDK version
break; // prevents a bug (See ReaderWriterTest)
}
}

if (nread > 0) {
if (MAX_BUFFER_SIZE - total < nread) {
throw new OutOfMemoryError("Required array size too large");
}
if (nread < buf.length) {
buf = Arrays.copyOfRange(buf, 0, nread);
}
total += nread;
if (result == null) {
result = buf;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -213,8 +213,7 @@ private ConsumesProducesAcceptor(
* @param requestContext The request to be tested.
* @return True if the {@code request} can be processed by this router, false otherwise.
*/
boolean isConsumable(ContainerRequest requestContext) {
MediaType contentType = requestContext.getMediaType();
boolean isConsumable(ContainerRequest requestContext, MediaType contentType) {
return contentType == null || consumes.getMediaType().isCompatible(contentType);
}

Expand Down Expand Up @@ -411,8 +410,9 @@ private List<Router> getMethodRouter(final RequestProcessingContext context) {

final List<ConsumesProducesAcceptor> satisfyingAcceptors = new LinkedList<>();
final Set<ResourceMethod> differentInvokableMethods = Collections.newSetFromMap(new IdentityHashMap<>());
final MediaType requestContentType = request.getMediaType();
for (ConsumesProducesAcceptor cpi : acceptors) {
if (cpi.isConsumable(request)) {
if (cpi.isConsumable(request, requestContentType)) {
satisfyingAcceptors.add(cpi);
differentInvokableMethods.add(cpi.methodRouting.method);
}
Expand All @@ -423,7 +423,6 @@ private List<Router> getMethodRouter(final RequestProcessingContext context) {

final List<AcceptableMediaType> acceptableMediaTypes = request.getQualifiedAcceptableMediaTypes();

final MediaType requestContentType = request.getMediaType();
final MediaType effectiveContentType = requestContentType == null ? MediaType.WILDCARD_TYPE : requestContentType;

final MethodSelector methodSelector = selectMethod(acceptableMediaTypes, satisfyingAcceptors, effectiveContentType,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -36,6 +36,7 @@
import org.glassfish.jersey.message.internal.OutboundMessageContext;
import org.glassfish.jersey.tests.e2e.common.TestRuntimeDelegate;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -225,4 +226,21 @@ public void testGetLength_tooLongForInt() {

fail();
}

@Test
public void testChangedContentType() {
OutboundMessageContext ctx = new OutboundMessageContext((Configuration) null);
ctx.setMediaType(MediaType.APPLICATION_XML_TYPE);
Assertions.assertEquals(MediaType.APPLICATION_XML_TYPE, ctx.getMediaType());
ctx.setMediaType(MediaType.APPLICATION_JSON_TYPE);
Assertions.assertEquals(MediaType.APPLICATION_JSON_TYPE, ctx.getMediaType());
}

@Test
public void testCopyConstructor() {
OutboundMessageContext ctx = new OutboundMessageContext((Configuration) null);
OutboundMessageContext newCtx = new OutboundMessageContext(ctx);
newCtx.setMediaType(MediaType.APPLICATION_XML_TYPE); // new value
Assertions.assertEquals(MediaType.APPLICATION_XML_TYPE, newCtx.getMediaType());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.e2e.common.message.internal;

import org.glassfish.jersey.message.internal.ReaderWriter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import javax.ws.rs.core.MediaType;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class ReaderWriterTest {
@Test
public void testToNeverAskToReadZeroBytes() throws IOException {
// Unnamed app server bug test
int size = ((ReaderWriter.BUFFER_SIZE + 1000) / 10) * 10;
StringBuilder sb = new StringBuilder(size);
String shortMsg = "0123456789";
while (sb.length() < size) {
sb.append(shortMsg);
}

ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8)) {
@Override
public synchronized int read(byte[] b, int off, int len) {
if (len == 0) {
return -1; // simulate the bug
}
return super.read(b, off, len);
}
};

String read = ReaderWriter.readFromAsString(byteArrayInputStream, MediaType.TEXT_HTML_TYPE);
Assertions.assertEquals(size, read.length());
}
}

0 comments on commit 4964825

Please sign in to comment.