catalogue-util-library/src/main/java/org/gcube/datacatalogue/utillibrary/jackan/JackanModule.java

136 lines
5.0 KiB
Java

/*
* Copyright 2015 Trento Rise.
*
* 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.
*/
package org.gcube.datacatalogue.utillibrary.jackan;
import static org.gcube.datacatalogue.utillibrary.jackan.CkanClient.formatTimestamp;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import org.gcube.com.fasterxml.jackson.core.JsonGenerator;
import org.gcube.com.fasterxml.jackson.core.JsonParser;
import org.gcube.com.fasterxml.jackson.core.JsonToken;
import org.gcube.com.fasterxml.jackson.core.type.TypeReference;
import org.gcube.com.fasterxml.jackson.databind.DeserializationContext;
import org.gcube.com.fasterxml.jackson.databind.JsonDeserializer;
import org.gcube.com.fasterxml.jackson.databind.PropertyNamingStrategy;
import org.gcube.com.fasterxml.jackson.databind.SerializerProvider;
import org.gcube.com.fasterxml.jackson.databind.module.SimpleModule;
import org.gcube.com.fasterxml.jackson.databind.ser.std.StdSerializer;
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanDataset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Custom Jackson module to serialize/deserialize as JSON Ckan objects with
* fields lower cased (i.e. 'author_email') and also Timestamp, which in Ckan
* has format like "2013-11-11T04:12:11.110868", see
* {@link CkanClient#CKAN_TIMESTAMP_PATTERN}. In case there are problems in
* parsing deserializes them to null.
*
* NOTE: We made a custom module because when reading dates, Jackson defaults to
* using GMT for all processing unless specifically told otherwise, see
* < href="http://wiki.fasterxml.com/JacksonFAQTimestampHandling" target="_blank">Jackson
* FAQ</a>. When writing dates, Jackson would also add a Z for timezone and add
* +1 for GMT, which we don't want.
*
* @author David Leoni
* @since 0.4.1
*/
public class JackanModule extends SimpleModule {
private static final Logger LOG = LoggerFactory.getLogger(JackanModule.class.getName());
public JackanModule() {
setNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
addSerializer(Timestamp.class, new StdSerializer<Timestamp>(Timestamp.class) {
@Override
public void serialize(Timestamp value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
try {
String str = formatTimestamp(value);
jgen.writeString(str);
}
catch (Exception ex) {
LOG.warn("Couldn't format timestamp " + value + ", writing 'null'", ex);
jgen.writeNull();
}
}
});
addDeserializer(Timestamp.class, new JsonDeserializer<Timestamp>() {
@Override
public Timestamp deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.VALUE_STRING) {
String str = jp.getText().trim();
try {
return CkanClient.parseTimestamp(str);
}
catch (IllegalArgumentException ex) {
LOG.warn("Couldn't parse timestamp " + str + ", returning null", ex);
return null;
}
}
if (t == JsonToken.VALUE_NULL) {
return null;
}
LOG.warn("Unrecognized json token for timestamp {0}, returning null", t.asString());
return null;
}
});
}
/**
* group org packages sometimes are arrays, sometimes numbers. If a number
* is found null is returned.
*/
public static class GroupOrgPackagesDeserializer extends JsonDeserializer<List<CkanDataset>> {
private static final Logger LOG = LoggerFactory.getLogger(GroupOrgPackagesDeserializer.class.getName());
@Override
public List<CkanDataset> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.VALUE_NUMBER_INT) {
return null;
}
if (t == JsonToken.START_ARRAY) {
return jp.readValueAs(new TypeReference<List<CkanDataset>>() {
});
}
LOG.warn("Unrecognized token {0} for 'packages' field, returning an empty array.", t.asString());
return new ArrayList();
}
}
}