protected String getRallyAPIError(String responseXML) { String errorMsg = ""; try { org.jdom.input.SAXBuilder bSAX = new org.jdom.input.SAXBuilder(); org.jdom.Document doc = bSAX.build(new StringReader(responseXML)); Element root = doc.getRootElement(); XPath xpath = XPath.newInstance("//Errors"); List xlist = xpath.selectNodes(root); Iterator iter = xlist.iterator(); while (iter.hasNext()) { Element item = (Element) iter.next(); errorMsg = item.getChildText("OperationResultError"); } } catch (Exception e) { errorMsg = e.toString(); } return errorMsg; }
public List<CIBuild> getBuilds(CIJob job) throws PhrescoException { if (debugEnabled) { S_LOGGER.debug("Entering Method CIManagerImpl.getCIBuilds(CIJob job)"); } List<CIBuild> ciBuilds = null; try { if (debugEnabled) { S_LOGGER.debug("getCIBuilds() JobName = " + job.getName()); } JsonArray jsonArray = getBuildsArray(job); ciBuilds = new ArrayList<CIBuild>(jsonArray.size()); Gson gson = new Gson(); CIBuild ciBuild = null; for (int i = 0; i < jsonArray.size(); i++) { ciBuild = gson.fromJson(jsonArray.get(i), CIBuild.class); setBuildStatus(ciBuild, job); String buildUrl = ciBuild.getUrl(); String jenkinUrl = job.getJenkinsUrl() + ":" + job.getJenkinsPort(); buildUrl = buildUrl.replaceAll( "localhost:" + job.getJenkinsPort(), jenkinUrl); // when displaying url it should display setup machine ip ciBuild.setUrl(buildUrl); ciBuilds.add(ciBuild); } } catch (Exception e) { if (debugEnabled) { S_LOGGER.debug( "Entering Method CIManagerImpl.getCIBuilds(CIJob job) " + e.getLocalizedMessage()); } } return ciBuilds; }
private void setSvnCredential(CIJob job) throws JDOMException, IOException { S_LOGGER.debug("Entering Method CIManagerImpl.setSvnCredential"); try { String jenkinsTemplateDir = Utility.getJenkinsTemplateDir(); String credentialFilePath = jenkinsTemplateDir + job.getRepoType() + HYPHEN + CREDENTIAL_XML; if (debugEnabled) { S_LOGGER.debug("credentialFilePath ... " + credentialFilePath); } File credentialFile = new File(credentialFilePath); SvnProcessor processor = new SvnProcessor(credentialFile); // DataInputStream in = new DataInputStream(new FileInputStream(credentialFile)); // while (in.available() != 0) { // System.out.println(in.readLine()); // } // in.close(); processor.changeNodeValue("credentials/entry//userName", job.getUserName()); processor.changeNodeValue("credentials/entry//password", job.getPassword()); processor.writeStream(new File(Utility.getJenkinsHome() + File.separator + job.getName())); // jenkins home location String jenkinsJobHome = System.getenv(JENKINS_HOME); StringBuilder builder = new StringBuilder(jenkinsJobHome); builder.append(File.separator); processor.writeStream(new File(builder.toString() + CI_CREDENTIAL_XML)); } catch (Exception e) { S_LOGGER.error( "Entered into the catch block of CIManagerImpl.setSvnCredential " + e.getLocalizedMessage()); } }
public List parsePage(String pageCode) { List sections = new ArrayList(); List folders = new ArrayList(); List files = new ArrayList(); int start = pageCode.indexOf("<div id=\"list-view\" class=\"view\""); int end = pageCode.indexOf("<div id=\"gallery-view\" class=\"view\""); String usefulSection = ""; if (start != -1 && end != -1) { usefulSection = pageCode.substring(start, end); } else { debug("Could not parse page"); } try { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(usefulSection)); Document doc = db.parse(is); NodeList divs = doc.getElementsByTagName("div"); for (int i = 0; i < divs.getLength(); i++) { Element div = (Element) divs.item(i); boolean isFolder = false; if (div.getAttribute("class").equals("filename")) { NodeList imgs = div.getElementsByTagName("img"); for (int j = 0; j < imgs.getLength(); j++) { Element img = (Element) imgs.item(j); if (img.getAttribute("class").indexOf("folder") > 0) { isFolder = true; } else { isFolder = false; // it's a file } } NodeList anchors = div.getElementsByTagName("a"); Element anchor = (Element) anchors.item(0); String attr = anchor.getAttribute("href"); String fileName = anchor.getAttribute("title"); String fileURL; if (isFolder && !attr.equals("#")) { folders.add(attr); folders.add(fileName); } else if (!isFolder && !attr.equals("#")) { // Dropbox uses ajax to get the file for download, so the url isn't enough. We must be // sneaky here. fileURL = "https://dl.dropbox.com" + attr.substring(23) + "?dl=1"; files.add(fileURL); files.add(fileName); } } } } catch (Exception e) { debug(e.toString()); } sections.add(files); sections.add(folders); return sections; }
public static final void main(String[] args) throws Exception { String uri = args[0]; int reqNum = Integer.parseInt(args[1]); int reqTerm = Integer.parseInt(args[2]); try { // new ClientSimple().request1(uri, reqNum, reqTerm); new ClientSimple().request2(uri, reqNum, reqTerm); } catch (Exception e) { System.out.println("ERROR:" + e.getMessage()); } }
public String getData(String url) { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); String response = ""; try { ResponseHandler<String> responseHandler = new BasicResponseHandler(); response = client.execute(get, responseHandler); } catch (Exception e) { debug(e.toString()); } return response; }
private void setMailCredential(CIJob job) { if (debugEnabled) { S_LOGGER.debug("Entering Method CIManagerImpl.setMailCredential"); } try { String jenkinsTemplateDir = Utility.getJenkinsTemplateDir(); String mailFilePath = jenkinsTemplateDir + MAIL + HYPHEN + CREDENTIAL_XML; if (debugEnabled) { S_LOGGER.debug("configFilePath ... " + mailFilePath); } File mailFile = new File(mailFilePath); SvnProcessor processor = new SvnProcessor(mailFile); // DataInputStream in = new DataInputStream(new FileInputStream(mailFile)); // while (in.available() != 0) { // System.out.println(in.readLine()); // } // in.close(); // Mail have to go with jenkins running email address InetAddress ownIP = InetAddress.getLocalHost(); processor.changeNodeValue( CI_HUDSONURL, HTTP_PROTOCOL + PROTOCOL_POSTFIX + ownIP.getHostAddress() + COLON + job.getJenkinsPort() + FORWARD_SLASH + CI + FORWARD_SLASH); processor.changeNodeValue("smtpAuthUsername", job.getSenderEmailId()); processor.changeNodeValue("smtpAuthPassword", job.getSenderEmailPassword()); processor.changeNodeValue("adminAddress", job.getSenderEmailId()); // jenkins home location String jenkinsJobHome = System.getenv(JENKINS_HOME); StringBuilder builder = new StringBuilder(jenkinsJobHome); builder.append(File.separator); processor.writeStream(new File(builder.toString() + CI_MAILER_XML)); } catch (Exception e) { S_LOGGER.error( "Entered into the catch block of CIManagerImpl.setMailCredential " + e.getLocalizedMessage()); } }
public void request1(String uri, int reqNum, int reqTerm) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); if (reqNum == 0) { System.out.println( String.format("request to %s infinite times with term '%d' ms", uri, reqTerm)); } else { System.out.println( String.format("request to %s '%d' times with term '%d' ms", uri, reqNum, reqTerm)); } int i = 0, tick = 0; HttpGet httpGet = new HttpGet(uri); CloseableHttpResponse response; HttpEntity entity; while (true) { usleep(reqTerm); tick = (int) (Math.random() * 10) % 2; if (tick == 0) { continue; } System.out.println("request " + httpGet.getURI()); response = httpclient.execute(httpGet); System.out.println("--> response status = " + response.getStatusLine()); // response handler try { entity = response.getEntity(); EntityUtils.consume(entity); } catch (Exception e) { System.out.println(" --> http fail:" + e.getMessage()); } finally { // Thread.sleep(5000); //테스트에만 썼다. close 지연시키려고. response.close(); } System.out.println("----------------------------------------"); if (reqNum != 0 && reqNum < ++i) { break; } } }
public void downloadFile(String url, String savePath, String fileName) { try { URL theURL = new URL(url); InputStream input = theURL.openStream(); OutputStream output = new FileOutputStream(new File(savePath, fileName)); try { byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) { output.write(buffer, 0, bytesRead); } } catch (Exception e) { debug(e.toString()); } finally { output.close(); } } catch (Exception e) { debug(e.toString()); } }
public String getFolderName(String pageCode) { String usefulSection = pageCode.substring( pageCode.indexOf("<h3 id=\"breadcrumb\">"), pageCode.indexOf("<div id=\"list-view\" class=\"view\"")); String folderName; try { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(usefulSection)); Document doc = db.parse(is); NodeList divs = doc.getElementsByTagName("h3"); for (int i = 0; i < divs.getLength(); i++) { Element div = (Element) divs.item(i); String a = div.getTextContent(); folderName = a.substring(a.indexOf("/>") + 2).trim(); return folderName; } } catch (Exception e) { debug(e.toString()); } return "Error!"; }
private boolean saveAndVerify(ZipInputStream data) throws IOException { try { ZipEntry ze; while ((ze = data.getNextEntry()) != null) { // Filename + reference to file. String filename = ze.getName(); File output = new File(local_path + filename); if (filename.endsWith("/")) { output.mkdirs(); } else { if (output.exists()) { // Delete the file if it already exists. if (!output.delete()) { return false; } } if (output.createNewFile()) { FileOutputStream out = new FileOutputStream(output); byte[] buffer = new byte[1024]; int count; while ((count = data.read(buffer)) != -1) { out.write(buffer, 0, count); } } else { return false; } } } } catch (Exception e) { e.printStackTrace(); return false; } finally { data.close(); } return true; }
protected Exception postJsonToPipelineWithRetry( String endpoint, List docs, ArrayList<String> mutable, Exception lastExc, int requestId) throws Exception { Exception retryAfterException = null; try { postJsonToPipeline(endpoint, docs, requestId); if (lastExc != null) log.info( "Re-try request " + requestId + " to " + endpoint + " succeeded after seeing a " + lastExc.getMessage()); } catch (Exception exc) { log.warn("Failed to send request " + requestId + " to '" + endpoint + "' due to: " + exc); if (mutable.size() > 1) { // try another endpoint but update the cloned list to avoid re-hitting the one having an // error if (log.isDebugEnabled()) log.debug("Will re-try failed request " + requestId + " on next endpoint in the list"); mutable.remove(endpoint); retryAfterException = exc; } else { // no other endpoints to try ... brief wait and then retry log.warn( "No more endpoints available to try ... will retry to send request " + requestId + " to " + endpoint + " after waiting 1 sec"); try { Thread.sleep(1000); } catch (InterruptedException ignore) { Thread.interrupted(); } // note we want the exception to propagate from here up the stack since we re-tried and it // didn't work postJsonToPipeline(endpoint, docs, requestId); log.info("Re-try request " + requestId + " to " + endpoint + " succeeded"); retryAfterException = null; // return success condition } } return retryAfterException; }