View Javadoc

1   package net.sf.dynpageplus.maven.sapep;
2   /*
3    * DynPagePlus maven plugin - a plugin to Apache Maven for SAP EP development
4    * Copyright (C) 2005  DynPagePlus project
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.IOException;
24  import java.io.InputStreamReader;
25  import java.io.OutputStream;
26  import java.net.URL;
27  import java.net.URLConnection;
28  
29  /***
30   * This is a helper class for deploying a par file in a given EP installation.
31   * 
32   * @author Thomas Bentzen <tbbe@users.sourceforge.net>
33   */
34  public class Deployer {
35  	private String hostname;
36  
37  	private String port;
38  
39  	private String username;
40  
41  	private String password;
42  
43  	private File parfile;
44  
45  	private boolean verbose = false;
46  
47  	public boolean isVerbose() {
48  		return verbose;
49  	}
50  
51  	public void setVerbose(boolean verbose) {
52  		this.verbose = verbose;
53  	}
54  
55  	public Deployer() {
56  		System.out.println("Deployer initilized");
57  	}
58  
59  	public boolean doUpload() {
60  
61  		try {
62  			if (parfile != null && parfile.exists()) {
63  				return uploadPar();
64  			} else {
65  				System.err
66  						.println("Couldn't upload - .par file does not exist");
67  				return false;
68  			}
69  
70  		} catch (IOException e) {
71  			e.printStackTrace();
72  			return false;
73  		}
74  	}
75  
76  	/***
77  	 * Creates an http-post multipart encoded request for the portal upload par
78  	 * page. This is based upon the way that th ol' PDK did it.
79  	 * 
80  	 * @return True if it was OK.
81  	 * @throws IOException
82  	 *             If an urlconnection is unobtainable.
83  	 */
84  	private boolean uploadPar() throws IOException {
85  
86  		// read the parfile data
87  		int length = (int) parfile.length();
88  		byte buffer1[] = new byte[length];
89  		FileInputStream fis = new FileInputStream(parfile);
90  		fis.read(buffer1);
91  		byte theData[] = buffer1;
92  
93  		// creating the header
94  		StringBuffer buffer = new StringBuffer(
95  				"-----------------------------7d22371e1f0356\r\nContent-Disposition: form-data; name=\"thefile\"; filename=\"");
96  		buffer.append(parfile.getAbsolutePath());
97  		buffer
98  				.append("\"\r\nContent-Type: application/x-zip-compressed\r\n\r\n");
99  		byte header[] = (new String(buffer)).getBytes();
100 
101 		// creating the footer
102 		byte footer[] = "-----------------------------7d22371e1f0356\r\nContent-Disposition: form-data; name=\"updateall\"\r\n\r\non\r\n-----------------------------7d22371e1f0356--"
103 				.getBytes();
104 
105 		// Create the url
106 		URL url = new URL(
107 				("http://"
108 						+ hostname
109 						+ ":"
110 						+ port
111 						+ "/"
112 						+ "irj/servlet/prt/portal/prteventname/upload/prtroot/com.sap.portal.runtime.system.console.ArchiveUploader"
113 						+ "?login_submit=on&j_user=" + username
114 						+ "&j_password=" + password + "&j_authscheme=default&uidPasswordLogon=Log%20on"));
115 		URLConnection con = url.openConnection();
116 		con.setDoOutput(true);
117 		con.setDoInput(true);
118 		con.setUseCaches(false);
119 
120 		// Emulate IE
121 		con.setRequestProperty("User-Agent",
122 				"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; T312461)");
123 
124 		// invoke the call
125 		con.setRequestProperty("Content-Type",
126 						"multipart/form-data; boundary=---------------------------7d22371e1f0356");
127 
128 		OutputStream out = con.getOutputStream();
129 		out.write(header);
130 		out.write(theData);
131 		out.write("\r\n".getBytes());
132 		out.write(footer);
133 		out.flush();
134 		out.close();
135 
136 		// read the result
137 		char buf[] = new char[1024];
138 		StringBuffer result = new StringBuffer(1024);
139 		InputStreamReader reader = new InputStreamReader(con.getInputStream());
140 		int i;
141 		while ((i = reader.read(buf, 0, 1024)) > -1)
142 			result.append(buf, 0, i);
143 		String response = new String(result);
144 		if (isVerbose())
145 			System.out.println(response);
146 		
147 		System.out.println("*********************************************");
148 		System.out.println("Upload completed succesfully");
149 		System.out.println("*********************************************");
150 		return true;
151 	}
152 
153 	public String getHostname() {
154 		return hostname;
155 	}
156 
157 	public void setHostname(String hostname) {
158 		this.hostname = hostname;
159 		log(hostname);
160 	}
161 
162 	public File getParfile() {
163 		return parfile;
164 	}
165 
166 	public void setParfileAsString(String filepath) {
167 		this.parfile = new File(filepath);
168 		log(filepath);
169 	}
170 
171 	public void setParfile(File parfile) {
172 		this.parfile = parfile;
173 	}
174 
175 	public String getPassword() {
176 		return password;
177 	}
178 
179 	public void setPassword(String password) {
180 		this.password = password;
181 		log(password);
182 	}
183 
184 	public String getPort() {
185 		return port;
186 	}
187 
188 	public void setPort(String port) {
189 		this.port = port;
190 		log(port);
191 	}
192 
193 	public String getUsername() {
194 		return username;
195 	}
196 
197 	public void setUsername(String username) {
198 		this.username = username;
199 		log(username);
200 	}
201 
202 	private void log(String msg) {
203 		if (verbose)
204 			System.out.println("Deployer-log: " + msg);
205 	}
206 }