001/* 002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo 016 */ 017 018package org.nuxeo.ecm.platform.publisher.remoting.invoker; 019 020import org.apache.http.HttpEntity; 021import org.apache.http.HttpResponse; 022import org.apache.http.client.HttpClient; 023import org.apache.http.client.methods.HttpPost; 024import org.apache.http.entity.StringEntity; 025import org.apache.http.impl.client.DefaultHttpClient; 026import org.nuxeo.common.utils.Base64; 027import org.nuxeo.ecm.core.api.NuxeoException; 028import org.nuxeo.ecm.platform.publisher.remoting.marshaling.interfaces.RemotePublisherMarshaler; 029import org.nuxeo.ecm.platform.publisher.remoting.server.PublicationInvokationHandler; 030import org.nuxeo.ecm.platform.publisher.remoting.server.TestInvokationHandler; 031 032import java.io.BufferedReader; 033import java.io.IOException; 034import java.io.InputStreamReader; 035import java.util.List; 036 037/** 038 * Dummy test invoker: does all marshaling work but directly calls the {@link TestInvokationHandler} without any 039 * network. 040 * 041 * @author tiry 042 */ 043public class DefaultRemotePublicationInvoker implements RemotePublicationInvoker { 044 045 protected String baseURL; 046 047 protected String userName; 048 049 protected String password; 050 051 protected RemotePublisherMarshaler marshaler; 052 053 protected PublicationInvokationHandler testPublicationHandler; 054 055 protected boolean useTestMode = false; 056 057 public void init(String baseURL, String userName, String password, RemotePublisherMarshaler marshaler) { 058 this.baseURL = baseURL; 059 this.userName = userName; 060 this.password = password; 061 this.marshaler = marshaler; 062 063 if (baseURL.startsWith("test")) { 064 useTestMode = true; 065 testPublicationHandler = new TestInvokationHandler(marshaler); 066 } else { 067 useTestMode = false; 068 } 069 } 070 071 public Object invoke(String methodName, List<Object> params) { 072 073 String marshaledData = marshaler.marshallParameters(params); 074 075 String result = doInvoke(methodName, marshaledData); 076 077 if (result == null) 078 return null; 079 return marshaler.unMarshallResult(result); 080 } 081 082 protected String doInvoke(String methodName, String marshaledData) { 083 084 if (useTestMode) { 085 return testPublicationHandler.invoke(methodName, marshaledData); 086 } else { 087 if (baseURL.startsWith("http")) { 088 try { 089 return doHttpCall(methodName, marshaledData); 090 } catch (IOException e) { 091 throw new NuxeoException("Error in http communication", e); 092 } 093 } 094 throw new NuxeoException("Unhandled protocol for url " + baseURL); 095 } 096 } 097 098 protected String doHttpCall(String methodName, String marshaledData) throws IOException { 099 100 HttpClient httpClient = new DefaultHttpClient(); 101 102 String BAHeaderContent = userName + ":" + password; 103 BAHeaderContent = Base64.encodeBytes(BAHeaderContent.getBytes()); 104 String BAHeader = "basic " + BAHeaderContent; 105 106 String targetUrl = baseURL + methodName; 107 108 HttpPost httpPost = new HttpPost(targetUrl); 109 110 HttpEntity entity = new StringEntity(marshaledData, "UTF-8"); 111 112 httpPost.setEntity(entity); 113 114 httpPost.setHeader("content-type", "nuxeo/remotepub"); 115 httpPost.setHeader("authorization", BAHeader); 116 117 HttpResponse response = httpClient.execute(httpPost); 118 119 HttpEntity responseEntity = response.getEntity(); 120 if (responseEntity == null) { 121 return null; 122 } 123 InputStreamReader isr = new InputStreamReader(responseEntity.getContent(), "UTF-8"); 124 125 BufferedReader br = new BufferedReader(isr); 126 127 StringBuffer sb = new StringBuffer(); 128 129 int ch; 130 while ((ch = br.read()) > -1) { 131 sb.append((char) ch); 132 } 133 br.close(); 134 135 String result = sb.toString(); 136 137 return result; 138 } 139 140}