Example usage for java.net URL getProtocol

List of usage examples for java.net URL getProtocol

Introduction

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

Prototype

public String getProtocol() 

Source Link

Document

Gets the protocol name of this URL .

Usage

From source file:edu.sampleu.admin.EdocLiteXmlIngesterBase.java

protected String[] getResourceListing(Class clazz, String pathStartsWith) throws Exception {
    String classPath = clazz.getName().replace(".", "/") + ".class";
    URL dirUrl = clazz.getClassLoader().getResource(classPath);

    if (!"jar".equals(dirUrl.getProtocol())) {
        throw new UnsupportedOperationException("Cannot list files for URL " + dirUrl);
    }// w  w w.  ja v  a2s. c om

    String jarPath = dirUrl.getPath().substring(5, dirUrl.getPath().indexOf("!")); //strip out only the JAR file
    JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
    Enumeration<JarEntry> entries = jar.entries();
    Set<String> result = new HashSet<String>();

    while (entries.hasMoreElements()) {
        String entry = entries.nextElement().getName();
        if (entry.startsWith(pathStartsWith) && !entry.endsWith("/")) { //filter according to the pathStartsWith skipping directories
            result.add(entry);
        }
    }

    return result.toArray(new String[result.size()]);
}

From source file:com.googlesource.gerrit.plugins.github.oauth.OAuthGitFilter.java

private int getPort(URL originalUrl) {
    String protocol = originalUrl.getProtocol().toLowerCase();
    int port = originalUrl.getPort();
    if (port == -1) {
        return protocol.equals("https") ? 443 : 80;
    } else {//from   w ww .  ja v a  2 s .  com
        return port;
    }
}

From source file:org.deegree.protocol.ows.http.OwsHttpClientImpl.java

private void setProxies(URL url, DefaultHttpClient client) {
    String host = url.getHost();//w  w w  . ja v  a  2s  . c  om
    String protocol = url.getProtocol().toLowerCase();
    handleProxies(protocol, client, host);
}

From source file:org.geomajas.layer.common.proxy.LayerHttpServiceImpl.java

/**
 * Get the port out of a full URL.// w  w  w  .j  ava  2  s .  c o  m
 * <p>
 * Note that we only take https & http into account if you are using a non-default port/protocol you will need to
 * add the port to your baseUrl.
 * 
 * @param url base url
 * @return domain name
 */
private int parsePort(String url) {
    try {
        URL u = new URL(url);
        int defaultport = "https".equalsIgnoreCase(u.getProtocol()) ? URL_DEFAULT_SECURE_PORT
                : URL_DEFAULT_PORT;
        return (u.getPort() == -1 ? defaultport : u.getPort());
    } catch (MalformedURLException e) {
        return URL_DEFAULT_SECURE_PORT;
    }
}

From source file:fr.gael.dhus.server.http.SolrWebapp.java

@Override
public void configure(String dest_folder) throws IOException {
    String configurationFolder = "fr/gael/dhus/server/http/solr/webapp";
    URL u = Thread.currentThread().getContextClassLoader().getResource(configurationFolder);
    if (u != null && "jar".equals(u.getProtocol())) {
        extractJarFolder(u, configurationFolder, dest_folder);
    } else if (u != null) {
        File webAppFolder = new File(dest_folder);
        copyFolder(new File(u.getFile()), webAppFolder);
    }/*from  w  w  w  .  jav a 2s .  c  om*/
}

From source file:org.dataconservancy.access.connector.MultiThreadedConnectorTest.java

@Override
protected DcsConnectorConfig getConnectorConfig() {
    final DcsConnectorConfig config = new DcsConnectorConfig();
    try {//from ww w  .  j  a  v a 2 s .  c  om
        URL u = new URL(
                String.format(accessServiceUrl, testServer.getServiceHostName(), testServer.getServicePort()));
        config.setScheme(u.getProtocol());
        config.setHost(u.getHost());
        config.setPort(u.getPort());
        config.setContextPath(u.getPath());
    } catch (MalformedURLException e) {
        fail("Malformed DCS access http url: " + e.getMessage());
    }
    config.setMaxOpenConn(2);
    return config;
}

From source file:org.ebayopensource.twin.TwinConnection.java

public TwinConnection(URL url) {
    if (url.getPath().endsWith("/"))
        try {//from  w w  w  .jav a2  s  .  c  om
            url = new URL(url.getProtocol(), url.getHost(), url.getPort(),
                    url.getPath().substring(0, url.getPath().length() - 1));
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    this.url = url;
}

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;//from w  w w.ja v a 2  s .c  om
    } 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.cladonia.xngreditor.URLUtilities.java

/**
 * Convert the given url to a location relative to the current machine, ie: 
 * file: is missing.// www. jav  a 2s .  c o  m
 * 
 * @param url the url string to be converted.
 * 
 * @return a new file or null if the string does not describe a file.
 */
public static String toRelativeString(URL url) {
    String location = null;

    if (url != null) {
        location = url.toString();

        if (url.getProtocol().equals("file")) {
            location = location.substring(6, location.length());
        }
    }

    return location;
}

From source file:org.wso2.carbon.device.mgt.iot.common.apimgt.TokenClient.java

private AccessTokenInfo getTokenInfo(List<NameValuePair> nameValuePairs) throws AccessTokenException {

    try {/*from ww  w .j a va  2s  . c  om*/
        URL tokenUrl = new URL(tokenURL);
        HttpClient httpClient = null;
        try {
            httpClient = IoTUtil.getHttpClient(tokenUrl.getPort(), tokenUrl.getProtocol());
        } catch (Exception e) {
            String msg = "Error on getting a http client for port :" + tokenUrl.getPort() + " protocol :"
                    + tokenUrl.getProtocol();
            log.error(msg);
            throw new AccessTokenException(msg);
        }
        HttpPost postMethod = new HttpPost(tokenURL);
        postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        if (appToken != null) {
            postMethod.addHeader("Authorization", "Basic " + appToken);
        }
        postMethod.addHeader("Content-Type", "application/x-www-form-urlencoded");

        HttpResponse httpResponse = httpClient.execute(postMethod);
        String response = IoTUtil.getResponseString(httpResponse);
        if (log.isDebugEnabled()) {
            log.debug(response);
        }
        JSONObject jsonObject = new JSONObject(response);

        AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
        accessTokenInfo.setAccess_token(jsonObject.getString("access_token"));
        accessTokenInfo.setRefresh_token(jsonObject.getString("refresh_token"));
        accessTokenInfo.setExpires_in(jsonObject.getInt("expires_in"));
        accessTokenInfo.setToken_type(jsonObject.getString("token_type"));
        return accessTokenInfo;

    } catch (IOException | JSONException | IoTException e) {
        log.error(e.getMessage());
        throw new AccessTokenException("Configuration Error for Access Token Generation");
    }

}