001/*
002 * (C) Copyright 2006-2016 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 */
019package org.nuxeo.ecm.platform.publisher.remoting.invoker;
020
021import java.io.BufferedReader;
022import java.io.IOException;
023import java.io.InputStreamReader;
024import java.util.List;
025
026import org.apache.commons.codec.binary.Base64;
027import org.apache.http.HttpEntity;
028import org.apache.http.HttpResponse;
029import org.apache.http.client.HttpClient;
030import org.apache.http.client.methods.HttpPost;
031import org.apache.http.entity.StringEntity;
032import org.apache.http.impl.client.DefaultHttpClient;
033import org.nuxeo.ecm.core.api.NuxeoException;
034import org.nuxeo.ecm.platform.publisher.remoting.marshaling.interfaces.RemotePublisherMarshaler;
035import org.nuxeo.ecm.platform.publisher.remoting.server.PublicationInvokationHandler;
036import org.nuxeo.ecm.platform.publisher.remoting.server.TestInvokationHandler;
037
038/**
039 * Dummy test invoker: does all marshaling work but directly calls the {@link TestInvokationHandler} without any
040 * network.
041 *
042 * @author tiry
043 */
044public class DefaultRemotePublicationInvoker implements RemotePublicationInvoker {
045
046    protected String baseURL;
047
048    protected String userName;
049
050    protected String password;
051
052    protected RemotePublisherMarshaler marshaler;
053
054    protected PublicationInvokationHandler testPublicationHandler;
055
056    protected boolean useTestMode = false;
057
058    public void init(String baseURL, String userName, String password, RemotePublisherMarshaler marshaler) {
059        this.baseURL = baseURL;
060        this.userName = userName;
061        this.password = password;
062        this.marshaler = marshaler;
063
064        if (baseURL.startsWith("test")) {
065            useTestMode = true;
066            testPublicationHandler = new TestInvokationHandler(marshaler);
067        } else {
068            useTestMode = false;
069        }
070    }
071
072    public Object invoke(String methodName, List<Object> params) {
073
074        String marshaledData = marshaler.marshallParameters(params);
075
076        String result = doInvoke(methodName, marshaledData);
077
078        if (result == null)
079            return null;
080        return marshaler.unMarshallResult(result);
081    }
082
083    protected String doInvoke(String methodName, String marshaledData) {
084
085        if (useTestMode) {
086            return testPublicationHandler.invoke(methodName, marshaledData);
087        } else {
088            if (baseURL.startsWith("http")) {
089                try {
090                    return doHttpCall(methodName, marshaledData);
091                } catch (IOException e) {
092                    throw new NuxeoException("Error in http communication", e);
093                }
094            }
095            throw new NuxeoException("Unhandled protocol for url " + baseURL);
096        }
097    }
098
099    protected String doHttpCall(String methodName, String marshaledData) throws IOException {
100
101        HttpClient httpClient = new DefaultHttpClient();
102
103        String BAHeaderContent = userName + ":" + password;
104        BAHeaderContent = Base64.encodeBase64String(BAHeaderContent.getBytes());
105        String BAHeader = "basic " + BAHeaderContent;
106
107        String targetUrl = baseURL + methodName;
108
109        HttpPost httpPost = new HttpPost(targetUrl);
110
111        HttpEntity entity = new StringEntity(marshaledData, "UTF-8");
112
113        httpPost.setEntity(entity);
114
115        httpPost.setHeader("content-type", "nuxeo/remotepub");
116        httpPost.setHeader("authorization", BAHeader);
117
118        HttpResponse response = httpClient.execute(httpPost);
119
120        HttpEntity responseEntity = response.getEntity();
121        if (responseEntity == null) {
122            return null;
123        }
124        InputStreamReader isr = new InputStreamReader(responseEntity.getContent(), "UTF-8");
125
126        BufferedReader br = new BufferedReader(isr);
127
128        StringBuilder sb = new StringBuilder();
129
130        int ch;
131        while ((ch = br.read()) > -1) {
132            sb.append((char) ch);
133        }
134        br.close();
135
136        String result = sb.toString();
137
138        return result;
139    }
140
141}