This project has retired. For details please refer to its
Attic page.
ObjGenApp xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.client.main;
20
21 import java.io.BufferedReader;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.net.MalformedURLException;
27 import java.net.URL;
28 import java.util.HashMap;
29 import java.util.Map;
30
31 import joptsimple.OptionParser;
32 import joptsimple.OptionSet;
33 import joptsimple.OptionSpec;
34
35 import org.apache.chemistry.opencmis.client.bindings.CmisBindingFactory;
36 import org.apache.chemistry.opencmis.client.filecopy.FileCopier;
37 import org.apache.chemistry.opencmis.commons.SessionParameter;
38 import org.apache.chemistry.opencmis.commons.data.ContentStream;
39 import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;
40 import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
41 import org.apache.chemistry.opencmis.commons.enums.BindingType;
42 import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
43 import org.apache.chemistry.opencmis.commons.spi.CmisBinding;
44 import org.apache.chemistry.opencmis.commons.spi.RepositoryService;
45 import org.apache.chemistry.opencmis.util.repository.MultiThreadedObjectGenerator;
46 import org.apache.chemistry.opencmis.util.repository.ObjectGenerator;
47 import org.apache.chemistry.opencmis.util.repository.ObjectGenerator.CONTENT_KIND;
48 import org.apache.chemistry.opencmis.util.repository.TimeLogger;
49
50 public class ObjGenApp {
51
52 private static final String PROP_USER = SessionParameter.USER;
53 private static final String PROP_PASSWORD = SessionParameter.PASSWORD;
54 private static final String DEFAULT_USER = "user";
55 private static final String DEFAULT_PASSWORD = "dummy";
56 private static final String PROP_ATOMPUB_URL = SessionParameter.ATOMPUB_URL;
57 private static final String PROP_WS_URL = "org.apache.chemistry.opencmis.binding.webservices.url";
58 private static final String PROP_BROWSER_URL = SessionParameter.BROWSER_URL;
59 private static final String PROP_BINDING = SessionParameter.BINDING_TYPE;
60 private static final String DEFAULT_ATOMPUB_URL = "http://localhost:8080/inmemory/atom";
61 private static final String DEFAULT_WS_URL = "http://localhost:8080/inmemory/services/";
62 private static final String DEFAULT_BROWSER_BINDING_URL = "http://localhost:8080/inmemory/browser/";
63 private static final String DEFAULT_BINDING = "atompub";
64 private static final String CMD = "Command";
65 private static final String REPOSITORY_ID = "RepositoryId";
66 private static final String FILLER_DOCUMENT_TYPE_ID = "DocumentTypeId";
67 private static final String FILLER_FOLDER_TYPE_ID = "FolderTypeId";
68 private static final String FILLER_DOCS_PER_FOLDER = "DocsPerFolder";
69 private static final String FILLER_FOLDERS_PER_FOLDER = "FoldersPerFolder";
70 private static final String FILLER_DEPTH = "Depth";
71 private static final String FILLER_CONTENT_SIZE = "ContentSizeInKB";
72 private static final String COUNT = "Count";
73 private static final String CLEANUP = "Cleanup";
74 private static final String ROOTFOLDER = "RootFolder";
75 private static final String THREADS = "Threads";
76 private static final String CONTENT_KIND = "ContentKind";
77 private static final String FILE_NAME_PATTERN = "FileName";
78 private static final String LOCAL_FILE = "File";
79 private static final String LOCAL_DIR = "Dir";
80
81 private static final String BINDING_ATOM = "atompub";
82 private static final String BINDING_WS = "webservices";
83 private static final String BINDING_BROWSER = "browser";
84
85 BindingType fBindingType;
86 private CONTENT_KIND fContentKind;
87 CmisBinding binding;
88
89 OptionSpec<String> fCmd;
90 OptionSpec<Integer> fDepth;
91 OptionSpec<Integer> fContentSize;
92 OptionSpec<Integer> fFolderPerFolder;
93 OptionSpec<Integer> fDocsPerFolder;
94 OptionSpec<String> fFolderType;
95 OptionSpec<String> fDocType;
96 OptionSpec<String> fRepoId;
97 OptionSpec<Integer> fCount;
98 OptionSpec<Boolean> fCleanup;
99 OptionSpec<String> fRootFolder;
100 OptionSpec<Integer> fThreads;
101 OptionSpec<String> fFileName;
102 OptionSpec<String> fContentKindStr;
103 OptionSpec<String> fFileNamePattern;
104 OptionSpec<String> fLocalDir;
105 OptionSpec<String> fLocalFile;
106
107 public static void main(String[] args) {
108
109 ObjGenApp app = new ObjGenApp();
110 try {
111 app.processCmdLine(args);
112 } catch (CmisBaseException ce) {
113 System.out.println("Error: Could not process command. " + ce);
114 System.out.println("Extended error: " + ce.getErrorContent());
115 ce.printStackTrace();
116 } catch (Exception e) {
117 System.out.println("Could not fill repository " + e);
118 e.printStackTrace();
119 }
120 }
121
122 private void processCmdLine(String[] args) {
123
124 OptionParser parser = new OptionParser();
125 fCmd = parser.accepts(CMD).withRequiredArg().describedAs("Command to perform (see below)");
126 fRepoId = parser.accepts(REPOSITORY_ID).withOptionalArg().describedAs("Repository used");
127 fDocType = parser.accepts(FILLER_DOCUMENT_TYPE_ID).withOptionalArg().defaultsTo(
128 BaseTypeId.CMIS_DOCUMENT.value()).describedAs("Document type created");
129 fFolderType = parser.accepts(FILLER_FOLDER_TYPE_ID).withOptionalArg()
130 .defaultsTo(BaseTypeId.CMIS_FOLDER.value()).describedAs("Folder type created");
131 fDocsPerFolder = parser.accepts(FILLER_DOCS_PER_FOLDER).withOptionalArg().ofType(Integer.class).describedAs(
132 "Documents on each level").defaultsTo(1);
133 fFolderPerFolder = parser.accepts(FILLER_FOLDERS_PER_FOLDER).withOptionalArg().ofType(Integer.class)
134 .describedAs(" Folders on each level").defaultsTo(0);
135 fDepth = parser.accepts(FILLER_DEPTH).withOptionalArg().ofType(Integer.class).describedAs("Levels of folders")
136 .defaultsTo(1);
137 fContentSize = parser.accepts(FILLER_CONTENT_SIZE).withOptionalArg().ofType(Integer.class).describedAs(
138 "Content size of each doc").defaultsTo(0);
139 fCount = parser.accepts(COUNT).withOptionalArg().ofType(Integer.class).defaultsTo(1).describedAs(
140 "Repeat a command n times (partially implemented)");
141 fCleanup = parser.accepts(CLEANUP).withOptionalArg().ofType(Boolean.class).defaultsTo(false).describedAs(
142 "Clean all created objects at the end");
143 fRootFolder = parser.accepts(ROOTFOLDER).withOptionalArg().ofType(String.class).describedAs(
144 "folder id used as root to create objects (default repository root folder)");
145 fThreads = parser.accepts(THREADS).withOptionalArg().ofType(Integer.class).defaultsTo(1).describedAs(
146 "Number of threads to start in parallel");
147
148 fContentKindStr = parser.accepts(CONTENT_KIND).withOptionalArg().ofType(String.class).defaultsTo("lorem/text")
149 .describedAs("kind of content: static/text, lorem/text, lorem/html, fractal/jpeg");
150 fFileNamePattern = parser.accepts(FILE_NAME_PATTERN).withOptionalArg().ofType(String.class).defaultsTo("ContentData-%03d.bin")
151 .describedAs("file name pattern to be used with CreateFiles action");
152 fLocalDir = parser.accepts(LOCAL_DIR).withOptionalArg().ofType(String.class).defaultsTo(".")
153 .describedAs("name of a directory to be recursively copied to the repository");
154 fLocalFile = parser.accepts(LOCAL_FILE).withOptionalArg().ofType(String.class)
155 .describedAs("file name of a file to be copied to the repository");
156 OptionSet options = parser.parse(args);
157
158 if (options.valueOf(fCmd) == null || options.has("?")) {
159 usage(parser);
160 }
161
162 String binding = getBinding();
163
164 if (binding.equals(BINDING_WS)) {
165 fBindingType = BindingType.WEBSERVICES;
166 } else if (binding.equals(BINDING_ATOM)) {
167 fBindingType = BindingType.ATOMPUB;
168 } else if (binding.equals(BINDING_BROWSER)) {
169 fBindingType = BindingType.BROWSER;
170 } else {
171 System.out.println("Error: Unknown binding: " + binding + " allowed values: "
172 + BINDING_WS + " or " + BINDING_ATOM + " or " + BINDING_BROWSER);
173 return;
174 }
175
176 String kind = options.valueOf(fContentKindStr);
177 if (null == kind) {
178 if (options.valueOf(fContentSize) > 0)
179 fContentKind = ObjectGenerator.CONTENT_KIND.StaticText;
180 else
181 fContentKind = null;
182 } if (kind.equals("static/text"))
183 fContentKind = ObjectGenerator.CONTENT_KIND.StaticText;
184 else if (kind.equals("lorem/text"))
185 fContentKind = ObjectGenerator.CONTENT_KIND.LoremIpsumText;
186 else if (kind.equals("lorem/html"))
187 fContentKind = ObjectGenerator.CONTENT_KIND.LoremIpsumHtml;
188 else if (kind.equals("fractal/jpeg"))
189 fContentKind = ObjectGenerator.CONTENT_KIND.ImageFractalJpeg;
190 else {
191 System.out.println("Unknown content kind: " + options.valueOf(fContentKindStr));
192 System.out.println(" must be one of static/text, lorem/text, lorem/html, fractal/jpeg");
193 usage(parser);
194 }
195
196 if (null == options.valueOf(fCmd)) {
197 System.out.println("No command given.");
198 usage(parser);
199 } else if (options.valueOf(fCmd).equals("FillRepository")) {
200 fillRepository(options);
201 } else if (options.valueOf(fCmd).equals("CreateDocument")) {
202 createSingleDocument(options);
203 } else if (options.valueOf(fCmd).equals("CreateFolder")) {
204 createFolders(options);
205 } else if (options.valueOf(fCmd).equals("RepositoryInfo")) {
206 repositoryInfo(options);
207
208
209 } else if (options.valueOf(fCmd).equals("CreateFiles")) {
210 createFiles(options);
211 } else if (options.valueOf(fCmd).equals("CopyFiles")) {
212 transferFiles(options);
213 } else if (options.valueOf(fCmd).equals("CopyFilesTest")) {
214 transferFilesTest(options);
215 } else {
216 System.out.println("Unknown cmd: " + options.valueOf(fCmd));
217 usage(parser);
218 }
219 }
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236 private static void usage(OptionParser parser) {
237 try {
238 System.out.println();
239 System.out.println("ObjGenApp is a command line tool for testing a CMIS repository.");
240 System.out.println("Usage:");
241 parser.printHelpOn(System.out);
242 System.out.println();
243 System.out.println("Command is one of [CreateDocument, CreateFolder, FillRepository, RepositoryInfo, CreateFiles]");
244 System.out.println("JVM system properties: " + PROP_ATOMPUB_URL + ", " + PROP_WS_URL + ", " + PROP_BROWSER_URL);
245 System.out.println(" " + PROP_USER + ", " + PROP_PASSWORD);
246 System.out.println();
247 System.out.println("Example: ");
248 System.out.println("java -D"
249 + PROP_ATOMPUB_URL
250 + "=http://localhost:8080/opencmis/atom -cp ... "
251 + "org.apache.chemistry.opencmis.util.repository.ObjGenApp --Binding=AtomPub --Command=CreateDocument "
252 + "--RepositoryId=A1 --ContentSizeInKB=25 --ContentKind=lorem/text");
253 return;
254 } catch (IOException e) {
255 e.printStackTrace();
256 }
257 }
258
259 private void fillRepository(String repoId, int docsPerFolder, int foldersPerFolders, int depth,
260 String documentType, String folderType, int contentSizeInKB, String rootFolderId, boolean doCleanup) {
261
262 MultiThreadedObjectGenerator.ObjectGeneratorRunner runner = MultiThreadedObjectGenerator.prepareForCreateTree(
263 getClientBindings(), repoId, docsPerFolder, foldersPerFolders, depth, documentType, folderType,
264 contentSizeInKB, rootFolderId, fContentKind, doCleanup);
265 ObjectGenerator gen = runner.getObjectGenerator();
266 runner.doCreateTree();
267
268 System.out.println();
269 System.out.println("Result:");
270 System.out.println("Filling repository succeeded.");
271 System.out.println("Folder used as root for creation (null=rootFolderId): " + rootFolderId);
272 System.out.println("Number of documents created: " + gen.getDocumentsInTotal());
273 System.out.println("Number of folders created: " + gen.getFoldersInTotal());
274 gen.printTimings();
275 }
276
277 private void fillRepositoryMT(int noThreads, String repoId, int docsPerFolder, int foldersPerFolders, int depth,
278 String documentType, String folderType, int contentSizeInKB, String rootFolderId, boolean doCleanup) {
279
280
281 MultiThreadedObjectGenerator.ObjectGeneratorRunner runner = MultiThreadedObjectGenerator
282 .prepareForCreateFolder(getClientBindings(), repoId, folderType, rootFolderId, noThreads, doCleanup);
283 String[] folderIds = runner.doCreateFolder();
284
285
286 MultiThreadedObjectGenerator.ObjectGeneratorRunner[] runners = MultiThreadedObjectGenerator
287 .prepareForCreateTreeMT(getClientBindings(), repoId, docsPerFolder, foldersPerFolders, depth, documentType,
288 folderType, contentSizeInKB, folderIds, fContentKind, doCleanup);
289
290 MultiThreadedObjectGenerator.runMultiThreaded(runners);
291 System.out.println("Filling repository succeeded.");
292 }
293
294 private void printParameters(OptionSet options) {
295 if (fBindingType == BindingType.ATOMPUB) {
296 System.out.println("Using AtomPub, connecting to " + getAtomPubUrl());
297 } else if (fBindingType == BindingType.WEBSERVICES) {
298 System.out.println("Using WebService, connecting to " + getWsUrl());
299 } else if (fBindingType == BindingType.BROWSER) {
300 System.out.println("Using Browser binding, connecting to " + getBrowserUrl());
301 } else
302 System.out.println("Unknown binding type.");
303
304 System.out.println("Repository id is: " + options.valueOf(fRepoId));
305 System.out.println("Content size: " + options.valueOf(fContentSize));
306 System.out.println("Document Type: " + options.valueOf(fDocType));
307 System.out.println("Folder id used as root: " + options.valueOf(fRootFolder));
308 System.out.println("Delete all objects after creation: " + options.valueOf(fCleanup));
309 System.out.println("Number of actions to perform: " + options.valueOf(fCount));
310 System.out.println("Number of threads to start: " + options.valueOf(fThreads));
311 System.out.println("Kind of created content: " + options.valueOf(fContentKindStr));
312 }
313
314 private void createSingleDocument(OptionSet options) {
315 System.out.println();
316 System.out.println("Creating document with parameters:");
317 printParameters(options);
318 int noThreads = options.valueOf(fThreads);
319 if (noThreads <= 1) {
320 createSingleDocument(options.valueOf(fRepoId), options.valueOf(fDocType), options.valueOf(fContentSize),
321 options.valueOf(fRootFolder), options.valueOf(fCount), options.valueOf(fCleanup));
322 } else {
323 createSingleDocumentMT(noThreads, options.valueOf(fRepoId), options.valueOf(fDocType), options
324 .valueOf(fContentSize), options.valueOf(fRootFolder), options.valueOf(fCount), options
325 .valueOf(fCleanup));
326 }
327 }
328
329 private void fillRepository(OptionSet options) {
330 System.out.println();
331 printParameters(options);
332 System.out.println("Creating object tree with folowing parameters: ");
333 System.out.println("Documents per folder: " + options.valueOf(fDocsPerFolder));
334 System.out.println("Folder per folder: " + options.valueOf(fFolderPerFolder));
335 System.out.println("Depth: " + options.valueOf(fDepth));
336 System.out.println("Folder Type: " + options.valueOf(fFolderType));
337
338 int noThreads = options.valueOf(fThreads);
339 if (noThreads <= 1) {
340 fillRepository(options.valueOf(fRepoId), options.valueOf(fDocsPerFolder),
341 options.valueOf(fFolderPerFolder), options.valueOf(fDepth), options.valueOf(fDocType), options
342 .valueOf(fFolderType), options.valueOf(fContentSize), options.valueOf(fRootFolder), options
343 .valueOf(fCleanup));
344 } else {
345 fillRepositoryMT(noThreads, options.valueOf(fRepoId), options.valueOf(fDocsPerFolder), options
346 .valueOf(fFolderPerFolder), options.valueOf(fDepth), options.valueOf(fDocType), options
347 .valueOf(fFolderType), options.valueOf(fContentSize), options.valueOf(fRootFolder), options
348 .valueOf(fCleanup));
349 }
350
351 }
352
353 private void createFolders(OptionSet options) {
354 System.out.println();
355 System.out.println("Creating folder with parameters:");
356 printParameters(options);
357 System.out.println("Folder Type: " + options.valueOf(fFolderType));
358 int noThreads = options.valueOf(fThreads);
359 if (noThreads <= 1) {
360 createFolders(options.valueOf(fRepoId), options.valueOf(fFolderType), options.valueOf(fRootFolder), options
361 .valueOf(fCount), options.valueOf(fCleanup));
362 } else {
363 createFoldersMT(noThreads, options.valueOf(fRepoId), options.valueOf(fFolderType), options
364 .valueOf(fRootFolder), options.valueOf(fCount), options.valueOf(fCleanup));
365 }
366 }
367
368 private void createSingleDocument(String repoId, String documentType, int contentSizeInKB, String rootFolderId,
369 int docCount, boolean doCleanup) {
370
371 MultiThreadedObjectGenerator.ObjectGeneratorRunner runner = MultiThreadedObjectGenerator
372 .prepareForCreateDocument(getClientBindings(), repoId, documentType, contentSizeInKB, rootFolderId, docCount,
373 fContentKind, doCleanup);
374 ObjectGenerator gen = runner.getObjectGenerator();
375 String[] ids = runner.doCreateDocument();
376 System.out.println();
377 System.out.println("Result:");
378 System.out.println("Document creation succeeded.");
379 System.out.println("Folder used as root for creation: " + rootFolderId);
380 System.out.println("Ids of created documents: ");
381 if (null == ids) {
382 System.out.println("<none>");
383 } else {
384 for (int i = 0; i < ids.length; i++) {
385 System.out.println(ids[i]);
386 }
387 }
388 gen.printTimings();
389 gen.resetCounters();
390 }
391
392 private void createSingleDocumentMT(int noThreads, String repoId, String documentType, int contentSizeInKB,
393 String rootFolderId, int docCount, boolean doCleanup) {
394
395 MultiThreadedObjectGenerator.ObjectGeneratorRunner[] runners = MultiThreadedObjectGenerator
396 .prepareForCreateDocumentMT(noThreads, getClientBindings(), repoId, documentType, contentSizeInKB,
397 rootFolderId, docCount, fContentKind, doCleanup);
398
399 MultiThreadedObjectGenerator.runMultiThreaded(runners);
400 System.out.println("Document creation succeeded. All threads terminated.");
401 }
402
403 private void createFolders(String repoId, String folderType, String rootFolderId, int noFolders, boolean doCleanup) {
404
405 MultiThreadedObjectGenerator.ObjectGeneratorRunner runner = MultiThreadedObjectGenerator
406 .prepareForCreateFolder(getClientBindings(), repoId, folderType, rootFolderId, noFolders, doCleanup);
407 ObjectGenerator gen = runner.getObjectGenerator();
408 String[] ids = runner.doCreateFolder();
409 System.out.println();
410 System.out.println("Result:");
411 System.out.println("Folder creation succeeded.");
412 System.out.println("Ids of created folders: ");
413 if (null == ids) {
414 System.out.println("<none>");
415 } else {
416 for (int i = 0; i < ids.length; i++) {
417 System.out.println(ids[i]);
418 }
419 }
420 gen.printTimings();
421 gen.resetCounters();
422 }
423
424 private void createFoldersMT(int noThreads, String repoId, String folderType, String rootFolderId, int noFolders,
425 boolean doCleanup) {
426
427 MultiThreadedObjectGenerator.ObjectGeneratorRunner[] runners = MultiThreadedObjectGenerator
428 .prepareForCreateFolderMT(noThreads, getClientBindings(), repoId, folderType, rootFolderId, noFolders,
429 doCleanup);
430 MultiThreadedObjectGenerator.runMultiThreaded(runners);
431 System.out.println("Folder creation succeeded.");
432 }
433
434 private void callRepoInfo(String repositoryId, int count) {
435 RepositoryService repSvc = getClientBindings().getRepositoryService();
436 TimeLogger timeLogger = new TimeLogger("RepoInfoTest");
437 RepositoryInfo repoInfo = null;
438 for (int i = 0; i < count; i++) {
439 binding.clearRepositoryCache(repositoryId);
440 timeLogger.start();
441 repoInfo = repSvc.getRepositoryInfo(repositoryId, null);
442 timeLogger.stop();
443 }
444 System.out.println("Root Folder id is: " + (repoInfo == null ? "<unknown>" : repoInfo.getRootFolderId()));
445 timeLogger.printTimes();
446 }
447
448 private void createTypes(OptionSet options) {
449
450 String repoId = options.valueOf(fRepoId);
451 String fileName = options.valueOf(fFileName);
452 System.out.println();
453 System.out.println("Not yet implemented waiting for CMIS 1.1!");
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472 }
473
474 private void repositoryInfo(OptionSet options) {
475 callRepoInfo(options.valueOf(fRepoId), options.valueOf(fCount));
476 }
477
478 private void createFiles(OptionSet options) {
479 ContentStream contentStream = null;
480 String fileNamePattern = options.valueOf(fFileNamePattern);
481 int count = options.valueOf(fCount);
482 int contentSize = options.valueOf(fContentSize);
483
484 System.out.println("Creating local files with content: ");
485 System.out.println("Kind: " + options.valueOf(fDocsPerFolder));
486 System.out.println("Number of files: " + count);
487 System.out.println("File name pattern: " + fileNamePattern);
488 System.out.println("Kind of content: " + options.valueOf(fContentKindStr));
489 System.out.println("Size of content (text only): " + contentSize);
490
491 ObjectGenerator objGen = new ObjectGenerator(null, null, null, null, null, fContentKind);
492 objGen.setContentSizeInKB(contentSize);
493
494 InputStream is = null;
495 FileOutputStream os = null;
496
497 try {
498 for (int i=0; i<count; i++) {
499 String fileName = String.format(fileNamePattern, i);
500 System.out.println("Generating file: " + fileName);
501 if (contentSize > 0) {
502 switch (fContentKind) {
503 case StaticText:
504 contentStream = objGen.createContentStaticText();
505 break;
506 case LoremIpsumText:
507 contentStream = objGen.createContentLoremIpsumText();
508 break;
509 case LoremIpsumHtml:
510 contentStream = objGen.createContentLoremIpsumHtml();
511 break;
512 case ImageFractalJpeg:
513 contentStream = objGen.createContentFractalimageJpeg();
514 break;
515 }
516 }
517
518
519 is = contentStream.getStream();
520 os = new FileOutputStream (fileName);
521 byte[] b = new byte[64 * 1024];
522 int read;
523 while ((read = is.read(b)) != -1)
524 os.write(b, 0, read);
525 is.close();
526 is = null;
527 os.close();
528 os = null;
529 }
530 } catch (Exception e) {
531 System.err.println("Error generating file: " + e);
532 e.printStackTrace();
533 } finally {
534 try {
535 if (null != is)
536 is.close();
537 if (null != os)
538 os.close();
539 } catch (IOException e) {
540 }
541 }
542 }
543
544 private void transferFiles(OptionSet options) {
545 String fileName = options.valueOf(fLocalFile);
546 String dirName = options.valueOf(fLocalDir);
547 String repoId = options.valueOf(fRepoId);
548 String folderId = options.valueOf(fRootFolder);
549 String name = fileName;
550
551 if ((null == fileName || fileName.length() == 0) && (null == dirName || dirName.length() == 0)) {
552 System.out.println("Error: You either have to provide a --file or a --dir option to copy file(s).");
553 return;
554 }
555
556
557 if (null == name || name.length() == 0)
558 name = dirName;
559
560 if (null == fRepoId || repoId.length() == 0) {
561 System.out.println("Error: You have to provide a repository id");
562 return;
563 }
564 System.out.println("Copying files to a repository: ");
565 System.out.println("Repository id is: " + repoId);
566 System.out.println("Folder id used as root: " + options.valueOf(fRootFolder));
567
568
569 Map<String, String> parameters = getConnectionParameters(getBinding(), repoId);
570 FileCopier fc = new FileCopier();
571 fc.connect(parameters);
572 fc.copyRecursive(name, folderId);
573 }
574
575 private void transferFilesTest(OptionSet options) {
576 String fileName = options.valueOf(fLocalFile);
577
578 if ((null == fileName || fileName.length() == 0)) {
579 System.out.println("Error: You either have to provide a --file option to test metadata extraction.");
580 return;
581 }
582
583 System.out.println("Testing metadata extraction: ");
584
585 FileCopier fc = new FileCopier();
586 fc.listMetadata(fileName);
587 }
588
589 private Map<String, String> getConnectionParameters(String binding, String repoId) {
590 Map<String, String> parameters = new HashMap<String, String>();
591 parameters.put(SessionParameter.REPOSITORY_ID, repoId);
592 parameters.put(SessionParameter.BINDING_TYPE, binding);
593
594 if (binding.equals(BindingType.ATOMPUB.value())) {
595 parameters.put(SessionParameter.ATOMPUB_URL, getAtomPubUrl());
596 filLoginParams(parameters, getUser(), getPassword());
597 } if (binding.equals(BindingType.WEBSERVICES.value())) {
598 fillWSParameters(parameters, getWsUrl(), isPrefix(getWsUrl()), getUser(), getPassword());
599 } else if (binding.equals(BindingType.BROWSER.value())) {
600 parameters.put(SessionParameter.BROWSER_URL, getBrowserUrl());
601 filLoginParams(parameters, getUser(), getPassword());
602 } else {
603 System.out.println("Error unknown binding: " + binding);
604 }
605
606 return parameters;
607 }
608
609 private CmisBinding getClientBindings() {
610 if (binding == null) {
611 if (fBindingType == BindingType.ATOMPUB) {
612 binding = createAtomBinding(getAtomPubUrl(), getUser(), getPassword());
613 } if (fBindingType == BindingType.WEBSERVICES) {
614 String url = getWsUrl();
615 binding = createWSBinding(url, isPrefix(url), getUser(), getPassword());
616 } else if (fBindingType == BindingType.BROWSER) {
617 binding = createBrowserBinding(getBrowserUrl(), getUser(), getPassword());
618 }
619 }
620 return binding;
621 }
622
623 private static void filLoginParams(Map<String, String> parameters, String user, String password) {
624 if (user != null && user.length() > 0) {
625 parameters.put(SessionParameter.USER, user);
626 }
627 if (user != null && user.length() > 0) {
628 parameters.put(SessionParameter.PASSWORD, password);
629 }
630 }
631
632 private static CmisBinding createAtomBinding(String url, String user, String password) {
633
634
635 Map<String, String> parameters = new HashMap<String, String>();
636 filLoginParams(parameters, user, password);
637
638
639 CmisBindingFactory factory = CmisBindingFactory.newInstance();
640 parameters.put(SessionParameter.ATOMPUB_URL, url);
641 CmisBinding binding = factory.createCmisAtomPubBinding(parameters);
642 return binding;
643 }
644
645 private static CmisBinding createBrowserBinding(String url, String user, String password) {
646
647
648 Map<String, String> parameters = new HashMap<String, String>();
649 filLoginParams(parameters, user, password);
650
651
652 CmisBindingFactory factory = CmisBindingFactory.newInstance();
653 parameters.put(SessionParameter.BROWSER_URL, url);
654 CmisBinding binding = factory.createCmisBrowserBinding(parameters);
655 return binding;
656 }
657
658 private static boolean isPrefix(String url) {
659 boolean isPrefix = true;
660 String urlLower = url.toLowerCase();
661
662 if (urlLower.endsWith("?wsdl")) {
663 isPrefix = false;
664 } else if (urlLower.endsWith(".wsdl")) {
665 isPrefix = false;
666 } else if (urlLower.endsWith(".xml")) {
667 isPrefix = false;
668 }
669 return isPrefix;
670 }
671
672 public static CmisBinding createWSBinding(String url, boolean isPrefix, String username, String password) {
673 Map<String, String> parameters = new HashMap<String, String>();
674 fillWSParameters(parameters, url, isPrefix, username, password);
675
676
677 CmisBindingFactory factory = CmisBindingFactory.newInstance();
678 CmisBinding binding = factory.createCmisWebServicesBinding(parameters);
679
680 return binding;
681 }
682
683 public static void fillWSParameters(Map<String, String> parameters, String url, boolean isPrefix, String username, String password) {
684
685 parameters.put(SessionParameter.USER, username);
686 parameters.put(SessionParameter.PASSWORD, password);
687
688 if (!isPrefix) {
689 parameters.put(SessionParameter.WEBSERVICES_REPOSITORY_SERVICE, url);
690 parameters.put(SessionParameter.WEBSERVICES_NAVIGATION_SERVICE, url);
691 parameters.put(SessionParameter.WEBSERVICES_OBJECT_SERVICE, url);
692 parameters.put(SessionParameter.WEBSERVICES_VERSIONING_SERVICE, url);
693 parameters.put(SessionParameter.WEBSERVICES_DISCOVERY_SERVICE, url);
694 parameters.put(SessionParameter.WEBSERVICES_RELATIONSHIP_SERVICE, url);
695 parameters.put(SessionParameter.WEBSERVICES_MULTIFILING_SERVICE, url);
696 parameters.put(SessionParameter.WEBSERVICES_POLICY_SERVICE, url);
697 parameters.put(SessionParameter.WEBSERVICES_ACL_SERVICE, url);
698 } else {
699 parameters.put(SessionParameter.WEBSERVICES_REPOSITORY_SERVICE, url + "RepositoryService?wsdl");
700 parameters.put(SessionParameter.WEBSERVICES_NAVIGATION_SERVICE, url + "NavigationService?wsdl");
701 parameters.put(SessionParameter.WEBSERVICES_OBJECT_SERVICE, url + "ObjectService?wsdl");
702 parameters.put(SessionParameter.WEBSERVICES_VERSIONING_SERVICE, url + "VersioningService?wsdl");
703 parameters.put(SessionParameter.WEBSERVICES_DISCOVERY_SERVICE, url + "DiscoveryService?wsdl");
704 parameters.put(SessionParameter.WEBSERVICES_RELATIONSHIP_SERVICE, url + "RelationshipService?wsdl");
705 parameters.put(SessionParameter.WEBSERVICES_MULTIFILING_SERVICE, url + "MultiFilingService?wsdl");
706 parameters.put(SessionParameter.WEBSERVICES_POLICY_SERVICE, url + "PolicyService?wsdl");
707 parameters.put(SessionParameter.WEBSERVICES_ACL_SERVICE, url + "ACLService?wsdl");
708 }
709 }
710
711 private static String getBinding() {
712 return System.getProperty(PROP_BINDING, DEFAULT_BINDING);
713 }
714
715 private static String getAtomPubUrl() {
716 return System.getProperty(PROP_ATOMPUB_URL, DEFAULT_ATOMPUB_URL);
717 }
718
719 private static String getWsUrl() {
720 return System.getProperty(PROP_WS_URL, DEFAULT_WS_URL);
721 }
722
723 private static String getBrowserUrl() {
724 return System.getProperty(PROP_BROWSER_URL, DEFAULT_BROWSER_BINDING_URL);
725 }
726
727 private static String getUser() {
728 return System.getProperty(PROP_USER, DEFAULT_USER);
729 }
730
731 private static String getPassword() {
732 return System.getProperty(PROP_PASSWORD, DEFAULT_PASSWORD);
733 }
734
735 private static void getUrl(String urlStr) {
736 URL url;
737 InputStream is;
738 InputStreamReader isr;
739 BufferedReader r;
740 String str;
741
742 try {
743 System.out.println("Reading URL: " + urlStr);
744 url = new URL(urlStr);
745 is = url.openStream();
746 isr = new InputStreamReader(is);
747 r = new BufferedReader(isr);
748 do {
749 str = r.readLine();
750 if (str != null) {
751 System.out.println(str);
752 }
753 } while (str != null);
754 } catch (MalformedURLException e) {
755 System.out.println("Must enter a valid URL" + e);
756 } catch (IOException e) {
757 System.out.println("Can not connect" + e);
758 }
759 }
760
761 }