Skip to content

Commit

Permalink
Bring back code to support JAXRS v1 (revert #134 for 2.18) (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning authored Jul 26, 2024
1 parent 26ecb80 commit 9931b19
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.fasterxml.jackson.jaxrs.base;

import java.io.IOException;

/**
* Implementors of this class contains strategies for NoContentException creation
*/
public interface NoContentExceptionSupplier
{
String NO_CONTENT_MESSAGE = "No content (empty input stream)";

IOException createNoContentException();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Type;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;

import javax.ws.rs.core.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;

Expand All @@ -16,6 +22,8 @@
import com.fasterxml.jackson.databind.util.LookupCache;
import com.fasterxml.jackson.databind.type.TypeFactory;

import com.fasterxml.jackson.jaxrs.base.nocontent.JaxRS1NoContentExceptionSupplier;
import com.fasterxml.jackson.jaxrs.base.nocontent.JaxRS2NoContentExceptionSupplier;
import com.fasterxml.jackson.jaxrs.cfg.*;
import com.fasterxml.jackson.jaxrs.util.ClassKey;

Expand All @@ -37,7 +45,7 @@ public abstract class ProviderBase<

protected final static String CLASS_NAME_NO_CONTENT_EXCEPTION = "javax.ws.rs.core.NoContentException";

private final static String NO_CONTENT_MESSAGE = "No content (empty input stream)";
private final NoContentExceptionSupplier noContentExceptionSupplier = _createNoContentExceptionSupplier();

/**
* Looks like we need to worry about accidental
Expand Down Expand Up @@ -565,8 +573,8 @@ public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotat
*/
@Override
public void writeTo(Object value, Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String,Object> httpHeaders, OutputStream entityStream)
MediaType mediaType,
MultivaluedMap<String,Object> httpHeaders, OutputStream entityStream)
throws IOException
{
EP_CONFIG endpoint = _endpointForWriting(value, type, genericType, annotations,
Expand Down Expand Up @@ -963,7 +971,7 @@ protected boolean _isIgnorableForWriting(ClassKey typeKey)
}

protected IOException _createNoContentException() {
return new NoContentException(NO_CONTENT_MESSAGE);
return noContentExceptionSupplier.createNoContentException();
}

/*
Expand Down Expand Up @@ -1040,4 +1048,45 @@ protected static void _addSuperTypes(Class<?> cls, Class<?> endBefore, Collectio
private final THIS _this() {
return (THIS) this;
}

/**
* Since class <code>javax.ws.rs.core.NoContentException</code> only exists in
* JAX-RS 2.0, but we want to have 1.x compatibility, need to dynamically select exception supplier
*/
private static NoContentExceptionSupplier _createNoContentExceptionSupplier() {
try {
final Class<?> cls = Class.forName(CLASS_NAME_NO_CONTENT_EXCEPTION, false, getClassLoader());
Constructor<?> ctor;
if (System.getSecurityManager() == null) {
ctor = cls.getDeclaredConstructor(String.class);
} else {
ctor = AccessController.doPrivileged(new PrivilegedAction<Constructor<?>>() {
@Override
public Constructor<?> run() {
try {
return cls.getDeclaredConstructor(String.class);
} catch (NoSuchMethodException ignore) {
return null;
}
}
});
}
if (ctor != null) {
return new JaxRS2NoContentExceptionSupplier();
}
} catch (ClassNotFoundException | NoSuchMethodException ex) { }
return new JaxRS1NoContentExceptionSupplier();
}

private static ClassLoader getClassLoader() {
if (System.getSecurityManager() == null) {
return ProviderBase.class.getClassLoader();
}
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
@Override
public ClassLoader run() {
return ProviderBase.class.getClassLoader();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.fasterxml.jackson.jaxrs.base.nocontent;

import com.fasterxml.jackson.jaxrs.base.NoContentExceptionSupplier;

import java.io.IOException;

/**
* Create plain IOException for JaxRS 1.x because {@link javax.ws.rs.core.NoContentException}
* has been introduced in JaxRS 2.x
*/
public class JaxRS1NoContentExceptionSupplier implements NoContentExceptionSupplier
{
@Override
public IOException createNoContentException()
{
return new IOException(NO_CONTENT_MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.fasterxml.jackson.jaxrs.base.nocontent;

import com.fasterxml.jackson.jaxrs.base.NoContentExceptionSupplier;

import javax.ws.rs.core.NoContentException;
import java.io.IOException;

/**
* This supplier creates fair NoContentException from JaxRS 2.x
*/
public class JaxRS2NoContentExceptionSupplier implements NoContentExceptionSupplier {
@Override
public IOException createNoContentException() {
return new NoContentException(NoContentExceptionSupplier.NO_CONTENT_MESSAGE);
}
}
1 change: 1 addition & 0 deletions base/src/moditect/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module com.fasterxml.jackson.jaxrs.base {
exports com.fasterxml.jackson.jaxrs.annotation;
exports com.fasterxml.jackson.jaxrs.base;
exports com.fasterxml.jackson.jaxrs.base.nocontent;
exports com.fasterxml.jackson.jaxrs.cfg;
exports com.fasterxml.jackson.jaxrs.util;

Expand Down
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Sub-modules:

2.18.0 (not yet released)

#192: Bring back code to support JAXRS v1 (revert #134 for 2.18)
(contributed by @pjfanning)
* Woodstox dependency now 7.0.0

2.17.2 (05-Jul-2024)
Expand Down

0 comments on commit 9931b19

Please sign in to comment.