KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > util > PublisherUtil


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * Created June 22, 2006
14  * @author mdamour
15  * @author mbatchelor
16  */

17 package org.pentaho.core.util;
18
19 import java.io.File JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.security.MessageDigest JavaDoc;
23
24 import org.apache.commons.httpclient.Credentials;
25 import org.apache.commons.httpclient.HttpClient;
26 import org.apache.commons.httpclient.HttpException;
27 import org.apache.commons.httpclient.HttpStatus;
28 import org.apache.commons.httpclient.UsernamePasswordCredentials;
29 import org.apache.commons.httpclient.auth.AuthScope;
30 import org.apache.commons.httpclient.methods.PostMethod;
31 import org.apache.commons.httpclient.methods.multipart.FilePart;
32 import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
33 import org.apache.commons.httpclient.methods.multipart.Part;
34 import org.dom4j.Document;
35 import org.dom4j.Node;
36 import org.pentaho.core.admin.datasources.DataSourceInfo;
37 import org.pentaho.core.repository.ISolutionRepository;
38 import org.pentaho.core.system.PentahoSystem;
39 import org.pentaho.messages.Messages;
40
41 public class PublisherUtil {
42     /**
43      * Provides utility methods for publishing solution files to the pentaho server.
44      *
45      */

46     private static final String JavaDoc PublishConfigFile = "publisher_config.xml"; //$NON-NLS-1$
47

48     /**
49      * Publishes a list of files only to the server.
50      *
51      * @param publishURL
52      * The URL of the Pentaho server
53      * @param publishPath
54      * The path in the solution to place the files
55      * @param publishFiles
56      * Array of File objects to post to the server
57      * @param publishPassword
58      * The publishing password for the server
59      * @return Server response as a string.
60      */

61     public static int publish(String JavaDoc publishURL, String JavaDoc publishPath, File JavaDoc publishFiles[], String JavaDoc publishPassword, boolean overwrite) {
62         return publish(publishURL, publishPath, publishFiles, null, publishPassword, null, null, overwrite);
63     }
64
65     /**
66      * Publishes a list of files only to the server where the server requires basic authentication.
67      *
68      * @param publishURL
69      * The URL of the Pentaho server
70      * @param publishPath
71      * The path in the solution to place the files
72      * @param publishFiles
73      * Array of File objects to post to the server
74      * @param publishPassword
75      * The publishing password for the server
76      * @param serverUserid
77      * The userid to authenticate to the server
78      * @param serverPassword
79      * The password to authenticate with the server
80      * @return Server response as a string
81      */

82     public static int publish(String JavaDoc publishURL, String JavaDoc publishPath, File JavaDoc publishFiles[], String JavaDoc publishPassword, String JavaDoc serverUserid, String JavaDoc serverPassword, boolean overwrite) {
83         return publish(publishURL, publishPath, publishFiles, null, publishPassword, serverUserid, serverPassword, overwrite);
84     }
85
86     /**
87      * Publishes a list of files and a datasource to the server.
88      *
89      * @param publishURL
90      * The URL of the Pentaho server
91      * @param publishPath
92      * The path in the solution to place the files
93      * @param publishFiles
94      * Array of File objects to post to the server
95      * @param dataSource
96      * The datasource to publish to the server
97      * @param publishPassword
98      * The publishing password for the server
99      * @return Server response as a string
100      */

101     public static int publish(String JavaDoc publishURL, String JavaDoc publishPath, File JavaDoc publishFiles[], DataSourceInfo dataSource, String JavaDoc publishPassword, boolean overwrite) {
102         return publish(publishURL, publishPath, publishFiles, dataSource, publishPassword, null, null, overwrite);
103     }
104
105     /**
106      * Publishes a list of files and a datasource to the server with basic authentication to the server
107      *
108      * @param publishURL
109      * The URL of the Pentaho server
110      * @param publishPath
111      * The path in the solution to place the files
112      * @param publishFiles
113      * Array of File objects to post to the server
114      * @param dataSource
115      * The datasource to publish to the server
116      * @param publishPassword
117      * The publishing password for the server
118      * @param serverUserid
119      * The userid to authenticate to the server
120      * @param serverPassword
121      * The password to authenticate with the server
122      * @return Server response as a string
123      */

124     public static int publish(String JavaDoc publishURL, String JavaDoc publishPath, File JavaDoc publishFiles[], DataSourceInfo dataSource, String JavaDoc publishPassword, String JavaDoc serverUserid, String JavaDoc serverPassword, boolean overwrite) {
125         int status = -1;
126         System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); //$NON-NLS-1$ //$NON-NLS-2$
127
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true"); //$NON-NLS-1$ //$NON-NLS-2$
128
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire.header", "warn");//$NON-NLS-1$ //$NON-NLS-2$
129
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "warn");//$NON-NLS-1$ //$NON-NLS-2$
130

131         String JavaDoc fullURL = publishURL + "?publishPath=" + publishPath; //$NON-NLS-1$
132
if (publishPassword == null) {
133             throw new IllegalArgumentException JavaDoc(Messages.getErrorString("PUBLISHERUTIL.ERROR_0001_PUBLISHPASSWORDREQUIRED")); //$NON-NLS-1$
134
}
135
136         fullURL += "&publishKey=" + getPasswordKey(publishPassword); //$NON-NLS-1$
137
fullURL += "&overwrite=" + overwrite; //$NON-NLS-1$
138

139         if (dataSource != null) {
140             fullURL += "&jndiName=" + dataSource.getName(); //$NON-NLS-1$
141
fullURL += "&jdbcDriver=" + dataSource.getDriver(); //$NON-NLS-1$
142
fullURL += "&jdbcUrl=" + dataSource.getUrl(); //$NON-NLS-1$
143
fullURL += "&jdbcUserId=" + dataSource.getUserId(); //$NON-NLS-1$
144
fullURL += "&jdbcPassword=" + dataSource.getPassword(); //$NON-NLS-1$
145
}
146         PostMethod filePost = new PostMethod(fullURL);
147         Part[] parts = new Part[publishFiles.length];
148         for (int i = 0; i < publishFiles.length; i++) {
149             try {
150                 parts[i] = new FilePart(publishFiles[i].getName(), publishFiles[i]);
151             } catch (FileNotFoundException JavaDoc e) {
152                 // TODO Auto-generated catch block
153
e.printStackTrace();
154             }
155         }
156         filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
157         HttpClient client = new HttpClient();
158         try {
159             // If server userid/password was supplied, use basic authentication to
160
// authenticate with the server.
161
if ((serverUserid != null) && (serverUserid.length() > 0) && (serverPassword != null) && (serverPassword.length() > 0)) {
162                 Credentials creds = new UsernamePasswordCredentials(serverUserid, serverPassword);
163                 client.getState().setCredentials(AuthScope.ANY, creds);
164                 client.getParams().setAuthenticationPreemptive(true);
165             }
166             status = client.executeMethod(filePost);
167             
168             if (status == HttpStatus.SC_OK) {
169                 String JavaDoc postResult = filePost.getResponseBodyAsString();
170                 
171                 if (postResult != null) {
172                     try {
173                         return Integer.parseInt(postResult.trim());
174                     } catch (NumberFormatException JavaDoc e) {
175                         e.printStackTrace();
176                         return ISolutionRepository.FILE_ADD_INVALID_USER_CREDENTIALS;
177                     }
178                 }
179             } else if (status == HttpStatus.SC_UNAUTHORIZED) {
180                 return ISolutionRepository.FILE_ADD_INVALID_USER_CREDENTIALS;
181             }
182         } catch (HttpException e) {
183             // TODO Auto-generated catch block
184
e.printStackTrace();
185         } catch (IOException JavaDoc e) {
186             // TODO Auto-generated catch block
187
e.printStackTrace();
188         } catch (Exception JavaDoc e) {
189             e.printStackTrace();
190         }
191         // return Messages.getString("REPOSITORYFILEPUBLISHER.USER_PUBLISHER_FAILED"); //$NON-NLS-1$
192
return ISolutionRepository.FILE_ADD_FAILED;
193     }
194
195     /**
196      * Utility for getting the MD5 hash from the provided key for sending the publishPassword.
197      *
198      * @param passWord
199      * The password to get an MD5 hash of
200      * @return zero-padded MD5 hash of the password
201      */

202     public static final String JavaDoc getPasswordKey(String JavaDoc passWord) {
203         try {
204             MessageDigest JavaDoc md = MessageDigest.getInstance("MD5"); //$NON-NLS-1$
205
md.reset(); // Reset the algorithm
206
md.update(passWord.getBytes()); // Update the algorithm with the e-mail
207
// Update the algorithm with a known "key" for keyed MD5
208
// It basically adds the new key to the end and computes
209
byte[] digest = md.digest("P3ntah0Publ1shPa55w0rd".getBytes()); //$NON-NLS-1$
210
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
211             String JavaDoc s;
212             for (int i = 0; i < digest.length; i++) {
213                 s = Integer.toHexString(0xFF & digest[i]);
214                 buf.append((s.length() == 1) ? "0" : "").append(s); //$NON-NLS-1$ //$NON-NLS-2$
215
}
216             return buf.toString(); // Return MD5 string
217
} catch (Exception JavaDoc ex) {
218             ex.printStackTrace();
219         }
220         return null;
221     }
222
223     /**
224      * Checks the publisher key in the publish_config.xml against the presented key.
225      *
226      * @param key
227      * The key to verify
228      * @return true if the presented key is the same as the one in publish_config.xml
229      */

230     public static final boolean checkPublisherKey(String JavaDoc key) {
231         if (key != null) {
232             Document doc = PentahoSystem.getSystemSettings().getSystemSettingsDocument(PublishConfigFile);
233             if (doc != null) {
234                 Node node = doc.selectSingleNode("//publisher-config/publisher-password"); //$NON-NLS-1$
235
if (node != null) {
236                     String JavaDoc setting = node.getText();
237                     if ((setting != null) && (setting.length() > 0)) {
238                         String JavaDoc pubKey = getPasswordKey(setting);
239                         return pubKey.equals(key);
240                     }
241                 }
242             }
243         }
244         return false;
245     }
246
247 }
248
Popular Tags