org.apache.wink.common.internal.model.ModelUtils Maven / Gradle / Ivy
/*******************************************************************************
* 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.wink.common.internal.model;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Set;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Providers;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.apache.wink.common.RestException;
import org.apache.wink.common.RuntimeContext;
import org.apache.wink.common.WinkApplication;
import org.apache.wink.common.internal.MultivaluedMapImpl;
import org.apache.wink.common.internal.application.ApplicationFileLoader;
import org.apache.wink.common.internal.application.ApplicationValidator;
import org.apache.wink.common.internal.contexts.ProvidersImpl;
import org.apache.wink.common.internal.i18n.Messages;
import org.apache.wink.common.internal.lifecycle.LifecycleManagersRegistry;
import org.apache.wink.common.internal.lifecycle.ScopeLifecycleManager;
import org.apache.wink.common.internal.registry.ProvidersRegistry;
import org.apache.wink.common.internal.registry.metadata.ProviderMetadataCollector;
import org.apache.wink.common.internal.runtime.AbstractRuntimeContext;
import org.apache.wink.common.internal.runtime.RuntimeContextTLS;
import org.apache.wink.common.internal.utils.UnmodifiableMultivaluedMap;
import org.apache.wink.common.model.atom.AtomContent;
import org.apache.wink.common.model.atom.AtomText;
import org.apache.wink.common.model.atom.AtomTextType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.LexicalHandler;
public class ModelUtils {
public static final MultivaluedMap EMPTY_OBJECT_MAP =
new UnmodifiableMultivaluedMap(
new MultivaluedMapImpl());
public static final MultivaluedMap EMPTY_STRING_MAP =
new UnmodifiableMultivaluedMap(
new MultivaluedMapImpl());
public static final Annotation[] EMPTY_ARRAY = new Annotation[0];
private final static SAXParserFactory spf;
private final static DatatypeFactory datatypeFactory;
private static final Logger logger =
LoggerFactory
.getLogger(ModelUtils.class);
static {
try {
spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(false);
datatypeFactory = DatatypeFactory.newInstance();
} catch (Exception e) {
throw new RestException(Messages.getMessage("errorSettingUpAtom", e)); //$NON-NLS-1$
}
}
public static boolean isTypeXml(String type) {
// remove parameters if they exist
int index = type.indexOf(';');
if (index > -1) {
type = type.substring(0, index).trim();
}
// as per RFC3023 and Atom specification
type = type.toLowerCase();
if (type.endsWith("/xml") || type.endsWith("+xml") //$NON-NLS-1$ //$NON-NLS-2$
|| type.equals("xhtml") //$NON-NLS-1$
|| type.equals("text/xml-external-parsed-entity") //$NON-NLS-1$
|| type.equals("application/xml-external-parsed-entity") //$NON-NLS-1$
|| type.equals("application/xml-dtd")) { //$NON-NLS-1$
return true;
}
return false;
}
public static boolean isValueActuallyXml(Object source) {
if (source instanceof AtomContent) {
AtomContent content = (AtomContent)source;
String type = content.getType();
if (ModelUtils.isTypeXml(type)) {
return true;
}
} else if (source instanceof AtomText) {
AtomText text = (AtomText)source;
AtomTextType type = text.getType();
if (type == AtomTextType.xhtml) {
return true;
}
}
return false;
}
public static void saxParse(Reader reader, ContentHandler handler, String errorMessage) {
XMLReader xmlReader;
try {
xmlReader = spf.newSAXParser().getXMLReader();
xmlReader.setContentHandler(handler);
// setting this property will cause the handler to get lexical
// events as well
if (handler instanceof LexicalHandler) {
xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler); //$NON-NLS-1$
}
xmlReader.parse(new InputSource(reader));
} catch (SAXException e) {
logger.error(errorMessage);
throw new WebApplicationException(e);
} catch (ParserConfigurationException e) {
logger.error(errorMessage);
throw new WebApplicationException(e);
} catch (IOException e) {
logger.error(errorMessage);
throw new WebApplicationException(e);
}
}
public static Object unmarshal(Unmarshaller unmarshaller, Reader reader) throws IOException {
Object result = null;
try {
result = unmarshaller.unmarshal(reader);
if (result instanceof JAXBElement>) {
result = ((JAXBElement>)result).getValue();
}
} catch (IllegalStateException e) {
throw new WebApplicationException(e);
} catch (JAXBException e) {
throw new WebApplicationException(e);
}
return result;
}
public static void marshal(Marshaller marshaller, Object jaxbObject, OutputStream os)
throws IOException {
try {
marshaller.marshal(jaxbObject, os);
} catch (JAXBException e) {
throw new WebApplicationException(e);
}
}
public static XMLGregorianCalendar timeToXmlGregorianCalendar(long time) {
if (time == -1) {
return null;
}
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(time);
XMLGregorianCalendar xmlGregCal = datatypeFactory.newXMLGregorianCalendar(calendar);
return xmlGregCal;
}
public static long xmlGregorianCalendarToTime(XMLGregorianCalendar xmlGregCal) {
if (xmlGregCal == null) {
return -1;
}
Calendar calendar = xmlGregCal.toGregorianCalendar();
long time = calendar.getTimeInMillis();
return time;
}
@SuppressWarnings("unchecked")
public static T readValue(List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy