001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *
010     *        http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License.
018     */
019    package org.apache.isis.viewer.xhtml.applib;
020    
021    import static org.apache.isis.viewer.xhtml.applib.StringUtils.asString;
022    import static org.apache.isis.viewer.xhtml.applib.UrlConnectionUtils.createGetConnection;
023    import static org.apache.isis.viewer.xhtml.applib.UrlConnectionUtils.createPostConnection;
024    import static org.apache.isis.viewer.xhtml.applib.UrlConnectionUtils.readDocFromConnectionInputStream;
025    import static org.apache.isis.viewer.xhtml.applib.UrlConnectionUtils.writeMapToConnectionOutputStream;
026    import static org.apache.isis.viewer.xhtml.applib.UrlEncodeUtils.urlEncode;
027    
028    import java.io.IOException;
029    import java.io.StringWriter;
030    import java.net.HttpURLConnection;
031    import java.net.ProtocolException;
032    import java.util.Map;
033    
034    import org.apache.log4j.Logger;
035    import org.jdom.Document;
036    import org.jdom.JDOMException;
037    import org.jdom.output.XMLOutputter;
038    
039    public abstract class AbstractRestfulClient {
040    
041        private static Logger LOG = Logger.getLogger(AbstractRestfulClient.class);
042    
043        private final String hostUri;
044    
045        public String getHostUri() {
046            return hostUri;
047        }
048    
049        public AbstractRestfulClient(final String hostUri) {
050            this.hostUri = hostUri;
051        }
052    
053        public Document get(final String uri) throws RestfulClientException {
054            if (LOG.isInfoEnabled()) {
055                LOG.info("getting from '" + uri + "'");
056            }
057            try {
058                final HttpURLConnection connection = createGetConnection(uri);
059                final Document document = readDocFromConnectionInputStream(connection);
060                if (LOG.isTraceEnabled()) {
061                    StringWriter sw = new StringWriter();
062                    new XMLOutputter().output(document, sw);
063                    LOG.trace(sw.toString());
064                }
065                return document;
066            } catch (final ProtocolException e) {
067                throw new RestfulClientException(e);
068            } catch (final IOException e) {
069                throw new RestfulClientException(e);
070            } catch (JDOMException e) {
071                throw new RestfulClientException(e);
072            }
073        }
074    
075        public org.jdom.Document post(final String uri, final String... paramArgs) {
076            return post(uri, StringUtils.asMap(paramArgs));
077        }
078    
079        private org.jdom.Document post(final String uri, final Map<String, String> formArgumentsByParameter) {
080            if (LOG.isInfoEnabled()) {
081                LOG.info("posting form arguments to '" + uri + "'");
082                LOG.info(asString(formArgumentsByParameter));
083            }
084            try {
085                final Map<String, String> encodedMap = urlEncode(formArgumentsByParameter);
086                if (LOG.isTraceEnabled()) {
087                    LOG.trace(asString(encodedMap));
088                }
089                final HttpURLConnection connection = createPostConnection(uri);
090                writeMapToConnectionOutputStream(encodedMap, connection);
091                return readDocFromConnectionInputStream(connection);
092            } catch (final IOException e) {
093                throw new RestfulClientException(e);
094            } catch (JDOMException e) {
095                throw new RestfulClientException(e);        }
096        }
097    
098        // //////////////////////////////////////////////////////////////////////
099        // Helpers: string
100        // //////////////////////////////////////////////////////////////////////
101    
102        protected static String combine(final String... pathParts) {
103            final StringBuilder buf = new StringBuilder();
104            for (final String part : pathParts) {
105                buf.append(part);
106            }
107            return buf.toString();
108        }
109    
110    }