001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo
018 */
019
020package org.nuxeo.ecm.platform.publisher.remoting.invoker;
021
022import org.apache.http.HttpEntity;
023import org.apache.http.HttpResponse;
024import org.apache.http.client.HttpClient;
025import org.apache.http.client.methods.HttpPost;
026import org.apache.http.entity.StringEntity;
027import org.apache.http.impl.client.DefaultHttpClient;
028import org.nuxeo.common.utils.Base64;
029import org.nuxeo.ecm.core.api.NuxeoException;
030import org.nuxeo.ecm.platform.publisher.remoting.marshaling.interfaces.RemotePublisherMarshaler;
031import org.nuxeo.ecm.platform.publisher.remoting.server.PublicationInvokationHandler;
032import org.nuxeo.ecm.platform.publisher.remoting.server.TestInvokationHandler;
033
034import java.io.BufferedReader;
035import java.io.IOException;
036import java.io.InputStreamReader;
037import java.util.List;
038
039/**
040 * Dummy test invoker: does all marshaling work but directly calls the {@link TestInvokationHandler} without any
041 * network.
042 *
043 * @author tiry
044 */
045public class DefaultRemotePublicationInvoker implements RemotePublicationInvoker {
046
047    protected String baseURL;
048
049    protected String userName;
050
051    protected String password;
052
053    protected RemotePublisherMarshaler marshaler;
054
055    protected PublicationInvokationHandler testPublicationHandler;
056
057    protected boolean useTestMode = false;
058
059    public void init(String baseURL, String userName, String password, RemotePublisherMarshaler marshaler) {
060        this.baseURL = baseURL;
061        this.userName = userName;
062        this.password = password;
063        this.marshaler = marshaler;
064
065        if (baseURL.startsWith("test")) {
066            useTestMode = true;
067            testPublicationHandler = new TestInvokationHandler(marshaler);
068        } else {
069            useTestMode = false;
070        }
071    }
072
073    public Object invoke(String methodName, List<Object> params) {
074
075        String marshaledData = marshaler.marshallParameters(params);
076
077        String result = doInvoke(methodName, marshaledData);
078
079        if (result == null)
080            return null;
081        return marshaler.unMarshallResult(result);
082    }
083
084    protected String doInvoke(String methodName, String marshaledData) {
085
086        if (useTestMode) {
087            return testPublicationHandler.invoke(methodName, marshaledData);
088        } else {
089            if (baseURL.startsWith("http")) {
090                try {
091                    return doHttpCall(methodName, marshaledData);
092                } catch (IOException e) {
093                    throw new NuxeoException("Error in http communication", e);
094                }
095            }
096            throw new NuxeoException("Unhandled protocol for url " + baseURL);
097        }
098    }
099
100    protected String doHttpCall(String methodName, String marshaledData) throws IOException {
101
102        HttpClient httpClient = new DefaultHttpClient();
103
104        String BAHeaderContent = userName + ":" + password;
105        BAHeaderContent = Base64.encodeBytes(BAHeaderContent.getBytes());
106        String BAHeader = "basic " + BAHeaderContent;
107
108        String targetUrl = baseURL + methodName;
109
110        HttpPost httpPost = new HttpPost(targetUrl);
111
112        HttpEntity entity = new StringEntity(marshaledData, "UTF-8");
113
114        httpPost.setEntity(entity);
115
116        httpPost.setHeader("content-type", "nuxeo/remotepub");
117        httpPost.setHeader("authorization", BAHeader);
118
119        HttpResponse response = httpClient.execute(httpPost);
120
121        HttpEntity responseEntity = response.getEntity();
122        if (responseEntity == null) {
123            return null;
124        }
125        InputStreamReader isr = new InputStreamReader(responseEntity.getContent(), "UTF-8");
126
127        BufferedReader br = new BufferedReader(isr);
128
129        StringBuffer sb = new StringBuffer();
130
131        int ch;
132        while ((ch = br.read()) > -1) {
133            sb.append((char) ch);
134        }
135        br.close();
136
137        String result = sb.toString();
138
139        return result;
140    }
141
142}