PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/dspace-sword/dspace-sword-api/src/main/java/org/purl/sword/client/ClientOptions.java

http://dspace-semantic-search.googlecode.com/
Java | 627 lines | 304 code | 90 blank | 233 comment | 40 complexity | 1edf08109f314dc5ae1c4a7fd1a2d1f8 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, Unlicense
  1. /**
  2. * The contents of this file are subject to the license and copyright
  3. * detailed in the LICENSE and NOTICE files at the root of the source
  4. * tree and available online at
  5. *
  6. * http://www.dspace.org/license/
  7. */
  8. package org.purl.sword.client;
  9. import java.util.ArrayList;
  10. import java.util.Iterator;
  11. import java.util.List;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14. import org.apache.log4j.Logger;
  15. /**
  16. * List of options that are parsed from the command line.
  17. * @author Neil Taylor
  18. */
  19. public class ClientOptions
  20. {
  21. /**
  22. * Label for the service operation.
  23. */
  24. public static final String TYPE_SERVICE = "service";
  25. /**
  26. * Label for the post operation.
  27. */
  28. public static final String TYPE_POST = "post";
  29. /**
  30. * Label for the multipost operation.
  31. */
  32. public static final String TYPE_MULTI_POST = "multipost";
  33. /**
  34. * The access type.
  35. */
  36. private String accessType = null;
  37. /**
  38. * Proxy host name.
  39. */
  40. private String proxyHost = null;
  41. /**
  42. * Proxy host port.
  43. */
  44. private int proxyPort = 8080;
  45. /**
  46. * Username to access the service/post server.
  47. */
  48. private String username = null;
  49. /**
  50. * Password to access the service/post server.
  51. */
  52. private String password = null;
  53. /**
  54. * HREF of the server to access.
  55. */
  56. private String href = null;
  57. /**
  58. * Filename to post.
  59. */
  60. private String filename = null;
  61. /**
  62. * Filetype.
  63. */
  64. private String filetype = null;
  65. /**
  66. * Specifies that the output streams are not to be captured by the GUI client.
  67. */
  68. private boolean noCapture = false;
  69. /**
  70. * SLUG Header field.
  71. */
  72. private String slug = null;
  73. /**
  74. * NoOp, used to indicate an operation on the server that does not
  75. * require the file to be stored.
  76. */
  77. private boolean noOp = false;
  78. /**
  79. * Request verbose output from the server.
  80. */
  81. private boolean verbose = false;
  82. /**
  83. * OnBehalfOf user id.
  84. */
  85. private String onBehalfOf = null;
  86. /**
  87. * Format namespace to be used for the posted file.
  88. */
  89. private String formatNamespace = null;
  90. /**
  91. * Introduce a checksum error. This is used to simualte an error with the
  92. * MD5 value.
  93. */
  94. private boolean checksumError = false;
  95. /**
  96. * Logger.
  97. */
  98. private static Logger log = Logger.getLogger(ClientOptions.class);
  99. /**
  100. * List of multiple destination items. Used if the mode is set to multipost.
  101. */
  102. private List<PostDestination> multiPost = new ArrayList<PostDestination>();
  103. /**
  104. * Pattern string to extract the data from a destination parameter in multipost mode.
  105. */
  106. private static final Pattern multiPattern = Pattern.compile("(.*?)(\\[(.*?)\\]){0,1}(:(.*)){0,1}@(http://.*)");
  107. /**
  108. * Flag that indicates if the gui mode has been set. This is
  109. * true by default.
  110. */
  111. private boolean guiMode = true;
  112. /**
  113. * Flat that indicates if the MD5 option has been selected. This
  114. * is true by default.
  115. */
  116. private boolean md5 = false;
  117. /**
  118. * Parse the list of options contained in the specified array.
  119. *
  120. * @param args The array of options.
  121. *
  122. * @return True if the options were parsed successfully.
  123. */
  124. public boolean parseOptions( String[] args )
  125. {
  126. try
  127. {
  128. // iterate over the args
  129. for( int i = 0; i < args.length; i++ )
  130. {
  131. if( "-md5".equals(args[i]))
  132. {
  133. md5 = true;
  134. }
  135. if( "-noOp".equals(args[i]))
  136. {
  137. noOp = true;
  138. }
  139. if( "-verbose".equals(args[i]))
  140. {
  141. verbose = true;
  142. }
  143. if( "-cmd".equals(args[i]) )
  144. {
  145. guiMode = false;
  146. }
  147. if( "-gui".equals(args[i]) )
  148. {
  149. guiMode = true;
  150. }
  151. if( "-host".equals(args[i]) )
  152. {
  153. i++;
  154. proxyHost = args[i];
  155. }
  156. if( "-port".equals(args[i]) )
  157. {
  158. i++;
  159. proxyPort = Integer.parseInt(args[i]);
  160. }
  161. if( "-u".equals(args[i]) )
  162. {
  163. i++;
  164. username = args[i];
  165. }
  166. if( "-p".equals(args[i]))
  167. {
  168. i++;
  169. password = args[i];
  170. }
  171. if( "-href".equals(args[i]))
  172. {
  173. i++;
  174. href = args[i];
  175. }
  176. if( "-help".equals(args[i]) )
  177. {
  178. // force the calling code to display the usage information
  179. return false;
  180. }
  181. if( "-t".equals(args[i]))
  182. {
  183. i++;
  184. accessType = args[i];
  185. }
  186. if( "-file".equals(args[i]))
  187. {
  188. i++;
  189. filename = args[i];
  190. }
  191. if( "-filetype".equals(args[i]))
  192. {
  193. i++;
  194. filetype = args[i];
  195. }
  196. if( "-slug".equals(args[i]))
  197. {
  198. i++;
  199. slug = args[i];
  200. }
  201. if( "-onBehalfOf".equals(args[i]))
  202. {
  203. i++;
  204. onBehalfOf = args[i];
  205. }
  206. if( "-formatNamespace".equals(args[i]))
  207. {
  208. i++;
  209. formatNamespace = args[i];
  210. }
  211. if( "-checksumError".equals(args[i]))
  212. {
  213. i++;
  214. checksumError = true;
  215. }
  216. if( "-dest".equals(args[i]))
  217. {
  218. i++;
  219. Matcher m = multiPattern.matcher(args[i]);
  220. if( ! m.matches() )
  221. {
  222. log.debug("Error with dest parameter. Ignoring value: " + args[i]);
  223. }
  224. else
  225. {
  226. int numGroups = m.groupCount();
  227. for( int g = 0; g <= numGroups; g++ )
  228. {
  229. log.debug("Group (" + g + ") is: " + m.group(g));
  230. }
  231. String username = m.group(1);
  232. String onBehalfOf = m.group(3);
  233. String password = m.group(5);
  234. String url = m.group(6);
  235. PostDestination destination = new PostDestination(url, username, password, onBehalfOf);
  236. multiPost.add(destination);
  237. }
  238. }
  239. if( "-nocapture".equals(args[i]) )
  240. {
  241. i++;
  242. noCapture = true;
  243. }
  244. }
  245. // apply any settings
  246. if( href == null && "service".equals(accessType) )
  247. {
  248. log.error( "No href specified.");
  249. return false;
  250. }
  251. if( multiPost.size() == 0 && "multipost".equals(accessType))
  252. {
  253. log.error("No destinations specified");
  254. return false;
  255. }
  256. if( accessType == null && ! guiMode )
  257. {
  258. log.error("No access type specified");
  259. return false;
  260. }
  261. if( ( username == null && password != null ) || (username != null && password == null))
  262. {
  263. log.error("The username and/or password are not specified. If one is specified, the other must also be specified.");
  264. return false;
  265. }
  266. }
  267. catch( ArrayIndexOutOfBoundsException ex )
  268. {
  269. log.error("Error with parameters.");
  270. return false;
  271. }
  272. return true;
  273. }
  274. /**
  275. * Get the access type.
  276. * @return The value, or <code>null</code> if the value is not set.
  277. */
  278. public String getAccessType()
  279. {
  280. return accessType;
  281. }
  282. /**
  283. * Set the access type.
  284. * @param accessType The value, or <code>null</code> to clear the value.
  285. */
  286. public void setAccessType(String accessType)
  287. {
  288. this.accessType = accessType;
  289. }
  290. /**
  291. * Get the proxy host.
  292. * @return The value, or <code>null</code> if the value is not set.
  293. */
  294. public String getProxyHost()
  295. {
  296. return proxyHost;
  297. }
  298. /**
  299. * Set the proxy host.
  300. * @param proxyHost The value, or <code>null</code> to clear the value.
  301. */
  302. public void setProxyHost(String proxyHost)
  303. {
  304. this.proxyHost = proxyHost;
  305. }
  306. /**
  307. * Get the proxy port.
  308. * @return The proxy port. Default value is 80.
  309. */
  310. public int getProxyPort()
  311. {
  312. return proxyPort;
  313. }
  314. /**
  315. * Set the proxy port.
  316. * @param proxyPort The proxy port.
  317. */
  318. public void setProxyPort(int proxyPort)
  319. {
  320. this.proxyPort = proxyPort;
  321. }
  322. /**
  323. * Get the username.
  324. * @return The value, or <code>null</code> if the value is not set.
  325. */
  326. public String getUsername()
  327. {
  328. return username;
  329. }
  330. /**
  331. * Set the username.
  332. * @param username The value, or <code>null</code> to clear the value.
  333. */
  334. public void setUsername(String username)
  335. {
  336. this.username = username;
  337. }
  338. /**
  339. * Get the password.
  340. * @return The value, or <code>null</code> if the value is not set.
  341. */
  342. public String getPassword()
  343. {
  344. return password;
  345. }
  346. /**
  347. * Set the password.
  348. * @param password The value, or <code>null</code> to clear the value.
  349. */
  350. public void setPassword(String password)
  351. {
  352. this.password = password;
  353. }
  354. /**
  355. * Get the HREF of the service to access.
  356. * @return The value, or <code>null</code> if the value is not set.
  357. */
  358. public String getHref()
  359. {
  360. return href;
  361. }
  362. /**
  363. * Set the HREF of the service to access.
  364. * @param href The value, or <code>null</code> to clear the value.
  365. */
  366. public void setHref(String href)
  367. {
  368. this.href = href;
  369. }
  370. /**
  371. * Get the name of the file to post.
  372. * @return The value, or <code>null</code> if the value is not set.
  373. */
  374. public String getFilename()
  375. {
  376. return filename;
  377. }
  378. /**
  379. * Set the name of the file to post.
  380. * @param filename The value, or <code>null</code> to clear the value.
  381. */
  382. public void setFilename(String filename)
  383. {
  384. this.filename = filename;
  385. }
  386. /**
  387. * Get the type of the file to post.
  388. * @return The filetype, or <code>null</code> if the value is not set.
  389. */
  390. public String getFiletype()
  391. {
  392. return filetype;
  393. }
  394. /**
  395. * Set the type of the file to post.
  396. * @param filetype The value, or <code>null</code> to clear the value.
  397. */
  398. public void setFiletype(String filetype)
  399. {
  400. this.filetype = filetype;
  401. }
  402. /**
  403. * Determine if the tool is to be run in GUI mode.
  404. * @return True if the tool is set for GUI mode.
  405. */
  406. public boolean isGuiMode()
  407. {
  408. return guiMode;
  409. }
  410. /**
  411. * Set the tool to run in GUI mode.
  412. * @param guiMode True if the tool is to run in gui mode.
  413. */
  414. public void setGuiMode(boolean guiMode)
  415. {
  416. this.guiMode = guiMode;
  417. }
  418. /**
  419. * Get the MD5 setting. True if the tool is to use MD5 for post operations.
  420. * @return The MD5 setting.
  421. */
  422. public boolean isMd5() {
  423. return md5;
  424. }
  425. /**
  426. * Set the MD5 setting.
  427. * @param md5 True if the tool should use MD5 for post operations.
  428. */
  429. public void setMd5(boolean md5) {
  430. this.md5 = md5;
  431. }
  432. /**
  433. * Determine if the NoOp header should be sent.
  434. * @return True if the header should be sent.
  435. */
  436. public boolean isNoOp() {
  437. return noOp;
  438. }
  439. /**
  440. * Set the NoOp setting.
  441. * @param noOp True if the NoOp header should be used.
  442. */
  443. public void setNoOp(boolean noOp) {
  444. this.noOp = noOp;
  445. }
  446. /**
  447. * Determine if the verbose option is set.
  448. * @return True if verbose option is set.
  449. */
  450. public boolean isVerbose() {
  451. return verbose;
  452. }
  453. /**
  454. * Set the verbose option.
  455. * @param verbose True if verbose should be set.
  456. */
  457. public void setVerbose(boolean verbose) {
  458. this.verbose = verbose;
  459. }
  460. /**
  461. * Get the onBehalfOf value.
  462. * @return The value, or <code>null</code> to clear the value.
  463. */
  464. public String getOnBehalfOf() {
  465. return onBehalfOf;
  466. }
  467. /**
  468. * Set the onBehalf of Value.
  469. * @param onBehalfOf The value, or <code>null</code> to clear the value.
  470. */
  471. public void setOnBehalfOf(String onBehalfOf) {
  472. this.onBehalfOf = onBehalfOf;
  473. }
  474. /**
  475. * Get the format namespace value.
  476. * @return The value, or <code>null</code> if the value is not set.
  477. */
  478. public String getFormatNamespace() {
  479. return formatNamespace;
  480. }
  481. /**
  482. * Set the format namespace value.
  483. * @param formatNamespace The value, or <code>null</code> to clear the value.
  484. */
  485. public void setFormatNamespace(String formatNamespace) {
  486. this.formatNamespace = formatNamespace;
  487. }
  488. /**
  489. * Get the checksum error value.
  490. * @return True if an error should be introduced into the checksum.
  491. */
  492. public boolean getChecksumError() {
  493. return checksumError;
  494. }
  495. /**
  496. * Set the checksum error value.
  497. * @param checksumError True if the error should be introduced.
  498. */
  499. public void setChecksumError(boolean checksumError) {
  500. this.checksumError = checksumError;
  501. }
  502. /**
  503. * Get the current slug header.
  504. * @return The slug value, or <code>null</code> if the value is not set.
  505. */
  506. public String getSlug( )
  507. {
  508. return this.slug;
  509. }
  510. /**
  511. * Set the text that is to be used for the slug header.
  512. * @param slug The value, or <code>null</code> to clear the value.
  513. */
  514. public void setSlug(String slug)
  515. {
  516. this.slug = slug;
  517. }
  518. /**
  519. * Get the list of post destinations.
  520. * @return An iterator over the list of PostDestination objects.
  521. */
  522. public Iterator<PostDestination> getMultiPost()
  523. {
  524. return multiPost.iterator();
  525. }
  526. /**
  527. * Determine if the noCapture option is set. This indicates that the code
  528. * should not attempt to redirect stdout and stderr to a different output
  529. * destination. Intended for use in a GUI client.
  530. *
  531. * @return The noCapture setting. True if set.
  532. */
  533. public boolean isNoCapture()
  534. {
  535. return noCapture;
  536. }
  537. }