view src/main/java/de/intevation/artifacts/httpclient/http/HttpClientImpl.java @ 8:77c09d0e2c2e

Tagged RELEASE 0.1 of HTTP Client. http-client/trunk@1623 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 30 Mar 2011 13:46:07 +0000
parents 06e9e25632cd
children 072e8d488f83
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */
package de.intevation.artifacts.httpclient.http;

import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.log4j.Logger;

import org.restlet.Client;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Protocol;
import org.restlet.data.Status;
import org.restlet.ext.xml.DomRepresentation;
import org.restlet.representation.Representation;

import org.w3c.dom.Document;

import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler;
import de.intevation.artifacts.httpclient.http.response.ResponseHandler;
import de.intevation.artifacts.httpclient.http.response.StreamResponseHandler;
import de.intevation.artifacts.httpclient.objects.Artifact;
import de.intevation.artifacts.httpclient.objects.ArtifactFactory;
import de.intevation.artifacts.httpclient.utils.ArtifactProtocolUtils;
import de.intevation.artifacts.httpclient.utils.ArtifactCreator;


/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class HttpClientImpl implements HttpClient {

    private static final Logger logger = Logger.getLogger(HttpClient.class);

    /** The URL part of the resource to list the existing users of the server.*/
    public static final String PATH_LIST_USERS = "/list-users";

    /** The URL part of the resource to list the Collections owned by a specific
     * user.*/
    public static final String PATH_USER_COLLECTIONS = "/list-collections";

    /** The URL part of the resource to call a specific service.*/
    public static final String PATH_SERVICE = "/service";

    /** The URL path of the resource to create new artifact collections.*/
    public static final String PATH_CREATE_COLLECTION = "/create-collection";

    /** The URL path of the resource to work with an artifact collections.*/
    public static final String PATH_ACTION_COLLECTION = "/collection";

    private String serverUrl;


    public HttpClientImpl(String serverUrl) {
        this.serverUrl = serverUrl;
    }


    @Override
    public ArtifactFactory[] getArtifactFactories()
    throws ConnectionException
    {
        ResponseHandler handler = new DocumentResponseHandler();

        try {
            String    url   = serverUrl + "/factories";
            Document result = (Document) handler.handle(doGet(url));

            return ArtifactProtocolUtils.extractArtifactFactories(result);
        }
        catch (IOException ioe) {
            throw new ConnectionException(
                "Connection to server failed. No Factories recieved.");
        }
    }


    /**
     * This method creates a new artifact in the artifact server and returns
     * this artifact. The new artifact is created using <i>creator</i>. If no
     * {@link ArtifactCreator} is given (null), an {@link Artifact} is returned.
     *
     * @param doc The CREATE document.
     * @param creator The {@link ArtifactCreator} that is used to extract the
     * new artifact from response document of the server.
     *
     * @return the new artifact.
     */
    @Override
    public Object create(Document doc, ArtifactCreator creator)
    throws ConnectionException
    {
        ResponseHandler handler = new DocumentResponseHandler();

        try {
            String   url    = serverUrl + "/create";
            Document result = (Document) handler.handle(doPost(url, doc));

            return creator == null
                ? ArtifactProtocolUtils.extractArtifact(result)
                : creator.create(result);
        }
        catch (IOException ioe) {
            throw new ConnectionException(
                "Connection to server failed. No Artifact created.");
        }
    }


    @Override
    public Object describe(
        Artifact        artifact,
        Document        doc,
        ResponseHandler handler)
    throws ConnectionException
    {
        try {
            String   url    = serverUrl + "/artifact/" + artifact.getUuid();
            return handler.handle(doPost(url, doc));
        }
        catch (IOException ioe) {
            throw new ConnectionException(
                "Connection to server failed: " + ioe.getMessage());
        }
    }


    @Override
    public Object feed(Artifact artifact, Document doc, ResponseHandler handler)
    throws ConnectionException
    {
        try {
            String   url    = serverUrl + "/artifact/" + artifact.getUuid();
            Document result = (Document) handler.handle(doPost(url, doc));

            return result;
        }
        catch (IOException ioe) {
            throw new ConnectionException(
                "Connection to server failed: " + ioe.getMessage());
        }
    }


    @Override
    public Object advance(Artifact artifact, Document doc, ResponseHandler handler)
    throws ConnectionException
    {
        try {
            String   url    = serverUrl + "/artifact/" + artifact.getUuid();
            Document result = (Document) handler.handle(doPost(url, doc));

            return result;
        }
        catch (IOException ioe) {
            throw new ConnectionException(
                "Connection to server failed: " + ioe.getMessage());
        }
    }


    @Override
    public void out(
        Artifact     artifact,
        Document     doc,
        String       target,
        OutputStream out)
    throws ConnectionException
    {
        try {
            String url =
                serverUrl
                + "/artifact/"
                + artifact.getUuid()
                + "/" + target;

            ResponseHandler handler = new StreamResponseHandler();

            InputStream stream = (InputStream) handler.handle(doPost(url, doc));

            byte[] b = new byte[4096];
            int i = -1;
            while ((i = stream.read(b)) > 0) {
                out.write(b, 0, i);
            }
        }
        catch (IOException ioe) {
            throw new ConnectionException(
                "Connection to server failed: " + ioe.getMessage());
        }
    }


    private Response doPost(String url, Document body) throws IOException {
        logger.info("Start HTTP-POST request to: "+ url);

        Client client   = new Client(Protocol.HTTP);
        Request request = new Request(Method.POST, url);

        Representation representation = new DomRepresentation(
            MediaType.APPLICATION_XML,
            body);

        request.setEntity(representation);
        Response response = client.handle(request);

        Status status = response.getStatus();
        if (status.getCode() != 200) {
            logger.error("Response status: " + status.getCode());
            throw new IOException(status.getDescription());
        }

        return response;
    }


    private Response doGet(String url) throws IOException {
        logger.info("Start HTTP-POST request to: "+ url);

        Client client   = new Client(Protocol.HTTP);
        Request request = new Request(Method.GET, url);

        Response response = client.handle(request);

        Status status = response.getStatus();
        if (status.getCode() != 200) {
            logger.error("Response status: " + status.getCode());
            throw new IOException(status.getDescription());
        }

        return response;
    }


    //==============================
    // Collection API
    //==============================

    /**
     * This method triggers the artifact servers resource to create a new
     * artifact collection.
     *
     * @param create The CREATE document for the collection.
     * @param ownerId The uuid of the creator.
     * @param handler The handler that is used to create the result object.
     *
     * @return a result object created by <i>handler</i>.
     */
    public Object createCollection(
        Document        create,
        String          ownerId,
        ResponseHandler handler)
    throws ConnectionException
    {
        String url = serverUrl + PATH_CREATE_COLLECTION + "/" + ownerId;

        try {
            return handler.handle(doPost(url, create));
        }
        catch (IOException ioe) {
            throw new ConnectionException(ioe.getMessage(), ioe);
        }
    }


    /**
     * This method might be used to trigger a collection specific action. The
     * action that is executed depends on the document <i>actionDoc</i>.
     *
     * @param actionDoc The document that describes the action to be executed.
     * @param uuid      The uuid of the collection.
     * @param handler   The handler that is used to create the result object.
     *
     * @return a result object created by <i>handler</i>.
     */
    public Object doCollectionAction(
        Document        actionDoc,
        String          uuid,
        ResponseHandler handler)
    throws ConnectionException
    {
        String url = serverUrl + PATH_ACTION_COLLECTION + "/" + uuid;

        try {
            return handler.handle(doPost(url, actionDoc));
        }
        catch (IOException ioe) {
            throw new ConnectionException(ioe.getMessage(), ioe);
        }
    }


    /*******************************
     * Service API
     *******************************/

     public Document callService(String url, String service, Document input)
     throws ConnectionException
     {
        logger.info("Start service call to '" + service + "'");

        DocumentResponseHandler handler = new DocumentResponseHandler();

        try {
            String serverUrl = url + PATH_SERVICE + "/" + service;
            return (Document) handler.handle(doPost(serverUrl, input));
        }
        catch (IOException ioe) {
            throw new ConnectionException(
                "Connection to server failed: " + ioe.getMessage());
        }
     }


    /*******************************
     * Users API
     *******************************/

    public Document listUsers()
    throws ConnectionException
    {
        ResponseHandler handler = new DocumentResponseHandler();
        String    url           = serverUrl + PATH_LIST_USERS;

        try {
            return (Document) handler.handle(doGet(url));
        }
        catch (IOException ioe) {
            throw new ConnectionException(ioe.getMessage(), ioe);
        }
    }


    public Document listUserCollections(String userid)
    throws ConnectionException
    {
        ResponseHandler handler = new DocumentResponseHandler();

        String url = serverUrl + PATH_USER_COLLECTIONS + "/" + userid;

        try {
            return (Document) handler.handle(doGet(url));
        }
        catch (IOException ioe) {
            throw new ConnectionException(ioe.getMessage(), ioe);
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org