001: /*
002: * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Emil Ong
028: */
029:
030: package com.caucho.soa.rest;
031:
032: import com.caucho.jaxb.JAXBUtil;
033: import com.caucho.util.URLUtil;
034:
035: import javax.jws.WebMethod;
036: import javax.jws.WebParam;
037: import javax.xml.bind.JAXBContext;
038: import javax.xml.bind.JAXBException;
039: import javax.xml.bind.Marshaller;
040: import javax.xml.bind.Unmarshaller;
041: import java.io.InputStream;
042: import java.io.IOException;
043: import java.io.OutputStream;
044: import java.lang.annotation.Annotation;
045: import java.lang.reflect.InvocationHandler;
046: import java.lang.reflect.Method;
047: import java.net.HttpURLConnection;
048: import java.net.URL;
049: import java.net.URLConnection;
050: import java.util.ArrayList;
051: import java.util.HashMap;
052: import java.util.logging.Logger;
053:
054: public class JAXBRestProxy extends RestProxy {
055: private static final Logger log = Logger.getLogger(RestProxy.class
056: .getName());
057:
058: private JAXBContext _context;
059: private Marshaller _marshaller;
060: private Unmarshaller _unmarshaller;
061:
062: public JAXBRestProxy(Class api, String url) throws JAXBException {
063: super (api, url);
064:
065: ArrayList<Class> classList = new ArrayList<Class>();
066: JAXBUtil.introspectClass(_api, classList);
067:
068: _context = JAXBContext.newInstance(classList
069: .toArray(new Class[0]));
070: init();
071: }
072:
073: public JAXBRestProxy(Class api, String url, Class[] jaxbClasses)
074: throws JAXBException {
075: super (api, url);
076:
077: _context = JAXBContext.newInstance(jaxbClasses);
078: init();
079: }
080:
081: public JAXBRestProxy(Class api, String url, String jaxbPackages)
082: throws JAXBException {
083: super (api, url);
084:
085: _context = JAXBContext.newInstance(jaxbPackages);
086: init();
087: }
088:
089: private void init() throws JAXBException {
090: _marshaller = _context.createMarshaller();
091: _marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
092: Boolean.TRUE);
093: _unmarshaller = _context.createUnmarshaller();
094: }
095:
096: protected void writePostData(OutputStream out,
097: ArrayList<Object> postValues) throws IOException,
098: RestException {
099: try {
100: for (Object postValue : postValues)
101: _marshaller.marshal(postValue, out);
102: } catch (JAXBException e) {
103: throw new RestException(e);
104: }
105: }
106:
107: protected Object readResponse(InputStream in) throws IOException,
108: RestException {
109: try {
110: return _unmarshaller.unmarshal(in);
111: } catch (JAXBException e) {
112: throw new RestException(e);
113: }
114: }
115: }
|