Skip to content

Commit

Permalink
Merge pull request #45232 from geoand/mediatype-delegate
Browse files Browse the repository at this point in the history
Avoid using the RuntimeDelegate indirection for MediaType
  • Loading branch information
geoand authored Dec 20, 2024
2 parents 06e5613 + 069c5a0 commit 33d99fd
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;

import org.jboss.resteasy.reactive.common.util.MediaTypeHelper;
import org.jboss.resteasy.reactive.server.core.EncodedMediaType;
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext;
import org.jboss.resteasy.reactive.server.spi.ServerHttpResponse;
Expand Down Expand Up @@ -65,7 +66,7 @@ public void handle(ResteasyReactiveRequestContext requestContext) throws Excepti
if (encodedProduces == null) {
synchronized (this) {
if (encodedProduces == null) {
encodedProduces = new EncodedMediaType(MediaType.valueOf(produces));
encodedProduces = new EncodedMediaType(MediaTypeHelper.valueOf(produces));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static URI getLocation(MultivaluedMap<String, ? extends Object> headers)
public static MediaType getMediaType(MultivaluedMap<String, ? extends Object> headers) {
Object first = headers.getFirst(HttpHeaders.CONTENT_TYPE);
if (first instanceof String contentType) {
return MediaType.valueOf(contentType);
return MediaTypeHelper.valueOf(contentType);
} else {
return (MediaType) first;
}
Expand Down Expand Up @@ -281,10 +281,10 @@ public static List<MediaType> getAcceptableMediaTypes(MultivaluedMap<String, ? e
StringTokenizer tokenizer = new StringTokenizer(accept, ",");
while (tokenizer.hasMoreElements()) {
String item = tokenizer.nextToken().trim();
list.add(MediaType.valueOf(item));
list.add(MediaTypeHelper.valueOf(item));
}
} else {
list.add(MediaType.valueOf(accept.trim()));
list.add(MediaTypeHelper.valueOf(accept.trim()));
}
}
MediaTypeHelper.sortByWeight(list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.ext.ContextResolver;

import org.jboss.resteasy.reactive.common.util.MediaTypeHelper;
import org.jboss.resteasy.reactive.spi.BeanFactory;

public class ResourceContextResolver {
Expand Down Expand Up @@ -48,7 +49,7 @@ public List<MediaType> mediaTypes() {
synchronized (this) {
List<MediaType> ret = new ArrayList<>();
for (String i : mediaTypeStrings) {
ret.add(MediaType.valueOf(i));
ret.add(MediaTypeHelper.valueOf(i));
}
mediaTypes = Collections.unmodifiableList(ret);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public List<MediaType> mediaTypes() {
synchronized (this) {
List<MediaType> mts = new ArrayList<>(mediaTypeStrings.size());
for (int i = 0; i < mediaTypeStrings.size(); i++) {
mts.add(MediaType.valueOf(mediaTypeStrings.get(i)));
mts.add(MediaTypeHelper.valueOf(mediaTypeStrings.get(i)));
}
mediaTypes = Collections.unmodifiableList(mts);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public List<MediaType> mediaTypes() {
synchronized (this) {
List<MediaType> mts = new ArrayList<>(mediaTypeStrings.size());
for (int i = 0; i < mediaTypeStrings.size(); i++) {
mts.add(MediaType.valueOf(mediaTypeStrings.get(i)));
mts.add(MediaTypeHelper.valueOf(mediaTypeStrings.get(i)));
}
mediaTypes = Collections.unmodifiableList(mts);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import org.jboss.resteasy.reactive.common.headers.MediaTypeHeaderDelegate;

/**
* @author <a href="mailto:[email protected]">Bill Burke</a>
*/
Expand All @@ -21,6 +23,14 @@ public class MediaTypeHelper {
public static final MediaTypeComparator QS_COMPARATOR = new MediaTypeComparator("qs");
private static final String MEDIA_TYPE_SUFFIX_DELIM = "+";

public static MediaType valueOf(String value) {
return MediaTypeHeaderDelegate.INSTANCE.fromString(value);
}

public static String toString(MediaType mediaType) {
return MediaTypeHeaderDelegate.INSTANCE.toString(mediaType);
}

private static float getQTypeWithParamInfo(MediaType type, String parameterName) {
if (type.getParameters() != null) {
String val = type.getParameters().get(parameterName);
Expand Down Expand Up @@ -212,7 +222,7 @@ public static List<MediaType> parseHeader(String header) {
ArrayList<MediaType> types = new ArrayList<>();
String[] medias = header.split(",");
for (String media : medias) {
types.add(MediaType.valueOf(media.trim()));
types.add(valueOf(media.trim()));
}
return types;
}
Expand Down Expand Up @@ -289,7 +299,7 @@ public static List<MediaType> toListOfMediaType(String[] mediaTypes) {

List<MediaType> list = new ArrayList<>(mediaTypes.length);
for (String mediaType : mediaTypes) {
list.add(MediaType.valueOf(mediaType));
list.add(valueOf(mediaType));
}

return Collections.unmodifiableList(list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import jakarta.ws.rs.core.MediaType;

import org.jboss.resteasy.reactive.common.headers.MediaTypeHeaderDelegate;

/**
* A representation of a server side media type.
*
Expand All @@ -25,7 +27,7 @@ public class ServerMediaType {
public static List<MediaType> mediaTypesFromArray(String[] mediaTypesStrs) {
List<MediaType> mediaTypes = new ArrayList<>(mediaTypesStrs.length);
for (String mediaTypesStr : mediaTypesStrs) {
mediaTypes.add(MediaType.valueOf(mediaTypesStr));
mediaTypes.add(MediaTypeHeaderDelegate.INSTANCE.fromString(mediaTypesStr));
}
return mediaTypes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public MediaType getMediaType() {
@Override
public String getEncoded() {
if (encoded == null) {
return encoded = mediaType.toString();
return encoded = MediaTypeHelper.toString(mediaType);
}
return encoded;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ public RuntimeResource buildResourceMethod(ResourceClass clazz,
Map<String, Integer> pathParameterIndexes = buildParamIndexMap(classPathTemplate, methodPathTemplate);
MediaType streamElementType = null;
if (method.getStreamElementType() != null) {
streamElementType = MediaType.valueOf(method.getStreamElementType());
streamElementType = MediaTypeHelper.valueOf(method.getStreamElementType());
}
List<MediaType> consumesMediaTypes;
if (method.getConsumes() == null) {
consumesMediaTypes = Collections.emptyList();
} else {
consumesMediaTypes = new ArrayList<>(method.getConsumes().length);
for (String s : method.getConsumes()) {
consumesMediaTypes.add(MediaType.valueOf(s));
consumesMediaTypes.add(MediaTypeHelper.valueOf(s));
}
}

Expand Down Expand Up @@ -407,7 +407,7 @@ public RuntimeResource buildResourceMethod(ResourceClass clazz,
if (method.getProduces() != null && method.getProduces().length > 0) {
//the method can only produce a single content type, which is the most common case
if (method.getProduces().length == 1) {
MediaType mediaType = MediaType.valueOf(method.getProduces()[0]);
MediaType mediaType = MediaTypeHelper.valueOf(method.getProduces()[0]);
//its a wildcard type, makes it hard to determine statically
if (mediaType.isWildcardType() || mediaType.isWildcardSubtype()) {
handlers.add(new VariableProducesHandler(serverMediaType, serialisers));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void handle(ResteasyReactiveRequestContext requestContext) throws Excepti
try {
if (MediaTypeHelper.getFirstMatch(
target.value.getConsumes(),
Collections.singletonList(MediaType.valueOf(contentType))) == null) {
Collections.singletonList(MediaTypeHelper.valueOf(contentType))) == null) {
throw new NotSupportedException("The content-type header value did not match the value in @Consumes");
}
} catch (IllegalArgumentException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private List<MediaType> contentTypeFromRequest(ResteasyReactiveRequestContext re
}
List<MediaType> result = new ArrayList<>(contentTypeList.size());
for (String s : contentTypeList) {
result.add(MediaType.valueOf(s));
result.add(MediaTypeHelper.valueOf(s));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void handle(ResteasyReactiveRequestContext requestContext) throws Excepti
Object requestType = requestContext.getHeader(HttpHeaders.CONTENT_TYPE, true);
if (requestType != null) {
try {
effectiveRequestType = MediaType.valueOf((String) requestType);
effectiveRequestType = MediaTypeHelper.valueOf((String) requestType);
} catch (Exception e) {
log.debugv("Incorrect media type", e);
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import org.jboss.resteasy.reactive.common.headers.HeaderUtil;
import org.jboss.resteasy.reactive.common.util.CaseInsensitiveMap;
import org.jboss.resteasy.reactive.common.util.MediaTypeHelper;
import org.jboss.resteasy.reactive.common.util.UnmodifiableMultivaluedMap;

/**
Expand Down Expand Up @@ -85,7 +86,7 @@ public MediaType getMediaType() {
if (obj == cachedMediaTypeString)
return cachedMediaType;
cachedMediaTypeString = obj;
cachedMediaType = MediaType.valueOf(obj);
cachedMediaType = MediaTypeHelper.valueOf(obj);
return cachedMediaType;
}

Expand Down

0 comments on commit 33d99fd

Please sign in to comment.