From e9b71f34bde55a9aae3517246ad7041beaa42ef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francesco=20Chicchiricc=C3=B2?= Date: Fri, 11 Apr 2014 13:49:14 +0200 Subject: [PATCH] [OLINGO-227] Fixing again windows build --- .../apache/olingo/fit/AbstractServices.java | 29 ++++++----- .../apache/olingo/fit/metadata/Metadata.java | 2 +- .../fit/serializer/FITAtomDeserializer.java | 49 +++++++++++++++++++ .../org/apache/olingo/fit/utils/Commons.java | 17 +++---- .../fit/utils/XMLEventReaderWrapper.java | 10 ++-- .../commons/core/data/AtomDeserializer.java | 16 +++--- 6 files changed, 86 insertions(+), 37 deletions(-) create mode 100644 fit/src/main/java/org/apache/olingo/fit/serializer/FITAtomDeserializer.java diff --git a/fit/src/main/java/org/apache/olingo/fit/AbstractServices.java b/fit/src/main/java/org/apache/olingo/fit/AbstractServices.java index f12898195..3f445db29 100644 --- a/fit/src/main/java/org/apache/olingo/fit/AbstractServices.java +++ b/fit/src/main/java/org/apache/olingo/fit/AbstractServices.java @@ -24,7 +24,6 @@ import org.apache.olingo.commons.api.data.Link; import org.apache.olingo.commons.api.data.Property; import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.core.data.AtomDeserializer; import org.apache.olingo.commons.core.data.AtomFeedImpl; import org.apache.olingo.commons.core.data.LinkImpl; import org.apache.olingo.fit.metadata.Metadata; @@ -89,6 +88,7 @@ import org.apache.olingo.fit.utils.FSManager; import org.apache.olingo.fit.utils.Commons; import org.apache.olingo.fit.methods.MERGE; import org.apache.olingo.fit.methods.PATCH; +import org.apache.olingo.fit.serializer.FITAtomDeserializer; import org.apache.olingo.fit.utils.AbstractJSONUtilities; import org.apache.olingo.fit.utils.AbstractUtilities; import org.apache.olingo.fit.utils.AbstractXMLUtilities; @@ -196,7 +196,7 @@ public abstract class AbstractServices { return utils.getValue().createResponse( FSManager.instance(version).readFile(Constants.get(version, ConstantKey.REF) - + File.separatorChar + filename, utils.getKey()), + + File.separatorChar + filename, utils.getKey()), null, utils.getKey()); } catch (Exception e) { @@ -243,8 +243,7 @@ public abstract class AbstractServices { InputStream res = util.patchEntity(entitySetName, entityId, IOUtils.toInputStream(changes), acceptType, ifMatch); - - final AtomDeserializer atomDeserializer = Commons.getAtomDeserializer(version); + final FITAtomDeserializer atomDeserializer = Commons.getAtomDeserializer(version); final ObjectMapper mapper = Commons.getJsonMapper(version); @@ -308,7 +307,7 @@ public abstract class AbstractServices { res = json.addOrReplaceEntity(entityId, entitySetName, IOUtils.toInputStream(entity)); } - final AtomDeserializer atomDeserializer = Commons.getAtomDeserializer(version); + final FITAtomDeserializer atomDeserializer = Commons.getAtomDeserializer(version); final ObjectMapper mapper = Commons.getJsonMapper(version); final Container cres; @@ -367,7 +366,7 @@ public abstract class AbstractServices { utils = getUtilities(acceptType); - final AtomDeserializer atomDeserializer = Commons.getAtomDeserializer(version); + final FITAtomDeserializer atomDeserializer = Commons.getAtomDeserializer(version); final AtomSerializer atomSerializer = Commons.getAtomSerializer(version); final ObjectMapper mapper = Commons.getJsonMapper(version); @@ -415,7 +414,7 @@ public abstract class AbstractServices { } else { final Container jcontainer = mapper.readValue(IOUtils.toInputStream(entity), new TypeReference() { - }); + }); entry = (new DataBinder(version)). getAtomEntry(jcontainer.getObject()); @@ -488,13 +487,13 @@ public abstract class AbstractServices { replaceAll("\"Salary\":[0-9]*,", "\"Salary\":0,"). replaceAll("\"Title\":\".*\"", "\"Title\":\"[Sacked]\""). replaceAll("\\.*\\", - "0"). + "0"). replaceAll("\\.*\\", "[Sacked]"); final FSManager fsManager = FSManager.instance(version); fsManager.putInMemory(IOUtils.toInputStream(newContent, "UTF-8"), fsManager.getAbsolutePath(Commons.getEntityBasePath("Person", entityId) + Constants.get(version, - ConstantKey.ENTITY), utils.getKey())); + ConstantKey.ENTITY), utils.getKey())); return utils.getValue().createResponse(null, null, utils.getKey(), Response.Status.NO_CONTENT); } catch (Exception e) { @@ -546,9 +545,9 @@ public abstract class AbstractServices { final Long newSalary = Long.valueOf(salaryMatcher.group(1)) + n; newContent = newContent. replaceAll("\"Salary\":" + salaryMatcher.group(1) + ",", - "\"Salary\":" + newSalary + ","). + "\"Salary\":" + newSalary + ","). replaceAll("\\" + salaryMatcher.group(1) + "", - "" + newSalary + ""); + "" + newSalary + ""); } FSManager.instance(version).putInMemory(IOUtils.toInputStream(newContent, "UTF-8"), @@ -662,7 +661,7 @@ public abstract class AbstractServices { final InputStream feed = FSManager.instance(version).readFile(builder.toString(), Accept.ATOM); - final AtomDeserializer atomDeserializer = Commons.getAtomDeserializer(version); + final FITAtomDeserializer atomDeserializer = Commons.getAtomDeserializer(version); final AtomSerializer atomSerializer = Commons.getAtomSerializer(version); final Container container = atomDeserializer.read(feed, AtomFeedImpl.class); @@ -680,7 +679,7 @@ public abstract class AbstractServices { mapper.writeValue( writer, new JsonFeedContainer(container.getContextURL(), container.getMetadataETag(), - (new DataBinder(version)).getJsonFeed((AtomFeedImpl) container.getObject()))); + (new DataBinder(version)).getJsonFeed((AtomFeedImpl) container.getObject()))); } return xml.createResponse(new ByteArrayInputStream(content.toByteArray()), @@ -780,7 +779,7 @@ public abstract class AbstractServices { InputStream entity = entityInfo.getValue(); - final AtomDeserializer atomDeserializer = Commons.getAtomDeserializer(version); + final FITAtomDeserializer atomDeserializer = Commons.getAtomDeserializer(version); final AtomSerializer atomSerializer = Commons.getAtomSerializer(version); final Container container = atomDeserializer.read(entity, AtomEntryImpl.class); @@ -865,7 +864,7 @@ public abstract class AbstractServices { final ObjectMapper mapper = Commons.getJsonMapper(version); mapper.writeValue( writer, new JsonEntryContainer(container.getContextURL(), container.getMetadataETag(), - (new DataBinder(version)).getJsonEntry((AtomEntryImpl) container.getObject()))); + (new DataBinder(version)).getJsonEntry((AtomEntryImpl) container.getObject()))); } return xml.createResponse(new ByteArrayInputStream(content.toByteArray()), diff --git a/fit/src/main/java/org/apache/olingo/fit/metadata/Metadata.java b/fit/src/main/java/org/apache/olingo/fit/metadata/Metadata.java index 84ad0438f..737f1c0a7 100644 --- a/fit/src/main/java/org/apache/olingo/fit/metadata/Metadata.java +++ b/fit/src/main/java/org/apache/olingo/fit/metadata/Metadata.java @@ -54,7 +54,7 @@ public class Metadata extends AbstractMetadataElement { try { while (reader.hasNext()) { - XMLEvent event = reader.nextEvent(); + final XMLEvent event = reader.nextEvent(); if (event.isStartElement() && event.asStartElement().getName().equals(new QName("Schema"))) { final Schema schema = getSchema(event.asStartElement(), reader); diff --git a/fit/src/main/java/org/apache/olingo/fit/serializer/FITAtomDeserializer.java b/fit/src/main/java/org/apache/olingo/fit/serializer/FITAtomDeserializer.java new file mode 100644 index 000000000..515fd066d --- /dev/null +++ b/fit/src/main/java/org/apache/olingo/fit/serializer/FITAtomDeserializer.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.olingo.fit.serializer; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CodingErrorAction; +import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLStreamException; +import org.apache.olingo.commons.api.Constants; +import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion; +import org.apache.olingo.commons.core.data.AtomDeserializer; + +public class FITAtomDeserializer extends AtomDeserializer { + + private static final Charset ENCODING = Charset.forName(Constants.UTF8); + + public FITAtomDeserializer(final ODataServiceVersion version) { + super(version); + } + + @Override + protected XMLEventReader getReader(final InputStream input) throws XMLStreamException { + final CharsetDecoder decoder = ENCODING.newDecoder(); + decoder.onMalformedInput(CodingErrorAction.IGNORE); + decoder.onUnmappableCharacter(CodingErrorAction.IGNORE); + + return FACTORY.createXMLEventReader(new InputStreamReader(input, decoder)); + } + +} diff --git a/fit/src/main/java/org/apache/olingo/fit/utils/Commons.java b/fit/src/main/java/org/apache/olingo/fit/utils/Commons.java index d4f6267a3..7cec7b9fb 100644 --- a/fit/src/main/java/org/apache/olingo/fit/utils/Commons.java +++ b/fit/src/main/java/org/apache/olingo/fit/utils/Commons.java @@ -42,10 +42,10 @@ import java.util.Map; import java.util.regex.Pattern; import org.apache.commons.io.IOUtils; import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion; -import org.apache.olingo.commons.core.data.AtomDeserializer; import org.apache.olingo.commons.core.data.AtomSerializer; import org.apache.olingo.commons.core.op.InjectableSerializerProvider; import org.apache.olingo.fit.metadata.Metadata; +import org.apache.olingo.fit.serializer.FITAtomDeserializer; import org.codehaus.plexus.util.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -57,8 +57,8 @@ public abstract class Commons { */ protected static final Logger LOG = LoggerFactory.getLogger(Commons.class); - private static Map atomDeserializer = - new EnumMap(ODataServiceVersion.class); + private static Map atomDeserializer = + new EnumMap(ODataServiceVersion.class); private static Map atomSerializer = new EnumMap(ODataServiceVersion.class); @@ -91,9 +91,9 @@ public abstract class Commons { mediaContent.put("Car/Photo", null); } - public static AtomDeserializer getAtomDeserializer(final ODataServiceVersion version) { + public static FITAtomDeserializer getAtomDeserializer(final ODataServiceVersion version) { if (!atomDeserializer.containsKey(version)) { - atomDeserializer.put(version, new AtomDeserializer(version)); + atomDeserializer.put(version, new FITAtomDeserializer(version)); } return atomDeserializer.get(version); } @@ -128,10 +128,7 @@ public abstract class Commons { public static Metadata getMetadata(final ODataServiceVersion version) { if (!metadata.containsKey(version)) { - final InputStream is = Commons.class.getResourceAsStream( - File.separatorChar - + version.name() - + File.separatorChar + "metadata.xml"); + final InputStream is = Commons.class.getResourceAsStream("/" + version.name() + "/metadata.xml"); metadata.put(version, new Metadata(is)); } @@ -178,7 +175,7 @@ public abstract class Commons { try { return FSManager.instance(version) .getAbsolutePath(basePath + Constants.get(version, ConstantKey.LINKS_FILE_PATH) - + File.separatorChar + linkName, accept); + + File.separatorChar + linkName, accept); } catch (Exception e) { throw new IOException(e); } diff --git a/fit/src/main/java/org/apache/olingo/fit/utils/XMLEventReaderWrapper.java b/fit/src/main/java/org/apache/olingo/fit/utils/XMLEventReaderWrapper.java index f75f988d4..5c88a42e8 100644 --- a/fit/src/main/java/org/apache/olingo/fit/utils/XMLEventReaderWrapper.java +++ b/fit/src/main/java/org/apache/olingo/fit/utils/XMLEventReaderWrapper.java @@ -32,7 +32,7 @@ import org.apache.commons.io.IOUtils; public class XMLEventReaderWrapper implements XMLEventReader { - private static Charset encoding = Charset.forName("UTF-8"); + private static final Charset ENCODING = Charset.forName("UTF-8"); public final static String CONTENT = "CONTENT_TAG"; @@ -49,14 +49,14 @@ public class XMLEventReaderWrapper implements XMLEventReader { factory.setProperty(XMLInputFactory.IS_VALIDATING, false); factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false); - final CharsetDecoder decoder = encoding.newDecoder(); + final CharsetDecoder decoder = ENCODING.newDecoder(); decoder.onMalformedInput(CodingErrorAction.IGNORE); decoder.onUnmappableCharacter(CodingErrorAction.IGNORE); - InputStreamReader reader = new InputStreamReader( + final InputStreamReader reader = new InputStreamReader( new ByteArrayInputStream((XMLEventReaderWrapper.CONTENT_STAG - + IOUtils.toString(stream, encoding).replaceAll("^<\\?xml.*\\?>", "") - + XMLEventReaderWrapper.CONTENT_ETAG).getBytes(encoding)), + + IOUtils.toString(stream, ENCODING).replaceAll("^<\\?xml.*\\?>", "") + + XMLEventReaderWrapper.CONTENT_ETAG).getBytes(ENCODING)), decoder); this.wrapped = factory.createXMLEventReader(reader); diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomDeserializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomDeserializer.java index 850611b27..f9a9c8241 100644 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomDeserializer.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AtomDeserializer.java @@ -43,10 +43,14 @@ import org.apache.olingo.commons.core.edm.EdmTypeInfo; public class AtomDeserializer extends AbstractAtomDealer { - private static final XMLInputFactory FACTORY = XMLInputFactory.newInstance(); + protected static final XMLInputFactory FACTORY = XMLInputFactory.newInstance(); private final AtomGeoValueDeserializer geoDeserializer; + protected XMLEventReader getReader(final InputStream input) throws XMLStreamException { + return FACTORY.createXMLEventReader(input); + } + public AtomDeserializer(final ODataServiceVersion version) { super(version); this.geoDeserializer = new AtomGeoValueDeserializer(); @@ -284,7 +288,7 @@ public class AtomDeserializer extends AbstractAtomDealer { } private Container property(final InputStream input) throws XMLStreamException { - final XMLEventReader reader = FACTORY.createXMLEventReader(input); + final XMLEventReader reader = getReader(input); final StartElement start = skipBeforeFirstStartElement(reader); return getContainer(start, property(reader, start)); } @@ -359,7 +363,7 @@ public class AtomDeserializer extends AbstractAtomDealer { } private Container linkCollection(final InputStream input) throws XMLStreamException { - final XMLEventReader reader = FACTORY.createXMLEventReader(input); + final XMLEventReader reader = getReader(input); final StartElement start = skipBeforeFirstStartElement(reader); return getContainer(start, linkCollection(reader, start)); } @@ -545,7 +549,7 @@ public class AtomDeserializer extends AbstractAtomDealer { } private Container entry(final InputStream input) throws XMLStreamException { - final XMLEventReader reader = FACTORY.createXMLEventReader(input); + final XMLEventReader reader = getReader(input); final StartElement start = skipBeforeFirstStartElement(reader); return getContainer(start, entry(reader, start)); } @@ -615,7 +619,7 @@ public class AtomDeserializer extends AbstractAtomDealer { } private Container feed(final InputStream input) throws XMLStreamException { - final XMLEventReader reader = FACTORY.createXMLEventReader(input); + final XMLEventReader reader = getReader(input); final StartElement start = skipBeforeFirstStartElement(reader); return getContainer(start, feed(reader, start)); } @@ -671,7 +675,7 @@ public class AtomDeserializer extends AbstractAtomDealer { } private Container error(final InputStream input) throws XMLStreamException { - final XMLEventReader reader = FACTORY.createXMLEventReader(input); + final XMLEventReader reader = getReader(input); final StartElement start = skipBeforeFirstStartElement(reader); return getContainer(start, error(reader, start)); }