List of usage examples for org.apache.commons.httpclient.methods HeadMethod getParams
@Override
public HttpMethodParams getParams()
From source file:com.gnizr.core.util.GnizrDaoUtil.java
public static Integer detectMIMEType(String url) { try {/*from www . j a v a 2s .c o m*/ HttpClient httpClient = new HttpClient(); HeadMethod method = new HeadMethod(url); method.getParams().setIntParameter("http.socket.timeout", 5000); int code = httpClient.executeMethod(method); if (code == 200) { Header h = method.getResponseHeader("Content-Type"); if (h != null) { HeaderElement[] headElm = h.getElements(); if (headElm != null & headElm.length > 0) { String mimeType = headElm[0].getValue(); if (mimeType == null) { mimeType = headElm[0].getName(); } if (mimeType != null) { return getMimeTypeIdCode(mimeType); } } } } } catch (Exception e) { // no code; } return MIMEType.UNKNOWN; }
From source file:com.xmlcalabash.library.ApacheHttpRequest.java
private HeadMethod doHead() { HeadMethod method = new HeadMethod(requestURI.toASCIIString()); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); for (Header header : headers) { method.addRequestHeader(header); }/*from w ww .j av a 2 s . c o m*/ return method; }
From source file:com.sittinglittleduck.DirBuster.workGenerators.WorkerGenerator.java
/** Thread run method */ public void run() { String currentDir = "/"; int failcode = 404; String line;//from w w w . ja v a 2 s .c om Vector extToCheck = new Vector(10, 5); boolean recursive = true; int passTotal = 0; // -------------------------------------------------- try { // find the total number of requests to be made, per pass // based on the fact there is a single entry per line BufferedReader d = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile))); passTotal = 0; while ((line = d.readLine()) != null) { if (!line.startsWith("#")) { passTotal++; } } manager.setTotalPass(passTotal); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } // ------------------------------------------------- // checks if the server surports heads requests if (manager.getAuto()) { try { URL headurl = new URL(firstPart); HeadMethod httphead = new HeadMethod(headurl.toString()); // set the custom HTTP headers Vector HTTPheaders = manager.getHTTPHeaders(); for (int a = 0; a < HTTPheaders.size(); a++) { HTTPHeader httpHeader = (HTTPHeader) HTTPheaders.elementAt(a); /* * Host header has to be set in a different way! */ if (httpHeader.getHeader().startsWith("Host:")) { httphead.getParams().setVirtualHost(httpHeader.getValue()); } else { httphead.setRequestHeader(httpHeader.getHeader(), httpHeader.getValue()); } } httphead.setFollowRedirects(Config.followRedirects); int responceCode = httpclient.executeMethod(httphead); if (Config.debug) { System.out.println("DEBUG WokerGen: responce code for head check = " + responceCode); } // if the responce code is method not implemented or if the head requests return // 400! if (responceCode == 501 || responceCode == 400 || responceCode == 405) { if (Config.debug) { System.out.println( "DEBUG WokerGen: Changing to GET only HEAD test returned 501(method no implmented) or a 400"); } // switch the mode to just GET requests manager.setAuto(false); } } catch (MalformedURLException e) { // TODO deal with error } catch (IOException e) { // TODO deal with error } } // end of checks to see if server surpports head requests int counter = 0; while ((!dirQueue.isEmpty() || !workQueue.isEmpty() || !manager.areWorkersAlive()) && recursive) { // get the dir we are about to process String baseResponce = null; recursive = manager.isRecursive(); BaseCase baseCaseObj = null; // rest the skip skipCurrent = false; // deal with the dirs try { // get item from queue // System.out.println("gen about to take"); DirToCheck tempDirToCheck = dirQueue.take(); // System.out.println("gen taken"); // get dir name currentDir = tempDirToCheck.getName(); // get any extention that need to be checked extToCheck = tempDirToCheck.getExts(); manager.setCurrentlyProcessing(currentDir); } catch (InterruptedException e) { e.printStackTrace(); } started = currentDir; // generate the list of dirs if (manager.getDoDirs()) { // find the fail case for the dir URL failurl = null; try { baseResponce = null; baseCaseObj = GenBaseCase.genBaseCase(manager, firstPart + currentDir, true, null); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // end of dir fail case if (stopMe) { return; } // generate work links try { // readin dir names BufferedReader d = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile))); if (Config.debug) { System.out.println("DEBUG WokerGen: Generating dir list for " + firstPart); } URL currentURL; // add the first item while doing dir's if (counter == 0) { try { String method; if (manager.getAuto() && !baseCaseObj.useContentAnalysisMode() && !baseCaseObj.isUseRegexInstead()) { method = "HEAD"; } else { method = "GET"; } currentURL = new URL(firstPart + currentDir); // System.out.println("first part = " + firstPart); // System.out.println("current dir = " + currentDir); workQueue.put(new WorkUnit(currentURL, true, "GET", baseCaseObj, null)); if (Config.debug) { System.out.println("DEBUG WokerGen: 1 adding dir to work list " + method + " " + currentDir.toString()); } } catch (MalformedURLException ex) { ex.printStackTrace(); } catch (InterruptedException ex) { ex.printStackTrace(); } } // end of dealing with first item int dirsProcessed = 0; // add the rest of the dirs while ((line = d.readLine()) != null) { // code to skip the current work load if (skipCurrent) { // add the totalnumber per pass - the amount process this pass to the // work correction total manager.addToWorkCorrection(passTotal - dirsProcessed); break; } // if the line is not empty or starts with a # if (!line.equalsIgnoreCase("") && !line.startsWith("#")) { line = line.trim(); line = makeItemsafe(line); try { String method; if (manager.getAuto() && !baseCaseObj.useContentAnalysisMode() && !baseCaseObj.isUseRegexInstead()) { method = "HEAD"; } else { method = "GET"; } currentURL = new URL(firstPart + currentDir + line + "/"); // BaseCase baseCaseObj = new BaseCase(currentURL, failcode, true, // failurl, baseResponce); // if the base case is null then we need to switch to content // anylsis mode // System.out.println("Gen about to add to queue"); workQueue.put(new WorkUnit(currentURL, true, method, baseCaseObj, line)); // System.out.println("Gen finshed adding to queue"); if (Config.debug) { System.out.println("DEBUG WokerGen: 2 adding dir to work list " + method + " " + currentURL.toString()); } } catch (MalformedURLException e) { // TODO deal with bad line // e.printStackTrace(); // do nothing if it's malformed, I dont care about them! } catch (InterruptedException e) { e.printStackTrace(); } // if there is a call to stop the work gen then stop! if (stopMe) { return; } dirsProcessed++; } } // end of while } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } // generate the list of files if (manager.getDoFiles()) { baseResponce = null; URL failurl = null; // loop for all the different file extentions for (int b = 0; b < extToCheck.size(); b++) { // only test if we are surposed to ExtToCheck extTemp = (ExtToCheck) extToCheck.elementAt(b); if (extTemp.toCheck()) { fileExtention = ""; if (extTemp.getName().equals(ExtToCheck.BLANK_EXT)) { fileExtention = ""; } else { fileExtention = "." + extTemp.getName(); } try { // get the base for this extention baseCaseObj = GenBaseCase.genBaseCase(manager, firstPart + currentDir, false, fileExtention); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // if the manager has sent the stop command then exit if (stopMe) { return; } try { BufferedReader d = new BufferedReader( new InputStreamReader(new FileInputStream(inputFile))); // if(failcode != 200) // { int filesProcessed = 0; while ((line = d.readLine()) != null) { // code to skip the current work load if (skipCurrent) { manager.addToWorkCorrection(passTotal - filesProcessed); break; } // dont process is the line empty for starts with a # if (!line.equalsIgnoreCase("") && !line.startsWith("#")) { line = line.trim(); line = makeItemsafe(line); try { String method; if (manager.getAuto() && !baseCaseObj.useContentAnalysisMode() && !baseCaseObj.isUseRegexInstead()) { method = "HEAD"; } else { method = "GET"; } URL currentURL = new URL(firstPart + currentDir + line + fileExtention); // BaseCase baseCaseObj = new BaseCase(currentURL, true, // failurl, baseResponce); workQueue.put(new WorkUnit(currentURL, false, method, baseCaseObj, line)); if (Config.debug) { System.out.println("DEBUG WokerGen: adding file to work list " + method + " " + currentURL.toString()); } } catch (MalformedURLException e) { // e.printStackTrace(); // again do nothing as I dont care } catch (InterruptedException e) { e.printStackTrace(); } if (stopMe) { return; } filesProcessed++; } } // end of while // } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } // end of file ext loop } // end of if files finished = started; counter++; try { Thread.sleep(200); } catch (InterruptedException ex) { ex.printStackTrace(); } } // end of main while // System.out.println("Gen FINISHED!"); // manager.youAreFinished(); }
From source file:org.apache.maven.wagon.providers.webdav.AbstractHttpClientWagonTest.java
public void testSetPreemptiveAuthParamViaConfig() { HttpMethodConfiguration methodConfig = new HttpMethodConfiguration(); methodConfig.addParam(HttpClientParams.PREEMPTIVE_AUTHENTICATION, "%b,true"); HttpConfiguration config = new HttpConfiguration(); config.setAll(methodConfig);/*from w w w . java 2 s . co m*/ TestWagon wagon = new TestWagon(); wagon.setHttpConfiguration(config); HeadMethod method = new HeadMethod(); wagon.setParameters(method); HttpMethodParams params = method.getParams(); assertNotNull(params); assertTrue(params.isParameterTrue(HttpClientParams.PREEMPTIVE_AUTHENTICATION)); }
From source file:org.apache.maven.wagon.providers.webdav.AbstractHttpClientWagonTest.java
public void testSetMaxRedirectsParamViaConfig() { HttpMethodConfiguration methodConfig = new HttpMethodConfiguration(); int maxRedirects = 2; methodConfig.addParam(HttpClientParams.MAX_REDIRECTS, "%i," + maxRedirects); HttpConfiguration config = new HttpConfiguration(); config.setAll(methodConfig);/*from w w w . j av a 2 s .co m*/ TestWagon wagon = new TestWagon(); wagon.setHttpConfiguration(config); HeadMethod method = new HeadMethod(); wagon.setParameters(method); HttpMethodParams params = method.getParams(); assertNotNull(params); assertEquals(maxRedirects, params.getIntParameter(HttpClientParams.MAX_REDIRECTS, -1)); }
From source file:org.mbs3.deliciouschecker.DeliciousChecker.java
/** * @param args//w w w.j av a 2 s .c om */ public static void main(String[] args) { System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog"); System.out.println("Connecting to del.icio.us"); Delicious connection = new Delicious(DeliciousChecker.username, DeliciousChecker.password); System.out.println("Getting post data for url verification"); List allPosts = connection.getAllPosts(); Iterator allPostsIterator = allPosts.iterator(); System.out.println("Received " + allPosts.size() + " different posts, checking each"); HttpClient hc = new HttpClient(); hc.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_0); hc.getParams().setParameter("http.socket.timeout", new Integer(1000)); while (allPostsIterator.hasNext()) { Post post = (Post) allPostsIterator.next(); //System.out.println("Trying " + post.getHref()); try { HeadMethod hm = new HeadMethod(post.getHref()); hm.setRequestHeader("User-agent", DeliciousChecker.useragent); hm.getParams().setParameter("http.socket.timeout", new Integer(5000)); int response = hc.executeMethod(hm); if (response != 200) { System.out .println(post.getDescription() + "(" + post.getHref() + ") returned HTTP " + response); post.setTag(post.getTag() + " broken"); } } catch (Exception ex) { System.out.println(post.getDescription() + "(" + post.getHref() + ") returned " + ex); post.setTag(post.getTag() + " exception"); } } }