Example usage for java.net URL getHost

List of usage examples for java.net URL getHost

Introduction

In this page you can find the example usage for java.net URL getHost.

Prototype

public String getHost() 

Source Link

Document

Gets the host name of this URL , if applicable.

Usage

From source file:com.esri.geoportal.commons.robots.BotsParser.java

private URL getRobotsTxtUrl(URL baseUrl) {
    try {/*from w  w  w .j av  a 2 s.  c  om*/
        if (baseUrl != null) {
            if (baseUrl.getPort() >= 0) {
                return new URL(String.format("%s://%s:%d/robots.txt", baseUrl.getProtocol(), baseUrl.getHost(),
                        baseUrl.getPort()));
            } else {
                return new URL(String.format("%s://%s/robots.txt", baseUrl.getProtocol(), baseUrl.getHost()));
            }
        }
    } catch (MalformedURLException ex) {
        LOG.warn("Invalid robots.txt url.", ex);
    }
    return null;
}

From source file:io.github.bonigarcia.wdm.WdmHttpClient.java

Proxy createProxy(String proxyUrl) {
    URL url = determineProxyUrl(proxyUrl);
    if (url == null) {
        return null;
    }//from   ww w . j av  a2  s.c o  m
    String proxyHost = url.getHost();
    int proxyPort = url.getPort() == -1 ? 80 : url.getPort();
    return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
}

From source file:com.netflix.genie.client.security.oauth2.TokenFetcher.java

/**
 * Constructor.//  w w w.  j a  v a 2s .  c o  m
 *
 * @param oauthUrl     The url of the IDP from where to get the credentials.
 * @param clientId     The clientId to use to get the credentials.
 * @param clientSecret The clientSecret to use to get the credentials.
 * @param grantType    The type of the grant.
 * @param scope        The scope of the credentials returned.
 * @throws GenieClientException If there is any problem.
 */
public TokenFetcher(final String oauthUrl, final String clientId, final String clientSecret,
        final String grantType, final String scope) throws GenieClientException {

    log.debug("Constructor called.");

    if (StringUtils.isBlank(oauthUrl)) {
        throw new IllegalArgumentException("URL cannot be null or empty");
    }

    if (StringUtils.isBlank(clientId)) {
        throw new IllegalArgumentException("Client Id cannot be null or empty");
    }

    if (StringUtils.isBlank(clientSecret)) {
        throw new IllegalArgumentException("Client Secret cannot be null or empty");
    }

    if (StringUtils.isBlank(grantType)) {
        throw new IllegalArgumentException("Grant Type cannot be null or empty");
    }

    if (StringUtils.isBlank(scope)) {
        throw new IllegalArgumentException("Scope cannot be null or empty");
    }

    try {
        final URL url = new URL(oauthUrl);

        // Construct the Base path of the type http[s]://serverhost/ for retrofit to work.
        final String oAuthServerUrl = url.getProtocol() + "://" + url.getHost() + "/";
        final Retrofit retrofit = new Retrofit.Builder().baseUrl(oAuthServerUrl)
                .addConverterFactory(JacksonConverterFactory.create()).build();

        this.oauthUrl = oauthUrl;

        // Instantiate the token service
        tokenService = retrofit.create(TokenService.class);

        // Construct the fields map to send to the IDP url.
        credentialParams.put(CLIENT_ID, clientId);
        credentialParams.put(CLIENT_SECRET, clientSecret);
        credentialParams.put(GRANT_TYPE, grantType);
        credentialParams.put(SCOPE, scope);
    } catch (Exception e) {
        throw new GenieClientException("Could not instantiate Token Service due to exception " + e);
    }
}

From source file:com.predic8.membrane.core.interceptor.WSDLInterceptor.java

private Exchange createExchange(String uri) throws MalformedURLException {
    URL url = new URL(uri);
    Request req = MessageUtil.getGetRequest(getCompletePath(url));
    req.getHeader().setHost(url.getHost());
    Exchange exc = new Exchange(null);
    exc.setRequest(req);/*from   w  w  w .j  a  v a2 s.  c  o  m*/
    exc.getDestinations().add(uri);
    return exc;
}

From source file:wuit.crawler.CrawlerHtml.java

private void getUrlInfo(DSCrawlerUrl info) {
    try {// w  ww  .ja va2s. c o  m
        //            System.out.println("getUrlInfo : " + info.url);

        URL _url = new URL(info.url);
        info.dns = _url.getHost() + "";
        info.path = _url.getPath();
        info.file = _url.getProtocol();
        if (!info.url.equals("") && info.url != null) {
            InetAddress a = InetAddress.getByName(_url.getHost());
            if (a != null)
                info.IP = a.getHostAddress();
        }
        /*        } catch (MalformedURLException ex) {
                    Logger.getLogger(CrawlerHtml.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnknownHostException ex) {
                    Logger.getLogger(CrawlerHtml.class.getName()).log(Level.SEVERE, null, ex);*/
    } catch (Exception e) {
        System.out.println(" crawlerHtml   " + e.getMessage());
    }
}

From source file:at.gv.egiz.pdfas.web.servlets.ErrorPage.java

protected void process(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    if (PdfAsHelper.getFromDataUrl(request)) {
        // redirect to here!
        response.sendRedirect(PdfAsHelper.generateErrorURL(request, response));
        return;/*  www .j a  v  a 2s  .  c  o  m*/
    } else {
        String errorURL = PdfAsHelper.getErrorURL(request, response);
        Throwable e = PdfAsHelper.getSessionException(request, response);

        StatisticEvent statisticEvent = PdfAsHelper.getStatisticEvent(request, response);
        if (statisticEvent != null) {
            if (!statisticEvent.isLogged()) {
                statisticEvent.setStatus(Status.ERROR);
                statisticEvent.setException(e);
                if (e instanceof PDFASError) {
                    statisticEvent.setErrorCode(((PDFASError) e).getCode());
                }
                statisticEvent.setEndNow();
                statisticEvent.setTimestampNow();
                StatisticFrontend.getInstance().storeEvent(statisticEvent);
                statisticEvent.setLogged(true);
            }
        }

        String message = PdfAsHelper.getSessionErrMessage(request, response);
        if (errorURL != null && WebConfiguration.isProvidePdfURLinWhitelist(errorURL)) {
            String template = PdfAsHelper.getErrorRedirectTemplateSL();

            URL url = new URL(errorURL);
            String errorURLProcessed = url.getProtocol() + "://" + // "http" + "://
                    url.getHost() + // "myhost"
                    ":" + // ":"
                    url.getPort() + // "8080"
                    url.getPath();

            template = template.replace("##ERROR_URL##", errorURLProcessed);

            String extraParams = UrlParameterExtractor.buildParameterFormString(url);
            template = template.replace("##ADD_PARAMS##", extraParams);

            String target = PdfAsHelper.getInvokeTarget(request, response);

            if (target == null) {
                target = "_self";
            }

            template = template.replace("##TARGET##", StringEscapeUtils.escapeHtml4(target));

            if (e != null && WebConfiguration.isShowErrorDetails()) {
                template = template.replace("##CAUSE##", URLEncoder.encode(e.getMessage(), "UTF-8"));
            } else {
                template = template.replace("##CAUSE##", "");
            }
            if (message != null) {
                template = template.replace("##ERROR##", URLEncoder.encode(message, "UTF-8"));
            } else {
                template = template.replace("##ERROR##", "Unbekannter Fehler");
            }
            response.setContentType("text/html");
            response.getWriter().write(template);
            response.getWriter().close();
        } else {
            if (errorURL != null) {
                logger.warn(errorURL + " is not allowed by whitelist");
            }

            String template = PdfAsHelper.getErrorTemplate();
            if (message != null) {
                template = template.replace(ERROR_MESSAGE, message);
            } else {
                template = template.replace(ERROR_MESSAGE, "Unbekannter Fehler");
            }

            if (e != null && WebConfiguration.isShowErrorDetails()) {
                template = template.replace(ERROR_STACK, HTMLFormater.formatStackTrace(e.getStackTrace()));
            } else {
                template = template.replace(ERROR_STACK, "");
            }

            response.setContentType("text/html");
            response.getWriter().write(template);
            response.getWriter().close();
        }
    }
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.worker.DedicatedWorkerGlobalScope.java

/**
 * Constructor./*from  w ww.j  a  va  2  s. com*/
 * @param browserVersion the simulated browser version
 * @param worker the started worker
 * @throws Exception in case of problem
 */
DedicatedWorkerGlobalScope(final Window owningWindow, final Context context,
        final BrowserVersion browserVersion, final Worker worker) throws Exception {
    context.initStandardObjects(this);

    final ClassConfiguration config = AbstractJavaScriptConfiguration
            .getClassConfiguration(DedicatedWorkerGlobalScope.class, browserVersion);
    final HtmlUnitScriptable prototype = JavaScriptEngine.configureClass(config, null, browserVersion);
    setPrototype(prototype);

    owningWindow_ = owningWindow;
    final URL currentURL = owningWindow.getWebWindow().getEnclosedPage().getUrl();
    origin_ = currentURL.getProtocol() + "://" + currentURL.getHost() + ':' + currentURL.getPort();

    worker_ = worker;
}

From source file:com.searchbox.framework.web.FavoriteController.java

@RequestMapping(value = "/favorite/downloadCSV")
public void downloadCSV(@RequestParam String sort, @Qualifier("favoriteTable") Pageable page,
        HttpServletResponse response, HttpServletRequest request) throws IOException {
    String csvFileName = "favorites.csv";
    response.setContentType("text/csv");
    // creates mock data
    String headerKey = "Content-Disposition";
    String headerValue = String.format("attachment; filename=\"%s\"", csvFileName);
    response.setHeader(headerKey, headerValue);
    List<Favorite> list = service.findAllCount(sort, new PageRequest(0, Integer.MAX_VALUE));
    String result = "Id, Title, Url, Time \n";
    for (Favorite f : list) {
        String[] row = new String[4];
        row[0] = f.getFavoriteId();//from   www. j  av a 2 s . c om
        row[1] = f.getTitle();
        URL url = new URL(request.getRequestURL().toString());
        row[2] = url.getHost() + "/oppfin/all/view/?ff=" + f.getIdField() + "[" + f.getFavoriteId() + "]";
        row[3] = f.getTime().toString();
        result = result + CSVUtils.printLine(row, CSVStrategy.EXCEL_STRATEGY) + "\n";
    }
    response.getWriter().print(result);
}

From source file:com.testmax.util.FileDownLoader.java

public String downloader(WebElement element, String attribute) throws Exception {
    //Assuming that getAttribute does some magic to return a fully qualified URL
    String downloadLocation = element.getAttribute(attribute);
    if (downloadLocation.trim().equals("")) {
        throw new Exception("The element you have specified does not link to anything!");
    }// ww  w.  j  a v  a  2s.  c o m
    URL downloadURL = new URL(downloadLocation);
    HttpClient client = new HttpClient();
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    client.setHostConfiguration(mimicHostConfiguration(downloadURL.getHost(), downloadURL.getPort()));
    client.setState(mimicCookieState(driver.manage().getCookies()));
    HttpMethod getRequest = new GetMethod(downloadURL.getPath());
    String file_path = downloadPath + downloadURL.getFile().replaceFirst("/|\\\\", "");
    FileWriter downloadedFile = new FileWriter(file_path, true);
    try {
        int status = client.executeMethod(getRequest);
        WmLog.getCoreLogger().info("HTTP Status {} when getting '{}'" + status + downloadURL.toExternalForm());
        BufferedInputStream in = new BufferedInputStream(getRequest.getResponseBodyAsStream());
        int offset = 0;
        int len = 4096;
        int bytes = 0;
        byte[] block = new byte[len];
        while ((bytes = in.read(block, offset, len)) > -1) {
            downloadedFile.write(bytes);

        }
        downloadedFile.close();
        in.close();
        WmLog.getCoreLogger().info("File downloaded to '{}'" + file_path);
    } catch (Exception Ex) {
        WmLog.getCoreLogger().error("Download failed: {}", Ex);
        throw new Exception("Download failed!");
    } finally {
        getRequest.releaseConnection();
    }
    return file_path;
}

From source file:net.siegmar.japtproxy.fetcher.HttpClientConfigurer.java

protected void configureProxy(final HttpClientBuilder httpClientBuilder, final String proxy)
        throws InitializationException {
    final URL proxyUrl;
    try {//w w w. j av a  2  s .  c o  m
        proxyUrl = new URL(proxy);
    } catch (final MalformedURLException e) {
        throw new InitializationException("Invalid proxy url", e);
    }

    final String proxyHost = proxyUrl.getHost();
    final int proxyPort = proxyUrl.getPort() != -1 ? proxyUrl.getPort() : proxyUrl.getDefaultPort();

    LOG.info("Set proxy server to '{}:{}'", proxyHost, proxyPort);
    httpClientBuilder.setRoutePlanner(new DefaultProxyRoutePlanner(new HttpHost(proxyHost, proxyPort)));

    final String userInfo = proxyUrl.getUserInfo();
    if (userInfo != null) {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), buildCredentials(userInfo));

        httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    }
}