Example usage for org.apache.commons.httpclient URIException printStackTrace

List of usage examples for org.apache.commons.httpclient URIException printStackTrace

Introduction

In this page you can find the example usage for org.apache.commons.httpclient URIException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Print this HttpException and its stack trace to the standard error stream.

Usage

From source file:de.fuberlin.wiwiss.marbles.loading.SemanticWebClient.java

/**
 * Initiates a {@link DereferencerBatch} to retrieve data for a given resource 
 * /*from   ww  w .  ja  va  2 s .c  om*/
 * @param resource
 * @return List of URLs queries in the process; these may be looked up in the metadata store for details
 */
public List<URI> discoverResource(Resource resource, boolean wait) {
    List<URI> urlsToBeFetched = getURLsForResource(resource);
    DereferencerBatch dereferencerBatch = new DereferencerBatch(cacheController, uriQueue, dataProviders,
            resource, MAX_STEPS, MAX_REDIRECTS);

    /* provide URLs to dereferencer */
    for (URI url : urlsToBeFetched) {
        try {
            dereferencerBatch.loadURL(url, 0 /* step */, 0 /* redirect step */, false /* don't force reload */);
        } catch (URIException e) {
            e.printStackTrace();
        }
    }

    /* Initiate link retrieval from any previous data */
    dereferencerBatch.processLinks(1);

    /* Wait loop with timeout */
    long timeStarted = System.currentTimeMillis();
    System.err.println(Thread.currentThread().getName() + ": starting discoverResource() at " + timeStarted);

    if (wait) {
        synchronized (dereferencerBatch) {
            while (dereferencerBatch.hasPending(0)
                    || ((System.currentTimeMillis() - timeStarted < TIME_LIMIT_ADDITIONAL * 1000)
                            && dereferencerBatch.hasPending())) {
                try {
                    dereferencerBatch.wait(100);
                } catch (InterruptedException e) {
                }
            }
        }
    }
    System.err.println(Thread.currentThread().getName() + ": finished discoverResource() after "
            + ((System.currentTimeMillis() - timeStarted) / 1000) + "s");

    /*
     * We stop waiting here so that the data retrieved so far can be shown to the client.
     * Nonetheless, retrieval is not canceled - the client could refresh at a later time to get it
     * (AJAX automation would make a lot of sense here), and additional information can be incorporated
     * into subsequent views 
     */
    return dereferencerBatch.getRetrievedURLs();
}

From source file:com.cyberway.issue.crawler.datamodel.ServerCache.java

/**
 * Get the {@link CrawlServer} associated with <code>curi</code>.
 * @param cauri CandidateURI we're to get server from.
 * @return CrawlServer instance that matches the passed CandidateURI.
 *//*w  w  w  .  jav a2  s.c  o  m*/
public CrawlServer getServerFor(CandidateURI cauri) {
    CrawlServer cs = null;
    try {
        String key = CrawlServer.getServerKey(cauri);
        // TODOSOMEDAY: make this robust against those rare cases
        // where authority is not a hostname.
        if (key != null) {
            cs = getServerFor(key);
        }
    } catch (URIException e) {
        logger.severe(e.getMessage() + ": " + cauri);
        e.printStackTrace();
    } catch (NullPointerException npe) {
        logger.severe(npe.getMessage() + ": " + cauri);
        npe.printStackTrace();
    }
    return cs;
}

From source file:ensen.controler.AnnotationClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;/*  w  w w.java 2  s  .  c  o m*/

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        client.setHttpConnectionManager(new MultiThreadedHttpConnectionManager());
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            System.out.println("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        InputStream responseBodyStream = method.getResponseBodyAsStream(); //Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

        int b = responseBodyStream.read();
        ArrayList<Integer> bytes = new ArrayList<Integer>();
        while (b != -1) {
            bytes.add(b);
            b = responseBodyStream.read();
        }
        byte[] responseBody = new byte[bytes.size()];

        for (int i = 0; i < bytes.size(); i++) {
            responseBody[i] = bytes.get(i).byteValue();
        }
        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        response = new String(responseBody);

    } catch (HttpException e) {
        System.out.println("Fatal protocol violation: " + e.getMessage());
        try {
            System.err.println(method.getURI());
        } catch (URIException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        System.out.println("Fatal transport error: " + e.getMessage());
        System.out.println(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:de.fuberlin.wiwiss.marbles.loading.DereferencerBatch.java

/**
 * Called by {@link DereferencerThread} once data has been retrieved.
 * Handles insertion into cache, processes redirects, and initiates following of known links
 * for the retrieved URL using {@link #processLinks(int, Resource...)} 
 *///  w  ww . ja va  2  s. c  o  m
public void dereferenced(DereferencingResult result) {
    ExtendedDereferencingTask task = (ExtendedDereferencingTask) result.getTask();

    /* Add to cache - including header data for redirects */
    cacheController.addURLData(result.getURI(), result.getResultData(), result.getMethod());

    /* Handle known redirect */
    if (null != result.getMethod() && null != result.getMethod()
            .getStatusLine()) /* against NullPointerException with getStatusCode() */ {
        int resultCode = result.getMethod().getStatusCode();
        if (HttpStatusCodes.isRedirect(resultCode)) {
            Header locationHeader;
            if (null != (locationHeader = result.getMethod().getResponseHeader("location"))) {
                try {
                    loadURL(new URI(new URI(result.getURI(), true), locationHeader.getValue(), true),
                            task.getStep(), task.getRedirectStep() + 1, task.isForceReload());
                } catch (URIException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    task.setDone(true);

    /* Wake up parent */
    synchronized (this) {
        notify();
    }

    /* find new links */
    if (result.isSuccess())
        processLinks(task.getStep() + 1, new URIImpl(result.getURI()));
}

From source file:com.apatar.webdav.ui.JWebDavTreeModePanel.java

private HttpsURL getHttpUrl(String url) {
    HttpsURL httpUrl = null;/*from  www.  ja  va 2 s. com*/

    try {
        httpUrl = new HttpsURL(url);
        httpUrl.setUserinfo(log, pass);
    } catch (URIException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

    return httpUrl;
}

From source file:com.apatar.webdav.ui.JWebDavTreeModePanel.java

public void openWebDavConnect(String url, String login, String password, String innerUri) {

    log = login;/*  ww  w.  j a va2 s. c o  m*/
    pass = password;
    this.url = url;

    try {
        if (ApplicationData.httpClient.isUseProxy()) {
            String proxyUser = ApplicationData.httpClient.getUserName();
            if (proxyUser != null) {
                Credentials cred = new UsernamePasswordCredentials(proxyUser,
                        ApplicationData.httpClient.getPassword());
                webdav = new WebdavResource(getHttpUrl(this.url + innerUri),
                        ApplicationData.httpClient.getHost(), ApplicationData.httpClient.getPort(), cred, true);
            } else {
                webdav = new WebdavResource(getHttpUrl(this.url + innerUri),
                        ApplicationData.httpClient.getHost(), ApplicationData.httpClient.getPort(), true);
            }
        } else {
            webdav = new WebdavResource(getHttpUrl(this.url + innerUri), true);
        }
        currentres = webdav;
        resources = webdav.getChildResources();

        makeWebDavPath(null);

    } catch (URIException e) {
        e.printStackTrace();
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:de.fuberlin.wiwiss.marbles.loading.DereferencerBatch.java

/**
 * Identifies known links from loaded data and submits them to <code>{@link #loadURL(URI, int, int, boolean)}</code> 
 * @param step   Current step level/*from  w w w.ja v  a2  s.co m*/
 * @param contexts   Contexts that are to be considered to find links
 */
public void processLinks(int step, Resource... contexts) {
    if (step > maxSteps)
        return;

    RepositoryConnection conn = null;
    try {
        conn = cacheController.getDataRepository().getConnection();
        for (org.openrdf.model.URI predicate : Constants.interestingPredicates) {
            List<Statement> statementsList;
            RepositoryResult<Statement> statements = conn.getStatements(mainResource, predicate, null /* obj */,
                    true /* includeInferred */, contexts);
            statementsList = Iterations.addAll(statements, new ArrayList<Statement>());
            statements.close();

            /* Also include inverse properties */
            statements = conn.getStatements(null, predicate, mainResource, true /* includeInferred */,
                    contexts);
            Iterations.addAll(statements, statementsList);
            statements.close();

            List<URI> urlsToBeFetched = new ArrayList<URI>();

            for (Statement st : statementsList) {
                Value obj = (st.getSubject().equals(mainResource) ? st.getObject() : st.getSubject());
                if (obj instanceof org.openrdf.model.URI && !urlsToBeFetched.contains(obj.toString()))
                    try {
                        urlsToBeFetched.add(new URI(obj.toString(), true));
                    } catch (URIException e) {
                        e.printStackTrace();
                    } catch (NullPointerException e) {
                        e.printStackTrace();
                    }
            }

            /* Ask data providers */
            for (DataProvider p : dataProviders) {
                List<URI> newURLs = p.getURLsFromData(cacheController, conn, mainResource);
                if (newURLs != null)
                    urlsToBeFetched.addAll(newURLs);
            }

            /* Load URLs */
            for (URI url : urlsToBeFetched) {
                try {
                    loadURL(url, step, 0 /* redirectStep */, false);
                } catch (URIException e) {
                    e.printStackTrace();
                }
            }
        }
    } catch (RepositoryException e) {
        e.printStackTrace();
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (RepositoryException e) {
            e.printStackTrace();
        }
    }

}

From source file:grafix.basedados.Download.java

public int baixaArquivo() {
    // Create an instance of HttpClient.
    HttpClient client = new HttpClient();
    int retorno = 0;
    // Create a method instance.

    if (this.usaProxy) {
        client.getHostConfiguration().setProxy(this.servidorProxy, this.portaProxy);
        client.getState().setProxyCredentials(new AuthScope(this.servidorProxy, this.portaProxy),
                new UsernamePasswordCredentials(this.usuarioProxy, this.senhaProxy));
        client.getParams().setAuthenticationPreemptive(true);

    }//w  ww . java2  s  .com

    URI url2;
    String ff = null;
    try {
        url2 = new URI(this.getUrl(), false);
        ff = url2.toString();
    } catch (URIException ex) {
        ex.printStackTrace();
    } catch (NullPointerException ex) {
        ex.printStackTrace();
    }

    GetMethod method = new GetMethod(ff);
    byte[] arquivo = null;
    long totalBytesRead = 0;
    long loopBytesRead = 0;
    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    byte[] buffer = new byte[4096];
    int progresso = 0;
    try {
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusCode() + " " + method.getStatusLine());
            retorno = method.getStatusCode();
        } else {
            long contentLength = method.getResponseContentLength();
            File file = new File(this.getArquivo());
            FileOutputStream os = new FileOutputStream(file);
            InputStream stream = method.getResponseBodyAsStream();
            while ((loopBytesRead = stream.read(buffer)) != -1) {
                for (int i = 0; i < loopBytesRead; i++) {
                    os.write(buffer[i]);
                }
                totalBytesRead += loopBytesRead;
                progresso = (int) ((float) totalBytesRead / contentLength * 100);
                if (progresso >= 0 || progresso <= 100) {
                    //   System.out.println("download " + progresso + " %");
                    if (this.mostraProgresso)
                        formAtualizacao.definirPercentualProgresso(progresso);
                }
            }
            os.flush();
            os.close();
            stream.close();
        }
    } catch (HttpException e) {
        retorno = 2;
        System.err.println("Fatal protocol violation: " + e.getMessage());
        e.printStackTrace();

    } catch (IOException e) {
        retorno = 3;
        System.err.println("Fatal transport error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        // Release the connection.
        method.releaseConnection();
    }

    if (this.mostraProgresso)
        formAtualizacao.definirPercentualProgresso(0);
    return retorno;
}

From source file:dk.netarkivet.harvester.tools.TwitterDecidingScope.java

/**
 * Adds a url as a seed if possible. Otherwise just prints an error description and returns.
 *
 * @param tweetUrl The url to be added./* w  ww . j a  v  a  2s. c om*/
 */
private void addSeedIfLegal(String tweetUrl) {
    try {
        CandidateURI curi = CandidateURI.createSeedCandidateURI(UURIFactory.getInstance(tweetUrl));
        System.out.println("Adding seed: '" + curi.toString() + "'");
        addSeed(curi);
    } catch (URIException e1) {
        log.error(e1.getMessage());
        e1.printStackTrace();
    }
}

From source file:it.lilik.capturemjpeg.CaptureMJPEG.java

/**
 * Changes the URI.<br>/*  w ww  .  j  a v  a 2  s . c o  m*/
 * A new connection will be performed after a complete
 * image reading.
 * @param url the url of the MJPEG stream
 * 
 */
public void setURL(String url) {
    synchronized (this.method) {
        this.method.releaseConnection();
        try {
            this.method.setURI(new URI(url, false));
        } catch (URIException e) {
            e.printStackTrace();
        }
        this.isChangePending = true;
        this.method.setFollowRedirects(true);
    }
}