651 |
11 Apr 08 |
nicklas |
1 |
/** |
651 |
11 Apr 08 |
nicklas |
$Id$ |
651 |
11 Apr 08 |
nicklas |
3 |
|
651 |
11 Apr 08 |
nicklas |
Copyright (C) Authors contributing to this file. |
651 |
11 Apr 08 |
nicklas |
5 |
|
1407 |
18 Oct 11 |
nicklas |
This is free software; you can redistribute it and/or |
651 |
11 Apr 08 |
nicklas |
modify it under the terms of the GNU General Public License |
1407 |
18 Oct 11 |
nicklas |
as published by the Free Software Foundation; either version 3 |
651 |
11 Apr 08 |
nicklas |
of the License, or (at your option) any later version. |
1407 |
18 Oct 11 |
nicklas |
10 |
|
1407 |
18 Oct 11 |
nicklas |
The software is distributed in the hope that it will be useful, |
651 |
11 Apr 08 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
651 |
11 Apr 08 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
651 |
11 Apr 08 |
nicklas |
GNU General Public License for more details. |
1407 |
18 Oct 11 |
nicklas |
15 |
|
651 |
11 Apr 08 |
nicklas |
You should have received a copy of the GNU General Public License |
1407 |
18 Oct 11 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
651 |
11 Apr 08 |
nicklas |
18 |
*/ |
651 |
11 Apr 08 |
nicklas |
19 |
package net.sf.basedb.ws.example; |
651 |
11 Apr 08 |
nicklas |
20 |
|
651 |
11 Apr 08 |
nicklas |
21 |
import java.io.File; |
651 |
11 Apr 08 |
nicklas |
22 |
import java.io.FileOutputStream; |
651 |
11 Apr 08 |
nicklas |
23 |
import java.io.IOException; |
651 |
11 Apr 08 |
nicklas |
24 |
import java.io.InputStream; |
651 |
11 Apr 08 |
nicklas |
25 |
import java.io.OutputStream; |
651 |
11 Apr 08 |
nicklas |
26 |
|
651 |
11 Apr 08 |
nicklas |
27 |
import org.apache.axis2.AxisFault; |
651 |
11 Apr 08 |
nicklas |
28 |
import org.apache.commons.io.IOUtils; |
651 |
11 Apr 08 |
nicklas |
29 |
|
651 |
11 Apr 08 |
nicklas |
30 |
import net.sf.basedb.info.DataFileTypeInfo; |
651 |
11 Apr 08 |
nicklas |
31 |
import net.sf.basedb.info.ExperimentInfo; |
1407 |
18 Oct 11 |
nicklas |
32 |
import net.sf.basedb.info.FileInfo; |
1407 |
18 Oct 11 |
nicklas |
33 |
import net.sf.basedb.info.FileSetMemberInfo; |
651 |
11 Apr 08 |
nicklas |
34 |
import net.sf.basedb.info.ProjectInfo; |
651 |
11 Apr 08 |
nicklas |
35 |
import net.sf.basedb.info.QueryOptions; |
651 |
11 Apr 08 |
nicklas |
36 |
import net.sf.basedb.info.RawBioAssayInfo; |
651 |
11 Apr 08 |
nicklas |
37 |
import net.sf.basedb.info.VersionInfo; |
651 |
11 Apr 08 |
nicklas |
38 |
import net.sf.basedb.ws.client.ExperimentClient; |
651 |
11 Apr 08 |
nicklas |
39 |
import net.sf.basedb.ws.client.ProjectClient; |
651 |
11 Apr 08 |
nicklas |
40 |
import net.sf.basedb.ws.client.RawBioAssayClient; |
651 |
11 Apr 08 |
nicklas |
41 |
import net.sf.basedb.ws.client.SessionClient; |
651 |
11 Apr 08 |
nicklas |
42 |
|
651 |
11 Apr 08 |
nicklas |
43 |
/** |
651 |
11 Apr 08 |
nicklas |
Example code for using BASE web services. This class need |
651 |
11 Apr 08 |
nicklas |
three arguments to it's main method: |
651 |
11 Apr 08 |
nicklas |
46 |
|
651 |
11 Apr 08 |
nicklas |
<pre> |
651 |
11 Apr 08 |
nicklas |
servicesUrl |
651 |
11 Apr 08 |
nicklas |
login |
651 |
11 Apr 08 |
nicklas |
password |
651 |
11 Apr 08 |
nicklas |
</pre> |
651 |
11 Apr 08 |
nicklas |
52 |
|
651 |
11 Apr 08 |
nicklas |
This class will connect to the BASE server as specified in the |
651 |
11 Apr 08 |
nicklas |
<code>servicesUrl</code>, login, list all projects, experiments |
651 |
11 Apr 08 |
nicklas |
and raw bioassays the user has access to. It may optionally download |
651 |
11 Apr 08 |
nicklas |
data files associated with a raw bioassay. The final step is to logout |
651 |
11 Apr 08 |
nicklas |
again. |
651 |
11 Apr 08 |
nicklas |
58 |
|
651 |
11 Apr 08 |
nicklas |
@author nicklas |
651 |
11 Apr 08 |
nicklas |
@version 2.5 |
651 |
11 Apr 08 |
nicklas |
@base.modified $Date$ |
651 |
11 Apr 08 |
nicklas |
62 |
*/ |
651 |
11 Apr 08 |
nicklas |
63 |
public class Main |
651 |
11 Apr 08 |
nicklas |
64 |
{ |
651 |
11 Apr 08 |
nicklas |
65 |
|
651 |
11 Apr 08 |
nicklas |
66 |
public static void main(String[] args) |
651 |
11 Apr 08 |
nicklas |
67 |
{ |
651 |
11 Apr 08 |
nicklas |
68 |
if (args.length < 3 || args.length > 4) |
651 |
11 Apr 08 |
nicklas |
69 |
{ |
651 |
11 Apr 08 |
nicklas |
70 |
showUsage(); |
651 |
11 Apr 08 |
nicklas |
71 |
System.exit(0); |
651 |
11 Apr 08 |
nicklas |
72 |
} |
651 |
11 Apr 08 |
nicklas |
73 |
String serviceUrl = args[0]; |
651 |
11 Apr 08 |
nicklas |
74 |
String username = args[1]; |
651 |
11 Apr 08 |
nicklas |
75 |
String password = args[2]; |
651 |
11 Apr 08 |
nicklas |
76 |
String directory = args.length >= 4 ? args[3] : null; |
651 |
11 Apr 08 |
nicklas |
77 |
|
651 |
11 Apr 08 |
nicklas |
78 |
|
651 |
11 Apr 08 |
nicklas |
79 |
Main main = new Main(); |
651 |
11 Apr 08 |
nicklas |
80 |
try |
651 |
11 Apr 08 |
nicklas |
81 |
{ |
651 |
11 Apr 08 |
nicklas |
82 |
System.out.println("Connecting to: " + serviceUrl); |
651 |
11 Apr 08 |
nicklas |
83 |
main.connect(serviceUrl); |
651 |
11 Apr 08 |
nicklas |
84 |
System.out.println("------------"); |
651 |
11 Apr 08 |
nicklas |
85 |
main.login(username, password); |
651 |
11 Apr 08 |
nicklas |
86 |
System.out.println("------------"); |
651 |
11 Apr 08 |
nicklas |
87 |
main.listProjects(); |
651 |
11 Apr 08 |
nicklas |
88 |
System.out.println("------------"); |
651 |
11 Apr 08 |
nicklas |
89 |
main.listExperiments(); |
651 |
11 Apr 08 |
nicklas |
90 |
System.out.println("------------"); |
651 |
11 Apr 08 |
nicklas |
91 |
main.listRawBioassays(directory); |
651 |
11 Apr 08 |
nicklas |
92 |
System.out.println("------------"); |
651 |
11 Apr 08 |
nicklas |
93 |
main.logout(); |
651 |
11 Apr 08 |
nicklas |
94 |
} |
651 |
11 Apr 08 |
nicklas |
95 |
catch (Throwable t) |
651 |
11 Apr 08 |
nicklas |
96 |
{ |
651 |
11 Apr 08 |
nicklas |
97 |
t.printStackTrace(); |
651 |
11 Apr 08 |
nicklas |
98 |
System.exit(1); |
651 |
11 Apr 08 |
nicklas |
99 |
} |
651 |
11 Apr 08 |
nicklas |
100 |
} |
651 |
11 Apr 08 |
nicklas |
101 |
|
651 |
11 Apr 08 |
nicklas |
102 |
private static void showUsage() |
651 |
11 Apr 08 |
nicklas |
103 |
{ |
651 |
11 Apr 08 |
nicklas |
104 |
System.out.println("Usage: Main <url> <login> <password> [directory]"); |
651 |
11 Apr 08 |
nicklas |
105 |
System.out.println(" url : The URL to the BASE webservices server"); |
651 |
11 Apr 08 |
nicklas |
106 |
System.out.println(" login : The login for an existing user account on the BASE server"); |
651 |
11 Apr 08 |
nicklas |
107 |
System.out.println(" password : The password for the account"); |
651 |
11 Apr 08 |
nicklas |
108 |
System.out.println(" directory : Optional; if specified all raw bioassay data files will be downloaded to this directory"); |
651 |
11 Apr 08 |
nicklas |
109 |
} |
651 |
11 Apr 08 |
nicklas |
110 |
|
651 |
11 Apr 08 |
nicklas |
111 |
private String serviceUrl; |
651 |
11 Apr 08 |
nicklas |
112 |
private SessionClient session; |
651 |
11 Apr 08 |
nicklas |
113 |
|
651 |
11 Apr 08 |
nicklas |
114 |
public Main() |
651 |
11 Apr 08 |
nicklas |
115 |
{} |
651 |
11 Apr 08 |
nicklas |
116 |
|
651 |
11 Apr 08 |
nicklas |
117 |
/** |
651 |
11 Apr 08 |
nicklas |
Connect to BASE with web services. |
651 |
11 Apr 08 |
nicklas |
@param serviceUrl The URL to the BASE webservices server |
651 |
11 Apr 08 |
nicklas |
120 |
*/ |
651 |
11 Apr 08 |
nicklas |
121 |
public void connect(String serviceUrl) |
651 |
11 Apr 08 |
nicklas |
122 |
throws AxisFault |
651 |
11 Apr 08 |
nicklas |
123 |
{ |
651 |
11 Apr 08 |
nicklas |
124 |
this.serviceUrl = serviceUrl; |
651 |
11 Apr 08 |
nicklas |
125 |
this.session = new SessionClient(serviceUrl, null, null); |
651 |
11 Apr 08 |
nicklas |
126 |
System.out.println("Connected to: " + serviceUrl); |
651 |
11 Apr 08 |
nicklas |
127 |
VersionInfo version = session.getVersion(); |
651 |
11 Apr 08 |
nicklas |
128 |
System.out.println("BASE Version: " + version.toString()); |
651 |
11 Apr 08 |
nicklas |
129 |
} |
651 |
11 Apr 08 |
nicklas |
130 |
|
651 |
11 Apr 08 |
nicklas |
131 |
/** |
651 |
11 Apr 08 |
nicklas |
Login to BASE. |
651 |
11 Apr 08 |
nicklas |
@param login The login for the account |
651 |
11 Apr 08 |
nicklas |
@param password The password |
651 |
11 Apr 08 |
nicklas |
135 |
*/ |
651 |
11 Apr 08 |
nicklas |
136 |
public void login(String login, String password) |
651 |
11 Apr 08 |
nicklas |
137 |
throws AxisFault |
651 |
11 Apr 08 |
nicklas |
138 |
{ |
1417 |
26 Oct 11 |
nicklas |
139 |
session.login(login, password, null); |
651 |
11 Apr 08 |
nicklas |
140 |
int timeout = session.getSessionTimeOut(); |
651 |
11 Apr 08 |
nicklas |
141 |
System.out.println("Logged in as: " + login); |
651 |
11 Apr 08 |
nicklas |
142 |
System.out.println("Timeout is: " + timeout + " minutes"); |
651 |
11 Apr 08 |
nicklas |
143 |
} |
651 |
11 Apr 08 |
nicklas |
144 |
|
651 |
11 Apr 08 |
nicklas |
145 |
/** |
651 |
11 Apr 08 |
nicklas |
Logout from BASE. |
651 |
11 Apr 08 |
nicklas |
147 |
*/ |
651 |
11 Apr 08 |
nicklas |
148 |
public void logout() |
651 |
11 Apr 08 |
nicklas |
149 |
throws AxisFault |
651 |
11 Apr 08 |
nicklas |
150 |
{ |
651 |
11 Apr 08 |
nicklas |
151 |
session.logout(); |
651 |
11 Apr 08 |
nicklas |
152 |
System.out.println("Logged out!"); |
651 |
11 Apr 08 |
nicklas |
153 |
} |
651 |
11 Apr 08 |
nicklas |
154 |
|
651 |
11 Apr 08 |
nicklas |
155 |
/** |
651 |
11 Apr 08 |
nicklas |
List all projects that the logged in user is has access to. |
651 |
11 Apr 08 |
nicklas |
The first project found is made the active project. |
651 |
11 Apr 08 |
nicklas |
158 |
*/ |
651 |
11 Apr 08 |
nicklas |
159 |
public void listProjects() |
651 |
11 Apr 08 |
nicklas |
160 |
throws AxisFault |
651 |
11 Apr 08 |
nicklas |
161 |
{ |
651 |
11 Apr 08 |
nicklas |
162 |
ProjectClient pc = new ProjectClient(session); |
651 |
11 Apr 08 |
nicklas |
163 |
ProjectInfo[] projects = pc.getProjects(new QueryOptions()); |
651 |
11 Apr 08 |
nicklas |
164 |
|
651 |
11 Apr 08 |
nicklas |
165 |
if (projects == null || projects.length == 0) |
651 |
11 Apr 08 |
nicklas |
166 |
{ |
651 |
11 Apr 08 |
nicklas |
167 |
System.out.println("No projects found."); |
651 |
11 Apr 08 |
nicklas |
168 |
} |
651 |
11 Apr 08 |
nicklas |
169 |
else |
651 |
11 Apr 08 |
nicklas |
170 |
{ |
651 |
11 Apr 08 |
nicklas |
171 |
System.out.println(projects.length + " projects found"); |
651 |
11 Apr 08 |
nicklas |
172 |
int i = 0; |
651 |
11 Apr 08 |
nicklas |
173 |
while (i < projects.length) |
651 |
11 Apr 08 |
nicklas |
174 |
{ |
651 |
11 Apr 08 |
nicklas |
175 |
ProjectInfo info = projects[i]; |
651 |
11 Apr 08 |
nicklas |
176 |
++i; |
651 |
11 Apr 08 |
nicklas |
177 |
System.out.println( i + ". " + info.getName() + " [id=" + info.getId() + "]"); |
651 |
11 Apr 08 |
nicklas |
178 |
} |
651 |
11 Apr 08 |
nicklas |
179 |
|
651 |
11 Apr 08 |
nicklas |
180 |
System.out.print("Activating project: " + projects[0].getName() + "..."); |
651 |
11 Apr 08 |
nicklas |
181 |
pc.setActive(projects[0].getId()); |
651 |
11 Apr 08 |
nicklas |
182 |
System.out.println("Done!"); |
651 |
11 Apr 08 |
nicklas |
183 |
} |
651 |
11 Apr 08 |
nicklas |
184 |
} |
651 |
11 Apr 08 |
nicklas |
185 |
|
651 |
11 Apr 08 |
nicklas |
186 |
/** |
651 |
11 Apr 08 |
nicklas |
List all experiments that the logged in user is has access to. |
651 |
11 Apr 08 |
nicklas |
188 |
*/ |
651 |
11 Apr 08 |
nicklas |
189 |
public void listExperiments() |
651 |
11 Apr 08 |
nicklas |
190 |
throws AxisFault |
651 |
11 Apr 08 |
nicklas |
191 |
{ |
651 |
11 Apr 08 |
nicklas |
192 |
ExperimentClient ec = new ExperimentClient(session); |
651 |
11 Apr 08 |
nicklas |
193 |
ExperimentInfo[] experiments = ec.getExperiments(new QueryOptions()); |
651 |
11 Apr 08 |
nicklas |
194 |
|
651 |
11 Apr 08 |
nicklas |
195 |
if (experiments == null || experiments.length == 0) |
651 |
11 Apr 08 |
nicklas |
196 |
{ |
651 |
11 Apr 08 |
nicklas |
197 |
System.out.println("No experiments found."); |
651 |
11 Apr 08 |
nicklas |
198 |
} |
651 |
11 Apr 08 |
nicklas |
199 |
else |
651 |
11 Apr 08 |
nicklas |
200 |
{ |
651 |
11 Apr 08 |
nicklas |
201 |
System.out.println(experiments.length + " experiments found"); |
651 |
11 Apr 08 |
nicklas |
202 |
int i = 0; |
651 |
11 Apr 08 |
nicklas |
203 |
while (i < experiments.length) |
651 |
11 Apr 08 |
nicklas |
204 |
{ |
651 |
11 Apr 08 |
nicklas |
205 |
ExperimentInfo info = experiments[i]; |
651 |
11 Apr 08 |
nicklas |
206 |
++i; |
651 |
11 Apr 08 |
nicklas |
207 |
System.out.println( i + ". " + info.getName() + " [id=" + info.getId() + "]"); |
651 |
11 Apr 08 |
nicklas |
208 |
} |
651 |
11 Apr 08 |
nicklas |
209 |
} |
651 |
11 Apr 08 |
nicklas |
210 |
} |
651 |
11 Apr 08 |
nicklas |
211 |
|
651 |
11 Apr 08 |
nicklas |
212 |
/** |
651 |
11 Apr 08 |
nicklas |
List all raw bioassays that the logged in user is has access to. For |
651 |
11 Apr 08 |
nicklas |
each raw bioassay, list it's datafiles and download those to the specified |
651 |
11 Apr 08 |
nicklas |
directory. If no directory is specified the download is disabled. |
651 |
11 Apr 08 |
nicklas |
216 |
*/ |
651 |
11 Apr 08 |
nicklas |
217 |
public void listRawBioassays(String directory) |
651 |
11 Apr 08 |
nicklas |
218 |
throws AxisFault |
651 |
11 Apr 08 |
nicklas |
219 |
{ |
651 |
11 Apr 08 |
nicklas |
220 |
RawBioAssayClient rc = new RawBioAssayClient(session); |
651 |
11 Apr 08 |
nicklas |
221 |
RawBioAssayInfo[] rawBioAssays = rc.getRawBioAssays(new QueryOptions()); |
651 |
11 Apr 08 |
nicklas |
222 |
|
651 |
11 Apr 08 |
nicklas |
223 |
File downloadDir = directory != null ? new File(directory) : null; |
651 |
11 Apr 08 |
nicklas |
224 |
boolean doDownload = downloadDir != null && downloadDir.isDirectory(); |
651 |
11 Apr 08 |
nicklas |
225 |
|
651 |
11 Apr 08 |
nicklas |
226 |
if (rawBioAssays == null || rawBioAssays.length == 0) |
651 |
11 Apr 08 |
nicklas |
227 |
{ |
651 |
11 Apr 08 |
nicklas |
228 |
System.out.println("No raw bioassays found."); |
651 |
11 Apr 08 |
nicklas |
229 |
} |
651 |
11 Apr 08 |
nicklas |
230 |
else |
651 |
11 Apr 08 |
nicklas |
231 |
{ |
651 |
11 Apr 08 |
nicklas |
232 |
System.out.println(rawBioAssays.length + " raw bioassays found; " + |
651 |
11 Apr 08 |
nicklas |
233 |
(doDownload ? "Downloading to directory '" + directory + "'" : "File download is disabled")); |
651 |
11 Apr 08 |
nicklas |
234 |
int i = 0; |
651 |
11 Apr 08 |
nicklas |
235 |
while (i < rawBioAssays.length) |
651 |
11 Apr 08 |
nicklas |
236 |
{ |
651 |
11 Apr 08 |
nicklas |
237 |
RawBioAssayInfo info = rawBioAssays[i]; |
651 |
11 Apr 08 |
nicklas |
238 |
++i; |
651 |
11 Apr 08 |
nicklas |
239 |
System.out.println( i + ". " + info.getName() + " [id=" + info.getId() + "]"); |
651 |
11 Apr 08 |
nicklas |
240 |
|
651 |
11 Apr 08 |
nicklas |
241 |
listDataFiles(rc, info, doDownload ? downloadDir : null); |
651 |
11 Apr 08 |
nicklas |
242 |
} |
651 |
11 Apr 08 |
nicklas |
243 |
} |
651 |
11 Apr 08 |
nicklas |
244 |
} |
651 |
11 Apr 08 |
nicklas |
245 |
|
651 |
11 Apr 08 |
nicklas |
246 |
/** |
651 |
11 Apr 08 |
nicklas |
List all datafiles for a given raw bioassay and download them if a |
651 |
11 Apr 08 |
nicklas |
download directory is given. |
651 |
11 Apr 08 |
nicklas |
249 |
*/ |
651 |
11 Apr 08 |
nicklas |
250 |
public void listDataFiles(RawBioAssayClient rc, RawBioAssayInfo info, File downloadDir) |
651 |
11 Apr 08 |
nicklas |
251 |
throws AxisFault |
651 |
11 Apr 08 |
nicklas |
252 |
{ |
1407 |
18 Oct 11 |
nicklas |
253 |
FileSetMemberInfo[] files = rc.getDataFiles(info.getId(), new QueryOptions()); |
651 |
11 Apr 08 |
nicklas |
254 |
if (files == null || files.length == 0) |
651 |
11 Apr 08 |
nicklas |
255 |
{ |
651 |
11 Apr 08 |
nicklas |
256 |
System.out.println(" No data files found."); |
651 |
11 Apr 08 |
nicklas |
257 |
} |
651 |
11 Apr 08 |
nicklas |
258 |
else |
651 |
11 Apr 08 |
nicklas |
259 |
{ |
1407 |
18 Oct 11 |
nicklas |
260 |
for (FileSetMemberInfo member : files) |
651 |
11 Apr 08 |
nicklas |
261 |
{ |
1407 |
18 Oct 11 |
nicklas |
262 |
DataFileTypeInfo fileType = member.getDataFileTypeInfo(); |
1407 |
18 Oct 11 |
nicklas |
263 |
FileInfo file = member.getFileInfo(); |
1407 |
18 Oct 11 |
nicklas |
264 |
|
1407 |
18 Oct 11 |
nicklas |
265 |
System.out.println(" " + fileType.getName() + ": " + |
1407 |
18 Oct 11 |
nicklas |
266 |
file.getName() + " [id=" + file.getId() + "] " + file.getSize() + " bytes"); |
651 |
11 Apr 08 |
nicklas |
267 |
if (downloadDir != null) |
651 |
11 Apr 08 |
nicklas |
268 |
{ |
1407 |
18 Oct 11 |
nicklas |
269 |
File downloadTo = new File(downloadDir, info.getName() + "." + file.getName()); |
651 |
11 Apr 08 |
nicklas |
270 |
try |
651 |
11 Apr 08 |
nicklas |
271 |
{ |
651 |
11 Apr 08 |
nicklas |
272 |
InputStream in = rc.downloadRawDataByType(info.getId(), fileType.getExternalId()); |
651 |
11 Apr 08 |
nicklas |
273 |
OutputStream out = new FileOutputStream(downloadTo); |
651 |
11 Apr 08 |
nicklas |
274 |
IOUtils.copy(in, out); |
651 |
11 Apr 08 |
nicklas |
275 |
out.close(); |
651 |
11 Apr 08 |
nicklas |
276 |
in.close(); |
1407 |
18 Oct 11 |
nicklas |
277 |
System.out.print(" Download --> " + downloadTo); |
651 |
11 Apr 08 |
nicklas |
278 |
} |
651 |
11 Apr 08 |
nicklas |
279 |
catch (IOException ex) |
651 |
11 Apr 08 |
nicklas |
280 |
{ |
1407 |
18 Oct 11 |
nicklas |
281 |
System.out.print(" Download failed: " + ex.getMessage()); |
651 |
11 Apr 08 |
nicklas |
282 |
} |
651 |
11 Apr 08 |
nicklas |
283 |
} |
651 |
11 Apr 08 |
nicklas |
284 |
System.out.println(""); |
651 |
11 Apr 08 |
nicklas |
285 |
} |
651 |
11 Apr 08 |
nicklas |
286 |
} |
651 |
11 Apr 08 |
nicklas |
287 |
} |
651 |
11 Apr 08 |
nicklas |
288 |
|
651 |
11 Apr 08 |
nicklas |
289 |
} |