Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented dynamic selection of NoContentException provider to back support of JaxRS 1.x #91

Merged
merged 1 commit into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,6 +2,7 @@

import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Type;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -13,6 +14,8 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
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;
import com.fasterxml.jackson.jaxrs.util.LRUMap;
Expand All @@ -33,6 +36,10 @@ public abstract class ProviderBase<
*/
public final static String HEADER_CONTENT_TYPE_OPTIONS = "X-Content-Type-Options";

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

protected final NoContentExceptionSupplier noContentExceptionSupplier = _createNoContentExceptionSupplier();

/**
* Looks like we need to worry about accidental
* data binding for types we shouldn't be handling. This is
Expand Down Expand Up @@ -967,9 +974,7 @@ protected boolean _isIgnorableForWriting(ClassKey typeKey)
*/
protected IOException _createNoContentException()
{
// 29-Jun-2016, tatu: With Jackson 2.8 we require JAX-RS 2.0 so this
// is fine; earlier had complicated Reflection-based access
return new NoContentException("No content (empty input stream)");
return noContentExceptionSupplier.createNoContentException();
}

/*
Expand Down Expand Up @@ -1046,4 +1051,24 @@ 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 {
Class cls = Class.forName(CLASS_NAME_NO_CONTENT_EXCEPTION);
Constructor<?> ctor = cls.getDeclaredConstructor(String.class);
if (ctor != null) {
return new JaxRS2NoContentExceptionSupplier();
} else {
return new JaxRS1NoContentExceptionSupplier();
}
} catch (ClassNotFoundException ex) {
return new JaxRS1NoContentExceptionSupplier();
} catch (NoSuchMethodException e) {
return new JaxRS1NoContentExceptionSupplier();
}
}
}
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);
}
}