Skip to content

Commit

Permalink
Implement #24
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 10, 2013
1 parent c9b69d2 commit 8b22cd8
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 134 deletions.
2 changes: 1 addition & 1 deletion base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-providers</artifactId>
<version>2.2.4-SNAPSHOT</version>
<version>2.3.0-SNAPSHOT</version>
</parent>
<artifactId>jackson-jaxrs-base</artifactId>
<name>Jackson-JAXRS-base</name>
Expand Down
130 changes: 88 additions & 42 deletions base/src/main/java/com/fasterxml/jackson/jaxrs/base/ProviderBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ public abstract class ProviderBase<
*/
protected int _jaxRSFeatures;

/**
* View to use for reading if none defined for the end point.
*/
protected Class<?> _defaultReadView;

/**
* View to use for writing if none defined for the end point.
*/
protected Class<?> _defaultWriteView;

/*
/**********************************************************
/* Excluded types
Expand Down Expand Up @@ -175,7 +185,7 @@ protected ProviderBase(MAPPER_CONFIG mconfig) {
* Should NOT be used by any code explicitly; only exists
* for proxy support.
*/
@Deprecated
@Deprecated // just to denote it should NOT be directly called; will not be removed
protected ProviderBase() {
_mapperConfig = null;
}
Expand Down Expand Up @@ -252,23 +262,61 @@ public void setMapper(MAPPER m) {
_mapperConfig.setMapper(m);
}

/**
* Method for specifying JSON View to use for reading content
* when end point does not have explicit View annotations.
*
* @since 2.3
*/
public THIS setDefaultReadView(Class<?> view) {
_defaultReadView = view;
return _this();
}

/**
* Method for specifying JSON View to use for reading content
* when end point does not have explicit View annotations.
*
* @since 2.3
*/
public THIS setDefaultWriteView(Class<?> view) {
_defaultWriteView = view;
return _this();
}

/**
* Method for specifying JSON View to use for reading and writing content
* when end point does not have explicit View annotations.
* Functionally equivalent to:
*<code>
* setDefaultReadView(view);
* setDefaultWriteView(view);
*</code>
*
* @since 2.3
*/
public THIS setDefaultView(Class<?> view) {
_defaultReadView = _defaultWriteView = view;
return _this();
}

// // // JaxRSFeature config

public THIS configure(JaxRSFeature feature, boolean state) {
_jaxRSFeatures |= feature.getMask();
_jaxRSFeatures |= feature.getMask();
return _this();
}

public THIS enable(JaxRSFeature feature) {
_jaxRSFeatures |= feature.getMask();
_jaxRSFeatures |= feature.getMask();
return _this();
}

public THIS enable(JaxRSFeature first, JaxRSFeature... f2) {
_jaxRSFeatures |= first.getMask();
for (JaxRSFeature f : f2) {
_jaxRSFeatures |= f.getMask();
}
_jaxRSFeatures |= first.getMask();
for (JaxRSFeature f : f2) {
_jaxRSFeatures |= f.getMask();
}
return _this();
}

Expand All @@ -278,10 +326,10 @@ public THIS disable(JaxRSFeature feature) {
}

public THIS disable(JaxRSFeature first, JaxRSFeature... f2) {
_jaxRSFeatures &= ~first.getMask();
for (JaxRSFeature f : f2) {
_jaxRSFeatures &= ~f.getMask();
}
_jaxRSFeatures &= ~first.getMask();
for (JaxRSFeature f : f2) {
_jaxRSFeatures &= ~f.getMask();
}
return _this();
}

Expand Down Expand Up @@ -398,10 +446,12 @@ protected boolean hasMatchingMediaTypeForWriting(MediaType mediaType) {

protected abstract MAPPER _locateMapperViaProvider(Class<?> type, MediaType mediaType);

protected abstract EP_CONFIG _configForReading(MAPPER mapper, Annotation[] annotations);
protected abstract EP_CONFIG _configForReading(MAPPER mapper,
Annotation[] annotations, Class<?> defaultView);

protected abstract EP_CONFIG _configForWriting(MAPPER mapper,
Annotation[] annotations, Class<?> defaultView);

protected abstract EP_CONFIG _configForWriting(MAPPER mapper, Annotation[] annotations);

/*
/**********************************************************
/* Partial MessageBodyWriter impl
Expand Down Expand Up @@ -444,22 +494,20 @@ public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotat
}
Boolean customUntouchable = _findCustomUntouchable(type);
if (customUntouchable != null) {
// negation: Boolean.TRUE means untouchable -> can not write
return !customUntouchable.booleanValue();
// negation: Boolean.TRUE means untouchable -> can not write
return !customUntouchable.booleanValue();
}
if (customUntouchable == null) {
/* Ok: looks like we must weed out some core types here; ones that
* make no sense to try to bind from JSON:
*/
if (_untouchables.contains(new ClassKey(type))) {
/* Ok: looks like we must weed out some core types here; ones that
* make no sense to try to bind from JSON:
*/
if (_untouchables.contains(new ClassKey(type))) {
return false;
}
// but some are interface/abstract classes, so
for (Class<?> cls : _unwritableClasses) {
if (cls.isAssignableFrom(type)) {
return false;
}
// but some are interface/abstract classes, so
for (Class<?> cls : _unwritableClasses) {
if (cls.isAssignableFrom(type)) {
return false;
}
}
}
// Also: if we really want to verify that we can deserialize, we'll check:
if (_cfgCheckCanSerialize) {
Expand Down Expand Up @@ -487,7 +535,7 @@ public void writeTo(Object value, Class<?> type, Type genericType, Annotation[]
// not yet resolved (or not cached any more)? Resolve!
if (endpoint == null) {
MAPPER mapper = locateMapper(type, mediaType);
endpoint = _configForWriting(mapper, annotations);
endpoint = _configForWriting(mapper, annotations, _defaultWriteView);
// and cache for future reuse
synchronized (_writers) {
_writers.put(key.immutableKey(), endpoint);
Expand Down Expand Up @@ -596,22 +644,20 @@ public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotati

Boolean customUntouchable = _findCustomUntouchable(type);
if (customUntouchable != null) {
// negation: Boolean.TRUE means untouchable -> can not write
return !customUntouchable.booleanValue();
// negation: Boolean.TRUE means untouchable -> can not write
return !customUntouchable.booleanValue();
}
if (customUntouchable == null) {
/* Ok: looks like we must weed out some core types here; ones that
* make no sense to try to bind from JSON:
*/
if (_untouchables.contains(new ClassKey(type))) {
/* Ok: looks like we must weed out some core types here; ones that
* make no sense to try to bind from JSON:
*/
if (_untouchables.contains(new ClassKey(type))) {
return false;
}
// and there are some other abstract/interface types to exclude too:
for (Class<?> cls : _unreadableClasses) {
if (cls.isAssignableFrom(type)) {
return false;
}
// and there are some other abstract/interface types to exclude too:
for (Class<?> cls : _unreadableClasses) {
if (cls.isAssignableFrom(type)) {
return false;
}
}
}
// Finally: if we really want to verify that we can serialize, we'll check:
if (_cfgCheckCanSerialize) {
Expand Down Expand Up @@ -641,7 +687,7 @@ public Object readFrom(Class<Object> type, Type genericType, Annotation[] annota
// not yet resolved (or not cached any more)? Resolve!
if (endpoint == null) {
MAPPER mapper = locateMapper(type, mediaType);
endpoint = _configForReading(mapper, annotations);
endpoint = _configForReading(mapper, annotations, _defaultReadView);
// and cache for future reuse
synchronized (_readers) {
_readers.put(key.immutableKey(), endpoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class EndpointConfigBase<THIS extends EndpointConfigBase<THIS>>
{
// // General configuration

protected Class<?> _activeView;
protected Class<?> _activeView;

protected String _rootName;

Expand Down Expand Up @@ -83,13 +83,25 @@ protected void addAnnotation(Class<? extends Annotation> type,
}
}
}

/**
* @deprecated Since 2.3
*/
@Deprecated
protected THIS initReader(ObjectMapper mapper) {
return initReader(mapper, null);
}

@SuppressWarnings("unchecked")
protected THIS initReader(ObjectMapper mapper)
protected THIS initReader(ObjectMapper mapper, Class<?> defaultView)
{
// first common config
if (_activeView != null) {
_reader = mapper.readerWithView(_activeView);
Class<?> view = _activeView;
if (view == null) {
view = defaultView;
}
if (view != null) {
_reader = mapper.readerWithView(view);
} else {
_reader = mapper.reader();
}
Expand All @@ -112,12 +124,24 @@ protected THIS initReader(ObjectMapper mapper)
return (THIS) this;
}

/**
* @deprecated Since 2.3
*/
@Deprecated
protected THIS initWriter(ObjectMapper mapper) {
return initWriter(mapper, null);
}

@SuppressWarnings("unchecked")
protected THIS initWriter(ObjectMapper mapper)
protected THIS initWriter(ObjectMapper mapper, Class<?> defaultView)
{
// first common config
if (_activeView != null) {
_writer = mapper.writerWithView(_activeView);
Class<?> view = _activeView;
if (view == null) {
view = defaultView;
}
if (view != null) {
_writer = mapper.writerWithView(view);
} else {
_writer = mapper.writer();
}
Expand Down
17 changes: 1 addition & 16 deletions json/pom.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
| Copyright 2012 FasterXML.com
|
| Licensed under the Apache License, Version 2.0 (the "License");
| you may not use this file except in compliance with the License.
| You may obtain a copy of the License at
|
| http://www.apache.org/licenses/LICENSE-2.0
|
| Unless required by applicable law or agreed to in writing, software
| distributed under the License is distributed on an "AS IS" BASIS,
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
| See the License for the specific language governing permissions and
| limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-providers</artifactId>
<version>2.2.4-SNAPSHOT</version>
<version>2.3.0-SNAPSHOT</version>
</parent>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<name>Jackson-JAXRS-JSON</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;

import com.fasterxml.jackson.jaxrs.base.ProviderBase;
import com.fasterxml.jackson.jaxrs.cfg.Annotations;

Expand Down Expand Up @@ -218,12 +217,15 @@ protected ObjectMapper _locateMapperViaProvider(Class<?> type, MediaType mediaTy
}

@Override
protected JsonEndpointConfig _configForReading(ObjectMapper mapper, Annotation[] annotations) {
return JsonEndpointConfig.forReading(mapper, annotations);
protected JsonEndpointConfig _configForReading(ObjectMapper mapper,
Annotation[] annotations, Class<?> defaultView) {
return JsonEndpointConfig.forReading(mapper, annotations, defaultView);
}

@Override
protected JsonEndpointConfig _configForWriting(ObjectMapper mapper, Annotation[] annotations) {
return JsonEndpointConfig.forWriting(mapper, annotations, _jsonpFunctionName);
protected JsonEndpointConfig _configForWriting(ObjectMapper mapper,
Annotation[] annotations, Class<?> defaultView) {
return JsonEndpointConfig.forWriting(mapper, annotations, defaultView,
_jsonpFunctionName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ public class JsonEndpointConfig

protected JsonEndpointConfig() { }

public static JsonEndpointConfig forReading(ObjectMapper mapper, Annotation[] annotations)
public static JsonEndpointConfig forReading(ObjectMapper mapper,
Annotation[] annotations, Class<?> defaultView)
{
return new JsonEndpointConfig()
.add(annotations, false)
.initReader(mapper);
.initReader(mapper, defaultView);
}

public static JsonEndpointConfig forWriting(ObjectMapper mapper, Annotation[] annotations,
public static JsonEndpointConfig forWriting(ObjectMapper mapper,
Annotation[] annotations, Class<?> defaultView,
String defaultJsonpMethod)
{
JsonEndpointConfig config = new JsonEndpointConfig();
Expand All @@ -44,7 +46,7 @@ public static JsonEndpointConfig forWriting(ObjectMapper mapper, Annotation[] an
}
return config
.add(annotations, true)
.initWriter(mapper)
.initWriter(mapper, defaultView)
;
}

Expand Down
Loading

0 comments on commit 8b22cd8

Please sign in to comment.