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

Upgrade Jackson 1.x to Jackson 2.x #22

Merged
merged 4 commits into from
Mar 10, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 5 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ libraryDependencies ++= Seq(
"joda-time" % "joda-time" % "2.1",
"com.sun.jersey" % "jersey-bundle" % "1.9.1",
"com.sun.jersey.contribs" % "jersey-multipart" % "1.9.1",
"org.codehaus.jackson" % "jackson-mapper-asl" % "1.8.3",
"org.codehaus.jackson" % "jackson-jaxrs" % "1.8.3",
"org.codehaus.jackson" % "jackson-xc" % "1.8.3",
"com.fasterxml.jackson.core" % "jackson-core" % "2.8.6",
"com.fasterxml.jackson.module" % "jackson-module-jaxb-annotations" % "2.8.6",
"com.fasterxml.jackson.datatype" % "jackson-datatype-joda" % "2.8.6",
"com.fasterxml.jackson.jaxrs" % "jackson-jaxrs-json-provider" % "2.8.6",
"com.google.guava" % "guava" % "12.0",
"org.apache.commons" % "commons-lang3" % "3.1",
"commons-beanutils" % "commons-beanutils" % "1.8.3",
"commons-io" % "commons-io" % "1.3.2",
"com.novocode" % "junit-interface" % "0.9" % "test",
"junit-addons" % "junit-addons" % "1.4" % "test"
"com.novocode" % "junit-interface" % "0.9" % "test"
)

parallelExecution in Test := false
Expand Down
26 changes: 22 additions & 4 deletions src/main/java/com/socrata/api/HttpLowLevel.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.socrata.api;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.socrata.exceptions.*;
import com.socrata.model.SodaErrorResponse;
import com.socrata.model.requests.SodaRequest;
import com.socrata.utils.JacksonObjectMapperProvider;
import com.socrata.utils.ObjectMapperFactory;
import com.socrata.utils.streams.CompressingGzipInputStream;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
Expand All @@ -20,7 +23,6 @@
import com.sun.jersey.multipart.FormDataMultiPart;
import com.sun.jersey.multipart.file.FileDataBodyPart;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
Expand Down Expand Up @@ -56,6 +58,8 @@ public final class HttpLowLevel
.withLocale(Locale.US)
.withZone(DateTimeZone.UTC);

private static final TypeReference<Map<String,Object>> GENERIC_JSON_OBJECT_TYPE = new TypeReference<Map<String, Object>>() {};


protected static final int DEFAULT_MAX_RETRIES = 200;
public static final long DEFAULT_RETRY_TIME = 4000;
Expand All @@ -78,6 +82,8 @@ public final class HttpLowLevel

public static final GenericType<List<Object>> MAP_OBJECT_TYPE = new GenericType<List<Object>>() {};

private final ObjectMapper mapper;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion: since ObjectMapper is stateless, you can maintain a single ObjectMapper object as singleton across the code base.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not critical. Can push it off to next release

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, but since this isn't a new field, I was considering this to be out of scope; however, I did add a new constructor signature (line 188) so that the application may provide the library with its singleton ObjectMapper


private final Client client;
private final String url;

Expand Down Expand Up @@ -169,17 +175,22 @@ public static final HttpLowLevel instantiateBasic(@Nonnull final String url, @No
return new HttpLowLevel(client, url);
}

public HttpLowLevel(final Client client, final String url) {
this(client, url, ObjectMapperFactory.create());
}


/**
* Constructor
*
* @param client the Jersey Client class that will be used for actually issuing requests
* @param url the base URL for the SODA2 domain to access.
*/
public HttpLowLevel(Client client, final String url)
public HttpLowLevel(final Client client, final String url, final ObjectMapper mapper)
{
this.client = client;
this.url = url;
this.mapper = mapper;
}

/**
Expand Down Expand Up @@ -571,6 +582,14 @@ public void close() {
}
}

/**
* make the {@link ObjectMapper} available to other classes in this package so that they can
* share the same configured instance instead of potentially using differently configured ObjectMappers
*/
ObjectMapper getObjectMapper() {
return mapper;
}


/**
* Internal API to add any common parameters. In this case, it sets the version parameter
Expand Down Expand Up @@ -607,7 +626,6 @@ private ClientResponse processErrors(final ClientResponse response) throws SodaE
return response;
}

final ObjectMapper mapper = new ObjectMapper();
final String body = response.getEntity(String.class);

if (status == 202) {
Expand All @@ -631,7 +649,7 @@ private ClientResponse processErrors(final ClientResponse response) throws SodaE
}

try {
final Map<String, Object> bodyProperties = (Map<String, Object>)mapper.readValue(body, Object.class);
final Map<String, Object> bodyProperties = mapper.readValue(body, GENERIC_JSON_OBJECT_TYPE);
if (bodyProperties.get("ticket") != null) {
ticket = bodyProperties.get("ticket").toString();
}
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/com/socrata/api/Soda2Producer.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.socrata.api;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.socrata.exceptions.LongRunningQueryException;
import com.socrata.exceptions.SodaError;
import com.socrata.model.Meta;
import com.socrata.model.UpsertError;
import com.socrata.model.UpsertResult;
import com.socrata.model.Meta;
import com.socrata.model.requests.SodaModRequest;
import com.socrata.model.requests.SodaRequest;
import com.socrata.model.requests.SodaTypedRequest;
import com.socrata.utils.GeneralUtils;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.ObjectMapper;

import javax.ws.rs.core.MediaType;
import java.io.File;
Expand All @@ -34,6 +34,8 @@
public class Soda2Producer extends Soda2Consumer
{

private final JsonFactory factory;

/**
* Create a new Soda2Producer object, using the supplied credentials for authentication.
*
Expand Down Expand Up @@ -76,6 +78,7 @@ public static final Soda2Producer newProducerWithRequestId(final String url, Str
public Soda2Producer(HttpLowLevel httpLowLevel)
{
super(httpLowLevel);
factory = httpLowLevel.getObjectMapper().getFactory();
}


Expand Down Expand Up @@ -433,17 +436,16 @@ public ClientResponse issueRequest() throws LongRunningQueryException, SodaError
}

/**
* THis will return an upsert result, regardless of whether it is
* This will return an upsert result, regardless of whether it is
* using the original response, or the new return from SODA Server
*
*
* @param is
* @return
*/
static UpsertResult deserializeUpsertResult(InputStream is) throws IOException
UpsertResult deserializeUpsertResult(InputStream is) throws IOException
{
ObjectMapper mapper = new ObjectMapper();
JsonParser parser = mapper.getJsonFactory().createJsonParser(is);
JsonParser parser = factory.createParser(is);


if (parser.nextToken() == JsonToken.START_ARRAY) {
Expand Down
12 changes: 1 addition & 11 deletions src/main/java/com/socrata/api/SodaDdl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
import com.socrata.model.search.SearchClause;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

Expand Down Expand Up @@ -355,16 +352,9 @@ public ClientResponse issueRequest() throws LongRunningQueryException, SodaError

try {
final ClientResponse response = requester.issueRequest();
return mapper.readValue(response.getEntity(InputStream.class), AssetResponse.class);
//return response.getEntity(AssetResponse.class);
return response.getEntity(AssetResponse.class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

response.getEntity doesn't work with creating assets /api/assets. Keep the original mapper.readValue and error handlers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot find any documentation for the "assets" resource of the Import API. Does response.getEntity not work because this resource does not accept requests for application/json so the MessageBodyReader will not be triggered?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jackson looks for json content-type in getEntity. But the actual content type returned from the server is text/plain despite that it is json. Using readValue from input stream get around this problem.

} catch (LongRunningQueryException e) {
return getHttpLowLevel().getAsyncResults(e.location, e.timeToRetry, getHttpLowLevel().getMaxRetries(), AssetResponse.class, requester);
} catch (JsonMappingException e) {
throw new SodaError("Illegal response from the service.");
} catch (JsonParseException e) {
throw new SodaError("Invalid JSON returned from the service.");
} catch (IOException e) {
throw new SodaError("Error communicating with service.");
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/socrata/api/SodaWorkflow.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.socrata.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.socrata.exceptions.LongRunningQueryException;
import com.socrata.exceptions.SodaError;
import com.socrata.model.Comment;
Expand All @@ -8,7 +9,6 @@
import com.socrata.model.importer.DatasetInfo;
import com.socrata.model.requests.SodaRequest;
import com.sun.jersey.api.client.ClientResponse;
import org.codehaus.jackson.map.ObjectMapper;

import javax.ws.rs.core.UriBuilder;
import java.net.URI;
Expand All @@ -33,8 +33,8 @@ public class SodaWorkflow

protected final URI geocodingUri;
protected final HttpLowLevel httpLowLevel;
protected final ObjectMapper mapper;
protected final URI viewUri;
protected final ObjectMapper mapper;

/**
* Create a new SodaWorkflow object, using the supplied credentials for authentication.
Expand Down Expand Up @@ -70,7 +70,7 @@ public SodaWorkflow(HttpLowLevel httpLowLevel)
.path(VIEWS_BASE_PATH)
.build();

mapper = new ObjectMapper();
mapper = httpLowLevel.getObjectMapper();
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/socrata/model/Address.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.socrata.model;

import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* The address field located in the Location object's JSON instance.
*/
@JsonIgnoreProperties(ignoreUnknown=true)
@JsonSerialize(include= JsonSerialize.Inclusion.NON_NULL)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Address
{
protected final String streetAddress;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/socrata/model/Comment.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.socrata.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.socrata.model.importer.DatasetInfo;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonSerialize;

/**
*/
@JsonIgnoreProperties(ignoreUnknown=true)
@JsonSerialize(include= JsonSerialize.Inclusion.NON_NULL)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Comment
{

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/socrata/model/DeleteRecord.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.socrata.model;

import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
*/
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/socrata/model/GeocodingResults.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.socrata.model;

import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
*/
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/socrata/model/Location.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.socrata.model;

import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown=true)
@JsonSerialize(include= JsonSerialize.Inclusion.NON_NULL)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Location
{
public final Double longitude;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/socrata/model/Meta.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.socrata.model;

import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Date;

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/socrata/model/SearchResult.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.socrata.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Function;
import com.socrata.model.importer.Dataset;
import com.socrata.model.importer.DatasetInfo;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonProperty;

import javax.annotation.Nullable;

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/socrata/model/SearchResults.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.socrata.model;

import com.socrata.model.importer.Dataset;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/socrata/model/SodaErrorResponse.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.socrata.model;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;

/**
*/
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/socrata/model/UpsertError.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.socrata.model;

import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* This represents an error with some of the data being added through
Expand Down
Loading