List of usage examples for java.net URI getPath
public String getPath()
From source file:Main.java
public static void main(String... args) throws Exception { String url = "http://www.java2s.com/Tutorials/Java/Algorithms_How_to/index.htm"; URI u = new URI(url); for (String ext : new String[] { ".png", ".pdf", ".jpg", ".html", "htm" }) { if (u.getPath().endsWith(ext)) { System.out.println("Yeap"); break; }//from ww w . java2 s. com } }
From source file:Main.java
public static void main(String[] args) throws NullPointerException, URISyntaxException { URI uri = new URI("http://www.java2s.com:8080/yourpath/fileName.htm"); System.out.println("URI : " + uri); System.out.println(uri.getPath()); }
From source file:edu.usc.goffish.gopher.impl.Main.java
public static void main(String[] args) { Properties properties = new Properties(); try {/*from w w w. j a v a 2 s. co m*/ properties.load(new FileInputStream(CONFIG_FILE)); } catch (IOException e) { String message = "Error while loading Container Configuration from " + CONFIG_FILE + " Cause -" + e.getCause(); log.warning(message); } if (args.length == 4) { PropertiesConfiguration propertiesConfiguration; String url = null; URI uri = URI.create(args[3]); String dataDir = uri.getPath(); String currentHost = uri.getHost(); try { propertiesConfiguration = new PropertiesConfiguration(dataDir + "/gofs.config"); propertiesConfiguration.load(); url = (String) propertiesConfiguration.getString(DataNode.DATANODE_NAMENODE_LOCATION_KEY); } catch (ConfigurationException e) { String message = " Error while reading gofs-config cause -" + e.getCause(); handleException(message); } URI nameNodeUri = URI.create(url); INameNode nameNode = new RemoteNameNode(nameNodeUri); int partition = -1; try { for (URI u : nameNode.getDataNodes()) { if (URIHelper.isLocalURI(u)) { IDataNode dataNode = DataNode.create(u); IntCollection partitions = dataNode.getLocalPartitions(args[2]); partition = partitions.iterator().nextInt(); break; } } if (partition == -1) { String message = "Partition not loaded from uri : " + nameNodeUri; handleException(message); } properties.setProperty(GopherInfraHandler.PARTITION, String.valueOf(partition)); } catch (Exception e) { String message = "Error while loading Partitions from " + nameNodeUri + " Cause -" + e.getMessage(); e.printStackTrace(); handleException(message); } properties.setProperty(Constants.STATIC_PELLET_COUNT, String.valueOf(1)); FloeRuntimeEnvironment environment = FloeRuntimeEnvironment.getEnvironment(); environment.setSystemConfig(properties); properties.setProperty(Constants.CURRET_HOST, currentHost); String managerHost = args[0]; int managerPort = Integer.parseInt(args[1]); Container container = environment.getContainer(); container.setManager(managerHost, managerPort); DefaultClientConfig config = new DefaultClientConfig(); config.getProperties().put(ClientConfig.FEATURE_DISABLE_XML_SECURITY, true); config.getFeatures().put(ClientConfig.FEATURE_DISABLE_XML_SECURITY, true); Client c = Client.create(config); if (managerHost == null || managerPort == 0) { handleException("Manager Host / Port have to be configured in " + args[0]); } WebResource r = c.resource("http://" + managerHost + ":" + managerPort + "/Manager/addContainerInfo/Container=" + container.getContainerInfo().getContainerId() + "/Host=" + container.getContainerInfo().getContainerHost()); c.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true); r.post(); log.log(Level.INFO, "Container started "); } else { String message = "Invalid arguments , arg[0]=Manager host, " + "arg[1] = mamanger port,arg[2]=graph id,arg[3]=partition uri"; message += "\n Current Arguments...." + args.length + "\n"; for (int i = 0; i < args.length; i++) { message += "arg " + i + " : " + args[i] + "\n"; } handleException(message); } }
From source file:com.yahoo.research.scoring.classifier.NutchOnlineClassifier.java
public static void main(String[] args) { try {//w w w . ja va 2 s .com URI urlTmp = new URI("http://www.google.com"); System.out.println(urlTmp.getPath()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:examples.mail.IMAPImportMbox.java
public static void main(String[] args) throws IOException { if (args.length < 2) { System.err.println(//from ww w . ja v a2 s . c om "Usage: IMAPImportMbox imap[s]://user:password@host[:port]/folder/path <mboxfile> [selectors]"); System.err.println("\tWhere: a selector is a list of numbers/number ranges - 1,2,3-10" + " - or a list of strings to match in the initial From line"); System.exit(1); } final URI uri = URI.create(args[0]); final String file = args[1]; final File mbox = new File(file); if (!mbox.isFile() || !mbox.canRead()) { throw new IOException("Cannot read mailbox file: " + mbox); } String path = uri.getPath(); if (path == null || path.length() < 1) { throw new IllegalArgumentException("Invalid folderPath: '" + path + "'"); } String folder = path.substring(1); // skip the leading / List<String> contains = new ArrayList<String>(); // list of strings to find BitSet msgNums = new BitSet(); // list of message numbers for (int i = 2; i < args.length; i++) { String arg = args[i]; if (arg.matches("\\d+(-\\d+)?(,\\d+(-\\d+)?)*")) { // number,m-n for (String entry : arg.split(",")) { String[] parts = entry.split("-"); if (parts.length == 2) { // m-n int low = Integer.parseInt(parts[0]); int high = Integer.parseInt(parts[1]); for (int j = low; j <= high; j++) { msgNums.set(j); } } else { msgNums.set(Integer.parseInt(entry)); } } } else { contains.add(arg); // not a number/number range } } // System.out.println(msgNums.toString()); // System.out.println(java.util.Arrays.toString(contains.toArray())); // Connect and login final IMAPClient imap = IMAPUtils.imapLogin(uri, 10000, null); int total = 0; int loaded = 0; try { imap.setSoTimeout(6000); final BufferedReader br = new BufferedReader(new FileReader(file)); // TODO charset? String line; StringBuilder sb = new StringBuilder(); boolean wanted = false; // Skip any leading rubbish while ((line = br.readLine()) != null) { if (line.startsWith("From ")) { // start of message; i.e. end of previous (if any) if (process(sb, imap, folder, total)) { // process previous message (if any) loaded++; } sb.setLength(0); total++; wanted = wanted(total, line, msgNums, contains); } else if (startsWith(line, PATFROM)) { // Unescape ">+From " in body text line = line.substring(1); } // TODO process first Received: line to determine arrival date? if (wanted) { sb.append(line); sb.append(CRLF); } } br.close(); if (wanted && process(sb, imap, folder, total)) { // last message (if any) loaded++; } } catch (IOException e) { System.out.println(imap.getReplyString()); e.printStackTrace(); System.exit(10); return; } finally { imap.logout(); imap.disconnect(); } System.out.println("Processed " + total + " messages, loaded " + loaded); }
From source file:de.uniko.west.winter.test.basics.JenaTests.java
public static void main(String[] args) { Model newModel = ModelFactory.createDefaultModel(); // /* w w w.j a va 2s . c o m*/ Model newModel2 = ModelFactory.createModelForGraph(ModelFactory.createMemModelMaker().getGraphMaker() .createGraph("http://www.defaultgraph.de/graph1")); StringBuilder updateString = new StringBuilder(); updateString.append("PREFIX dc: <http://purl.org/dc/elements/1.1/>"); updateString.append("PREFIX xsd: <http://bla.org/dc/elements/1.1/>"); updateString.append("INSERT { "); updateString.append( "<http://example/egbook1> dc:title <http://example/egbook1/#Title1>, <http://example/egbook1/#Title2>. "); //updateString.append("<http://example/egbook1> dc:title \"Title1.1\". "); //updateString.append("<http://example/egbook1> dc:title \"Title1.2\". "); updateString.append("<http://example/egbook21> dc:title \"Title2\"; "); updateString.append("dc:title \"2.0\"^^xsd:double. "); updateString.append("<http://example/egbook3> dc:title \"Title3\". "); updateString.append("<http://example/egbook4> dc:title \"Title4\". "); updateString.append("<http://example/egbook5> dc:title \"Title5\". "); updateString.append("<http://example/egbook6> dc:title \"Title6\" "); updateString.append("}"); UpdateRequest update = UpdateFactory.create(updateString.toString()); UpdateAction.execute(update, newModel); StmtIterator iter = newModel.listStatements(); System.out.println("After add"); while (iter.hasNext()) { System.out.println(iter.next().toString()); } StringBuilder constructQueryString = new StringBuilder(); constructQueryString.append("PREFIX dc: <http://purl.org/dc/elements/1.1/>"); constructQueryString.append("CONSTRUCT {?sub dc:title <http://example/egbook1/#Title1>}"); constructQueryString.append("WHERE {"); constructQueryString.append("?sub dc:title <http://example/egbook1/#Title1>"); constructQueryString.append("}"); StringBuilder askQueryString = new StringBuilder(); askQueryString.append("PREFIX dc: <http://purl.org/dc/elements/1.1/>"); askQueryString.append("ASK {"); askQueryString.append("?sub dc:title <http://example/egbook1/#Title1>"); askQueryString.append("}"); StringBuilder selectQueryString = new StringBuilder(); selectQueryString.append("PREFIX dc: <http://purl.org/dc/elements/1.1/>"); selectQueryString.append("SELECT * "); selectQueryString.append("WHERE {"); selectQueryString.append("?sub ?pred ?obj"); selectQueryString.append("}"); Query cquery = QueryFactory.create(constructQueryString.toString()); System.out.println(cquery.getQueryType()); Query aquery = QueryFactory.create(askQueryString.toString()); System.out.println(aquery.getQueryType()); Query query = QueryFactory.create(selectQueryString.toString()); System.out.println(query.getQueryType()); QueryExecution queryExecution = QueryExecutionFactory.create(query, newModel); ByteArrayOutputStream baos = new ByteArrayOutputStream(); URI test = null; try { test = new URI("http://bla.org/dc/elements/1.1/double"); } catch (URISyntaxException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } System.out.println(test.getPath().toString().substring(test.getPath().lastIndexOf("/") + 1)); System.out.println("java.lang." + test.getPath().toString().substring(test.getPath().lastIndexOf("/") + 1).substring(0, 1) .toUpperCase() + test.getPath().toString().substring(test.getPath().lastIndexOf("/") + 1).substring(1)); String typ = "java.lang.Boolean"; String val = "true"; try { Object typedLiteral = Class.forName(typ, true, ClassLoader.getSystemClassLoader()) .getConstructor(String.class).newInstance(val); System.out.println("Type: " + typedLiteral.getClass().getName() + " Value: " + typedLiteral); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { System.out.println("Query..."); com.hp.hpl.jena.query.ResultSet results = queryExecution.execSelect(); System.out.println("RESULT:"); ResultSetFormatter.output(System.out, results, ResultSetFormat.syntaxJSON); // // // ResultSetFormatter.outputAsJSON(baos, results); // System.out.println(baos.toString()); // System.out.println("JsonTest: "); // JSONObject result = new JSONObject(baos.toString("ISO-8859-1")); // for (Iterator key = result.keys(); result.keys().hasNext(); ){ // System.out.println(key.next()); // // for (Iterator key2 = ((JSONObject)result.getJSONObject("head")).keys(); key2.hasNext(); ){ // System.out.println(key2.next()); // } // } // Model results = queryExecution.execConstruct(); // results.write(System.out, "TURTLE"); // for ( ; results.hasNext() ; ){ // QuerySolution soln = results.nextSolution() ; // RDFNode x = soln.get("sub") ; // System.out.println("result: "+soln.get("sub")+" hasTitle "+soln.get("obj")); // Resource r = soln.getResource("VarR") ; // Literal l = soln.getLiteral("VarL") ; // // } } catch (Exception e) { // TODO: handle exception } // StringBuilder updateString2 = new StringBuilder(); // updateString2.append("PREFIX dc: <http://purl.org/dc/elements/1.1/>"); // updateString2.append("DELETE DATA { "); // updateString2.append("<http://example/egbook3> dc:title \"Title3\" "); // updateString2.append("}"); // // UpdateAction.parseExecute(updateString2.toString(), newModel); // // iter = newModel.listStatements(); // System.out.println("After delete"); // while (iter.hasNext()) { // System.out.println(iter.next().toString()); // // } // // StringBuilder updateString3 = new StringBuilder(); // updateString3.append("PREFIX dc: <http://purl.org/dc/elements/1.1/>"); // updateString3.append("DELETE DATA { "); // updateString3.append("<http://example/egbook6> dc:title \"Title6\" "); // updateString3.append("}"); // updateString3.append("INSERT { "); // updateString3.append("<http://example/egbook6> dc:title \"New Title6\" "); // updateString3.append("}"); // // UpdateAction.parseExecute(updateString3.toString(), newModel); // UpdateAction.parseExecute( "prefix exp: <http://www.example.de>"+ // "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+ // "INSERT { graph <http://www.defaultgraph.de/graph1> {"+ // " <http://www.test.de#substructure1> <exp:has_relation3> <http://www.test.de#substructure2> ."+ // " <http://www.test.de#substructure1> <rdf:type> <http://www.test.de#substructuretype1> ."+ // " <http://www.test.de#substructure2> <rdf:type> <http://www.test.de#substructuretype2> ."+ // "}}", newModel2); // // iter = newModel.listStatements(); // System.out.println("After update"); // while (iter.hasNext()) { // System.out.println(iter.next().toString()); // // } }
From source file:dataflow.examples.TransferFiles.java
public static void main(String[] args) throws IOException, URISyntaxException { Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class); List<FilePair> filePairs = new ArrayList<FilePair>(); URI ftpInput = new URI(options.getInput()); FTPClient ftp = new FTPClient(); ftp.connect(ftpInput.getHost());/* w w w .j a v a 2 s .c o m*/ // After connection attempt, you should check the reply code to verify // success. int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); logger.error("FTP server refused connection."); throw new RuntimeException("FTP server refused connection."); } ftp.login("anonymous", "someemail@gmail.com"); String ftpPath = ftpInput.getPath(); FTPFile[] files = ftp.listFiles(ftpPath); URI gcsUri = null; if (options.getOutput().endsWith("/")) { gcsUri = new URI(options.getOutput()); } else { gcsUri = new URI(options.getOutput() + "/"); } for (FTPFile f : files) { logger.info("File: " + f.getName()); FilePair p = new FilePair(); p.server = ftpInput.getHost(); p.ftpPath = f.getName(); // URI ftpURI = new URI("ftp", p.server, f.getName(), ""); p.gcsPath = gcsUri.resolve(FilenameUtils.getName(f.getName())).toString(); filePairs.add(p); } ftp.logout(); Pipeline p = Pipeline.create(options); PCollection<FilePair> inputs = p.apply(Create.of(filePairs)); inputs.apply(ParDo.of(new FTPToGCS()).named("CopyToGCS")) .apply(AvroIO.Write.withSchema(FilePair.class).to(options.getOutput())); p.run(); }
From source file:edu.usc.goffish.gofs.tools.GoFSFormat.java
public static void main(String[] args) throws IOException { if (args.length < REQUIRED_ARGS) { PrintUsageAndQuit(null);//from w ww . j a v a 2 s . c o m } if (args.length == 1 && args[0].equals("-help")) { PrintUsageAndQuit(null); } Path executableDirectory; try { executableDirectory = Paths .get(GoFSFormat.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParent(); } catch (URISyntaxException e) { throw new RuntimeException("Unexpected error retrieving executable location", e); } Path configPath = executableDirectory.resolve(DEFAULT_CONFIG).normalize(); boolean copyBinaries = false; // parse optional arguments int i = 0; OptArgLoop: for (i = 0; i < args.length - REQUIRED_ARGS; i++) { switch (args[i]) { case "-config": i++; try { configPath = Paths.get(args[i]); } catch (InvalidPathException e) { PrintUsageAndQuit("Config file - " + e.getMessage()); } break; case "-copyBinaries": copyBinaries = true; break; default: break OptArgLoop; } } if (args.length - i < REQUIRED_ARGS) { PrintUsageAndQuit(null); } // finished parsing args if (i < args.length) { PrintUsageAndQuit("Unrecognized argument \"" + args[i] + "\""); } // parse config System.out.println("Parsing config..."); PropertiesConfiguration config = new PropertiesConfiguration(); config.setDelimiterParsingDisabled(true); try { config.load(Files.newInputStream(configPath)); } catch (ConfigurationException e) { throw new IOException(e); } // retrieve data nodes ArrayList<URI> dataNodes; { String[] dataNodesArray = config.getStringArray(GOFS_DATANODES_KEY); if (dataNodesArray.length == 0) { throw new ConversionException("Config must contain key " + GOFS_DATANODES_KEY); } dataNodes = new ArrayList<>(dataNodesArray.length); if (dataNodesArray.length == 0) { throw new ConversionException("Config key " + GOFS_DATANODES_KEY + " has invalid format - must define at least one data node"); } try { for (String node : dataNodesArray) { URI dataNodeURI = new URI(node); if (!"file".equalsIgnoreCase(dataNodeURI.getScheme())) { throw new ConversionException("config key " + GOFS_DATANODES_KEY + " value \"" + dataNodeURI + "\" has invalid format - data node urls must have 'file' scheme"); } else if (dataNodeURI.getPath() == null || dataNodeURI.getPath().isEmpty()) { throw new ConversionException("config key " + GOFS_DATANODES_KEY + " value \"" + dataNodeURI + "\" has invalid format - data node urls must have an absolute path specified"); } // ensure uri ends with a slash, so we know it is a directory if (!dataNodeURI.getPath().endsWith("/")) { dataNodeURI = dataNodeURI.resolve(dataNodeURI.getPath() + "/"); } dataNodes.add(dataNodeURI); } } catch (URISyntaxException e) { throw new ConversionException( "Config key " + GOFS_DATANODES_KEY + " has invalid format - " + e.getMessage()); } } // validate serializer type Class<? extends ISliceSerializer> serializerType; { String serializerTypeName = config.getString(GOFS_SERIALIZER_KEY); if (serializerTypeName == null) { throw new ConversionException("Config must contain key " + GOFS_SERIALIZER_KEY); } try { serializerType = SliceSerializerProvider.loadSliceSerializerType(serializerTypeName); } catch (ReflectiveOperationException e) { throw new ConversionException( "Config key " + GOFS_SERIALIZER_KEY + " has invalid format - " + e.getMessage()); } } // retrieve name node IInternalNameNode nameNode; try { nameNode = NameNodeProvider.loadNameNodeFromConfig(config, GOFS_NAMENODE_TYPE_KEY, GOFS_NAMENODE_LOCATION_KEY); } catch (ReflectiveOperationException e) { throw new RuntimeException("Unable to load name node", e); } System.out.println("Contacting name node..."); // validate name node if (!nameNode.isAvailable()) { throw new IOException("Name node at " + nameNode.getURI() + " is not available"); } System.out.println("Contacting data nodes..."); // validate data nodes for (URI dataNode : dataNodes) { // only attempt ssh if host exists if (dataNode.getHost() != null) { try { SSHHelper.SSH(dataNode, "true"); } catch (IOException e) { throw new IOException("Data node at " + dataNode + " is not available", e); } } } // create temporary directory Path workingDir = Files.createTempDirectory("gofs_format"); try { // create deploy directory Path deployDirectory = Files.createDirectory(workingDir.resolve(DATANODE_DIR_NAME)); // create empty slice directory Files.createDirectory(deployDirectory.resolve(DataNode.DATANODE_SLICE_DIR)); // copy binaries if (copyBinaries) { System.out.println("Copying binaries..."); FileUtils.copyDirectory(executableDirectory.toFile(), deployDirectory.resolve(executableDirectory.getFileName()).toFile()); } // write config file Path dataNodeConfigFile = deployDirectory.resolve(DataNode.DATANODE_CONFIG); try { // create config for every data node and scp deploy folder into place for (URI dataNodeParent : dataNodes) { URI dataNode = dataNodeParent.resolve(DATANODE_DIR_NAME); PropertiesConfiguration datanode_config = new PropertiesConfiguration(); datanode_config.setDelimiterParsingDisabled(true); datanode_config.setProperty(DataNode.DATANODE_INSTALLED_KEY, true); datanode_config.setProperty(DataNode.DATANODE_NAMENODE_TYPE_KEY, config.getString(GOFS_NAMENODE_TYPE_KEY)); datanode_config.setProperty(DataNode.DATANODE_NAMENODE_LOCATION_KEY, config.getString(GOFS_NAMENODE_LOCATION_KEY)); datanode_config.setProperty(DataNode.DATANODE_LOCALHOSTURI_KEY, dataNode.toString()); try { datanode_config.save(Files.newOutputStream(dataNodeConfigFile)); } catch (ConfigurationException e) { throw new IOException(e); } System.out.println("Formatting data node " + dataNode.toString() + "..."); // scp everything into place on the data node SCPHelper.SCP(deployDirectory, dataNodeParent); // update name node nameNode.addDataNode(dataNode); } // update name node nameNode.setSerializer(serializerType); } catch (Exception e) { System.out.println( "ERROR: data node formatting interrupted - name node and data nodes are in an inconsistent state and require clean up"); throw e; } System.out.println("GoFS format complete"); } finally { FileUtils.deleteQuietly(workingDir.toFile()); } }
From source file:MainClass.java
public static void main(String args[]) throws Exception { URI u = new URI("http://www.java2s.com"); System.out.println("The URI is " + u); if (u.isOpaque()) { System.out.println("This is an opaque URI."); System.out.println("The scheme is " + u.getScheme()); System.out.println("The scheme specific part is " + u.getSchemeSpecificPart()); System.out.println("The fragment ID is " + u.getFragment()); } else {// ww w. jav a 2 s .c o m System.out.println("This is a hierarchical URI."); System.out.println("The scheme is " + u.getScheme()); u = u.parseServerAuthority(); System.out.println("The host is " + u.getUserInfo()); System.out.println("The user info is " + u.getUserInfo()); System.out.println("The port is " + u.getPort()); System.out.println("The path is " + u.getPath()); System.out.println("The query string is " + u.getQuery()); System.out.println("The fragment ID is " + u.getFragment()); } }
From source file:examples.mail.IMAPExportMbox.java
public static void main(String[] args) throws IOException { int connect_timeout = CONNECT_TIMEOUT; int read_timeout = READ_TIMEOUT; int argIdx = 0; String eol = EOL_DEFAULT;//from w w w . j a va 2s . co m boolean printHash = false; boolean printMarker = false; int retryWaitSecs = 0; for (argIdx = 0; argIdx < args.length; argIdx++) { if (args[argIdx].equals("-c")) { connect_timeout = Integer.parseInt(args[++argIdx]); } else if (args[argIdx].equals("-r")) { read_timeout = Integer.parseInt(args[++argIdx]); } else if (args[argIdx].equals("-R")) { retryWaitSecs = Integer.parseInt(args[++argIdx]); } else if (args[argIdx].equals("-LF")) { eol = LF; } else if (args[argIdx].equals("-CRLF")) { eol = CRLF; } else if (args[argIdx].equals("-.")) { printHash = true; } else if (args[argIdx].equals("-X")) { printMarker = true; } else { break; } } final int argCount = args.length - argIdx; if (argCount < 2) { System.err.println("Usage: IMAPExportMbox [-LF|-CRLF] [-c n] [-r n] [-R n] [-.] [-X]" + " imap[s]://user:password@host[:port]/folder/path [+|-]<mboxfile> [sequence-set] [itemnames]"); System.err.println( "\t-LF | -CRLF set end-of-line to LF or CRLF (default is the line.separator system property)"); System.err.println("\t-c connect timeout in seconds (default 10)"); System.err.println("\t-r read timeout in seconds (default 10)"); System.err.println("\t-R temporary failure retry wait in seconds (default 0; i.e. disabled)"); System.err.println("\t-. print a . for each complete message received"); System.err.println("\t-X print the X-IMAP line for each complete message received"); System.err.println( "\tthe mboxfile is where the messages are stored; use '-' to write to standard output."); System.err.println( "\tPrefix filename with '+' to append to the file. Prefix with '-' to allow overwrite."); System.err.println( "\ta sequence-set is a list of numbers/number ranges e.g. 1,2,3-10,20:* - default 1:*"); System.err .println("\titemnames are the message data item name(s) e.g. BODY.PEEK[HEADER.FIELDS (SUBJECT)]" + " or a macro e.g. ALL - default (INTERNALDATE BODY.PEEK[])"); System.exit(1); } final URI uri = URI.create(args[argIdx++]); final String file = args[argIdx++]; String sequenceSet = argCount > 2 ? args[argIdx++] : "1:*"; final String itemNames; // Handle 0, 1 or multiple item names if (argCount > 3) { if (argCount > 4) { StringBuilder sb = new StringBuilder(); sb.append("("); for (int i = 4; i <= argCount; i++) { if (i > 4) { sb.append(" "); } sb.append(args[argIdx++]); } sb.append(")"); itemNames = sb.toString(); } else { itemNames = args[argIdx++]; } } else { itemNames = "(INTERNALDATE BODY.PEEK[])"; } final boolean checkSequence = sequenceSet.matches("\\d+:(\\d+|\\*)"); // are we expecting a sequence? final MboxListener chunkListener; if (file.equals("-")) { chunkListener = null; } else if (file.startsWith("+")) { final File mbox = new File(file.substring(1)); System.out.println("Appending to file " + mbox); chunkListener = new MboxListener(new BufferedWriter(new FileWriter(mbox, true)), eol, printHash, printMarker, checkSequence); } else if (file.startsWith("-")) { final File mbox = new File(file.substring(1)); System.out.println("Writing to file " + mbox); chunkListener = new MboxListener(new BufferedWriter(new FileWriter(mbox, false)), eol, printHash, printMarker, checkSequence); } else { final File mbox = new File(file); if (mbox.exists()) { throw new IOException("mailbox file: " + mbox + " already exists!"); } System.out.println("Creating file " + mbox); chunkListener = new MboxListener(new BufferedWriter(new FileWriter(mbox)), eol, printHash, printMarker, checkSequence); } String path = uri.getPath(); if (path == null || path.length() < 1) { throw new IllegalArgumentException("Invalid folderPath: '" + path + "'"); } String folder = path.substring(1); // skip the leading / // suppress login details final PrintCommandListener listener = new PrintCommandListener(System.out, true) { @Override public void protocolReplyReceived(ProtocolCommandEvent event) { if (event.getReplyCode() != IMAPReply.PARTIAL) { // This is dealt with by the chunk listener super.protocolReplyReceived(event); } } }; // Connect and login final IMAPClient imap = IMAPUtils.imapLogin(uri, connect_timeout * 1000, listener); String maxIndexInFolder = null; try { imap.setSoTimeout(read_timeout * 1000); if (!imap.select(folder)) { throw new IOException("Could not select folder: " + folder); } for (String line : imap.getReplyStrings()) { maxIndexInFolder = matches(line, PATEXISTS, 1); if (maxIndexInFolder != null) { break; } } if (chunkListener != null) { imap.setChunkListener(chunkListener); } // else the command listener displays the full output without processing while (true) { boolean ok = imap.fetch(sequenceSet, itemNames); // If the fetch failed, can we retry? if (!ok && retryWaitSecs > 0 && chunkListener != null && checkSequence) { final String replyString = imap.getReplyString(); //includes EOL if (startsWith(replyString, PATTEMPFAIL)) { System.err.println("Temporary error detected, will retry in " + retryWaitSecs + "seconds"); sequenceSet = (chunkListener.lastSeq + 1) + ":*"; try { Thread.sleep(retryWaitSecs * 1000); } catch (InterruptedException e) { // ignored } } else { throw new IOException( "FETCH " + sequenceSet + " " + itemNames + " failed with " + replyString); } } else { break; } } } catch (IOException ioe) { String count = chunkListener == null ? "?" : Integer.toString(chunkListener.total); System.err.println("FETCH " + sequenceSet + " " + itemNames + " failed after processing " + count + " complete messages "); if (chunkListener != null) { System.err.println("Last complete response seen: " + chunkListener.lastFetched); } throw ioe; } finally { if (printHash) { System.err.println(); } if (chunkListener != null) { chunkListener.close(); final Iterator<String> missingIds = chunkListener.missingIds.iterator(); if (missingIds.hasNext()) { StringBuilder sb = new StringBuilder(); for (;;) { sb.append(missingIds.next()); if (!missingIds.hasNext()) { break; } sb.append(","); } System.err.println("*** Missing ids: " + sb.toString()); } } imap.logout(); imap.disconnect(); } if (chunkListener != null) { System.out.println("Processed " + chunkListener.total + " messages."); } if (maxIndexInFolder != null) { System.out.println("Folder contained " + maxIndexInFolder + " messages."); } }