Example usage for java.net URI getAuthority

List of usage examples for java.net URI getAuthority

Introduction

In this page you can find the example usage for java.net URI getAuthority.

Prototype

public String getAuthority() 

Source Link

Document

Returns the decoded authority component of this URI.

Usage

From source file:org.apache.hadoop.fs.swift.block.SwiftBlockFileSystem.java

@Override
public BlockLocation[] getFileBlockLocations(FileStatus file, long start, long len) throws IOException {
    final INode iNode = store.retrieveINode(file.getPath());
    final BlockLocation[] blockLocations = new BlockLocation[iNode.getBlocks().length];

    int idx = 0;//from  w  w  w  .jav  a 2  s. com
    long offset = 0l;
    for (Block block : iNode.getBlocks()) {
        final List<URI> locations = store.getObjectLocation(
                new Path(new SwiftObjectPath(uri.getHost(), String.valueOf(block.getId())).toString()));
        final String[] names = new String[locations.size()];
        final String[] hosts = new String[locations.size()];
        int i = 0;
        for (URI uri : locations) {
            hosts[i] = uri.getHost();
            names[i] = uri.getAuthority();
            i++;
        }
        blockLocations[idx++] = new BlockLocation(names, hosts, offset, block.getLength());
        offset += block.getLength();
        LOG.debug("block location: " + Arrays.toString(names) + " hosts  " + Arrays.toString(hosts)
                + " : length: " + block.getLength());
    }

    return blockLocations;
}

From source file:org.semantictools.frame.api.OntologyManager.java

private File repoDir(String assetURI) {
    try {//from  w w w  .  ja  va  2 s . co  m
        URI uri = new URI(assetURI);
        String path = uri.getAuthority() + "/" + uri.getPath();

        return new File(localRepository, path);
    } catch (Throwable oops) {
        return null;
    }
}

From source file:org.apache.hadoop.hive.ql.parse.LoadSemanticAnalyzer.java

private URI initializeFromURI(String fromPath, boolean isLocal) throws IOException, URISyntaxException {
    URI fromURI = new Path(fromPath).toUri();

    String fromScheme = fromURI.getScheme();
    String fromAuthority = fromURI.getAuthority();
    String path = fromURI.getPath();

    // generate absolute path relative to current directory or hdfs home
    // directory//from ww  w  .  j  av  a2s . c  o  m
    if (!path.startsWith("/")) {
        if (isLocal) {
            path = URIUtil.decode(new Path(System.getProperty("user.dir"), fromPath).toUri().toString());
        } else {
            path = new Path(new Path("/user/" + System.getProperty("user.name")), path).toString();
        }
    }

    // set correct scheme and authority
    if (StringUtils.isEmpty(fromScheme)) {
        if (isLocal) {
            // file for local
            fromScheme = "file";
        } else {
            // use default values from fs.default.name
            URI defaultURI = FileSystem.get(conf).getUri();
            fromScheme = defaultURI.getScheme();
            fromAuthority = defaultURI.getAuthority();
        }
    }

    // if scheme is specified but not authority then use the default authority
    if ((!fromScheme.equals("file")) && StringUtils.isEmpty(fromAuthority)) {
        URI defaultURI = FileSystem.get(conf).getUri();
        fromAuthority = defaultURI.getAuthority();
    }

    LOG.debug(fromScheme + "@" + fromAuthority + "@" + path);
    return new URI(fromScheme, fromAuthority, path, null, null);
}

From source file:com.bah.applefox.main.plugins.webcrawler.utilities.WebPageCrawl.java

/**
 * Constructor for the WebPageParser object. Runs the page crawl when it is
 * invoked./*  w w  w  . j a va  2 s.c om*/
 * 
 * @param parentURL
 *            - the URL of the page to be parsed
 * @param UserAgent
 *            - the UserAgent of the server request property
 * @throws PageCrawlException
 *             If an error occurs crawling the page.
 */
public WebPageCrawl(String parentURL, String UserAgent, Set<String> authorityLimits) throws PageCrawlException {
    // get the url, check if we can crawl this page.
    URL url;
    try {
        url = new URL(parentURL);
    } catch (MalformedURLException e) {
        throw new PageCrawlException(e);
    }
    robotsTXT = RobotsTXT.get(url, UserAgent);
    this.UserAgent = UserAgent;
    try {
        this.parentURL = url.toURI();
    } catch (URISyntaxException e) {
        throw new PageCrawlException(e);
    }

    if (robotsTXT.allowed(parentURL)) {
        URLConnection con;
        try {
            con = url.openConnection();
        } catch (MalformedURLException e) {
            throw new PageCrawlException(e);
        } catch (IOException e) {
            throw new PageCrawlException(e);
        }
        con.setRequestProperty("User-Agent", UserAgent);
        InputStream stream = null;
        try {
            try {
                stream = con.getInputStream();
            } catch (IOException e) {
                throw new PageCrawlException(e);
            }
            Parser parser = new AutoDetectParser();
            ParagraphContentHandler paragraphGetter = new ParagraphContentHandler();
            BodyContentHandler bodyHandler = new BodyContentHandler(paragraphGetter);
            // link handlers also do image tags
            LinkContentHandler linkHandler = new LinkContentHandler();
            ContentHandler overallHandler = new TeeContentHandler(bodyHandler, linkHandler);
            Metadata metadata = new Metadata();
            ParseContext context = new ParseContext();
            try {
                parser.parse(stream, overallHandler, metadata, context);
            } catch (IOException e) {
                throw new PageCrawlException(e);
            } catch (SAXException e) {
                throw new PageCrawlException(e);
            } catch (TikaException e) {
                throw new PageCrawlException(e);
            }
            // WE FINALLY GET THE DATA!
            String docTitle = metadata.get("title");
            this.title = docTitle != null ? docTitle : "";
            pageContents = paragraphGetter.getParagraphs();
            List<String> images = new ArrayList<String>();
            List<String> links = new ArrayList<String>();
            for (Link link : linkHandler.getLinks()) {
                URI linkURL = this.parentURL.resolve(link.getUri());
                if (authorityLimits.size() > 0 && !authorityLimits.contains(linkURL.getAuthority())) {
                    continue;
                }
                String protocol = linkURL.getScheme();
                if (!protocol.equals("http") && !protocol.equals("https"))
                    continue;
                if (link.isImage()) {
                    images.add(linkURL.toString());
                }
                if (link.isAnchor()) {
                    links.add(linkURL.toString());
                }
            }
            allImages = Collections.unmodifiableList(images);
            allLinks = Collections.unmodifiableList(links);
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    throw new PageCrawlException(e);
                }
            }
        }
    } else {
        title = "";
        pageContents = Collections.emptyList();
        allImages = Collections.emptyList();
        allLinks = Collections.emptyList();
    }

}

From source file:org.apache.amber.signature.AbstractMethod.java

/**
 * Calculates the OAuth base string./* w  w w.  j a v  a2s . com*/
 *
 * @param request
 * @return the calculated OAuth base string.
 * @throws SignatureException if any error occurs.
 */
private String createBaseString(OAuthRequest request) throws SignatureException {
    // the HTTP method
    String method = request.getHTTPMethod().name();

    // the normalized request URL
    URI url = request.getRequestURL();
    String scheme = url.getScheme().toLowerCase();
    String authority = url.getAuthority().toLowerCase();

    int port = url.getPort();
    if ((HTTP_PROTOCOL.equals(scheme) && DEFAULT_HTTP_PORT == port)
            || (HTTPS_PROTOCOL.equals(scheme) && DEFAULT_HTTPS_PORT == port)) {
        int index = authority.lastIndexOf(':');
        if (index >= 0) {
            authority = authority.substring(0, index);
        }
    }

    String path = url.getPath();
    if (path == null || path.length() <= 0) {
        path = PATH_SEPARATOR; // conforms to RFC 2616 section 3.2.2
    }

    String requestUrl = new StringBuilder(scheme).append(SCHEME_SEPARATOR).append(authority).append(path)
            .toString();

    // parameters normalization
    SortedSet<Entry<String, String>> normalizedParameters = new TreeSet<Entry<String, String>>();

    for (OAuthMessageParameter parameter : request.getOAuthMessageParameters()) {
        if (parameter.getKey().isIncludeInSignature()) {
            encodeAndAddParameter(parameter.getKey().getLabel(), parameter.getValue(), normalizedParameters);
        }
    }

    for (OAuthRequestParameter parameter : request.getOAuthRequestParameters()) {
        if (request.getOAuthMessageParameters().contains(parameter)) {
            throw new SignatureException(
                    "Request parameter " + parameter + " can't override an OAuth message one");
        }
        encodeAndAddParameter(parameter.getKey(), parameter.getValue(), normalizedParameters);
    }

    // now serialize the normalized parameters
    StringBuilder normalizedParametersBuffer = new StringBuilder();
    int counter = 0;
    for (Entry<String, String> parameter : normalizedParameters) {
        if (counter > 0) {
            normalizedParametersBuffer.append('&');
        }

        normalizedParametersBuffer.append(parameter.getKey());
        normalizedParametersBuffer.append('=');
        normalizedParametersBuffer.append(parameter.getValue());
        counter++;
    }

    return new StringBuilder(method).append('&').append(percentEncode(requestUrl)).append('&')
            .append(percentEncode(normalizedParametersBuffer.toString())).toString();
}

From source file:org.talend.mdm.service.calljob.CallJobServiceBean.java

public String receiveFromInbound(ItemPOJOPK itemPK, String routingOrderID, String compiledParameters)
        throws XtentisException {
    JobInvokeConfig jobInvokeConfig = null;
    CompiledParameters parameters;/* w  w w  .j  a  va  2 s .  c  om*/
    try {
        parameters = CompiledParameters.deserialize(compiledParameters);
    } catch (Exception e) {
        LOGGER.error("Could not read parameters", e);
        throw new XtentisException(e);
    }
    try {
        // set the parameters
        URI uri = URI.create(parameters.getUrl());
        String protocol = uri.getScheme();
        String jobName = uri.getHost();
        if (jobName == null) {
            jobName = uri.getAuthority();
        }
        String jobVersion = uri.getPath().substring(1);
        if (LTJ_PROTOCOL.equals(protocol)) {
            jobInvokeConfig = new JobInvokeConfig();
            jobInvokeConfig.setJobName(jobName);
            jobInvokeConfig.setJobVersion(jobVersion);
        } else if (HTTP_PROTOCOL.equalsIgnoreCase(protocol)) {
            throw new NotImplementedException(); // TODO Too much dependency on JBoss class (to rewrite)
        } else {
            throw new IllegalArgumentException("Protocol '" + protocol + "' is not supported.");
        }
        // the text should be a map(key=value)
        String exchangeXML = StringUtils.EMPTY;
        Properties p = new Properties();
        if (parameters.getTisContext() != null) {
            for (ContextParam kv : parameters.getTisContext()) {
                String value = kv.getValue();
                if (kv.isItemXML()) {
                    value = createExchangeXML(itemPK);
                    // TMDM-2633 Always include exchange XML message in parameters (save computed value for later)
                    exchangeXML = value;
                }
                p.setProperty(kv.getName(), value);
            }
        }
        Map<String, String> argsMap = new HashMap<String, String>();
        for (Object o : p.keySet()) {
            String key = (String) o;
            String value = p.getProperty(key);
            argsMap.put(key, value);
        }
        // TMDM-2633: Always include exchange XML message in parameters
        if (exchangeXML.isEmpty()) { // (don't compute it twice)
            exchangeXML = createExchangeXML(itemPK);
        }
        argsMap.put(MDMJobInvoker.EXCHANGE_XML_PARAMETER, exchangeXML);
        JobContainer.getUniqueInstance().getJobInvoker(jobName, jobVersion).call(argsMap);
        return "callJob Service successfully executed!'";
    } catch (XtentisException xe) {
        throw xe;
    } catch (Exception e) {
        String err;
        if (jobInvokeConfig != null) {
            err = "Could not execute callJob service for job " + jobInvokeConfig.getJobName() + " in version " //$NON-NLS-1$//$NON-NLS-2$
                    + jobInvokeConfig.getJobVersion();
        } else {
            err = "Could not execute callJob service for service " + parameters.getUrl(); //$NON-NLS-1$
        }
        LOGGER.error(err, e);
        throw new XtentisException(e);
    }
}

From source file:org.apache.hadoop.hive.metastore.MultiHdfsInfo.java

public boolean processCtas(String queryTmpdir, String deststr, String dbname, ArrayList<String> dirList)
        throws MetaException {
    LOG.debug(this.queryid + " process ctas");
    LOG.debug(this.queryid + " queryTmpdir:" + queryTmpdir);
    LOG.debug(this.queryid + " deststr:" + deststr);
    LOG.debug(this.queryid + " dbname:" + dbname);
    String nnscheme = getHdfsPathFromDB(dbname);
    URI destURI = new Path(deststr).toUri();
    URI nnURI = new Path(nnscheme).toUri();

    if (destURI.getScheme().equalsIgnoreCase(nnURI.getScheme())
            && destURI.getAuthority().equalsIgnoreCase(nnURI.getAuthority())) {
        LOG.info(this.queryid + " no need to change");
        return false;
    }/* w  w  w.  j  av  a 2  s  .c om*/

    processDir(queryTmpdir, deststr, nnURI, dirList);
    return true;
}

From source file:eu.stratosphere.runtime.fs.hdfs.DistributedFileSystem.java

@Override
public void initialize(URI path) throws IOException {
    // For HDFS we have to have an authority
    if (path.getAuthority() == null) {

        String configEntry = this.conf.get("fs.default.name", null);
        if (configEntry == null) {
            // fs.default.name deprecated as of hadoop 2.2.0 http://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/DeprecatedProperties.html
            configEntry = this.conf.get("fs.defaultFS", null);
        }//from  w  ww.  j a  va  2 s .  c  o  m

        if (LOG.isDebugEnabled()) {
            LOG.debug("fs.defaultFS is set to " + configEntry);
        }

        if (configEntry == null) {
            throw new IOException(getMissingAuthorityErrorPrefix(path)
                    + "Either no default hdfs configuration was registered, "
                    + "or that configuration did not contain an entry for the default hdfs.");
        } else {
            try {
                URI initURI = URI.create(configEntry);

                if (initURI.getAuthority() == null) {
                    throw new IOException(getMissingAuthorityErrorPrefix(path)
                            + "Either no default hdfs configuration was registered, "
                            + "or the provided configuration contains no valid hdfs namenode address (fs.default.name or fs.defaultFS) describing the hdfs namenode host and port.");
                } else if (!initURI.getScheme().equalsIgnoreCase("hdfs")) {
                    throw new IOException(getMissingAuthorityErrorPrefix(path)
                            + "Either no default hdfs configuration was registered, "
                            + "or the provided configuration describes a file system with scheme '"
                            + initURI.getScheme() + "' other than the Hadoop Distributed File System (HDFS).");
                } else {
                    try {
                        this.fs.initialize(initURI, this.conf);
                    } catch (Exception e) {
                        throw new IOException(getMissingAuthorityErrorPrefix(path)
                                + "Could not initialize the file system connection with the given address of the HDFS Namenode"
                                + e.getMessage() != null ? ": " + e.getMessage() : ".", e);
                    }
                }
            } catch (IllegalArgumentException e) {
                throw new IOException(getMissingAuthorityErrorPrefix(path)
                        + "The configuration contains an invalid hdfs default name (fs.default.name or fs.defaultFS): "
                        + configEntry);
            }
        }
    } else {
        // Initialize HDFS
        try {
            this.fs.initialize(path, this.conf);
        } catch (Exception e) {
            throw new IOException("The given file URI (" + path.toString()
                    + ") described the host and port of an HDFS Namenode, but the File System could not be initialized with that address"
                    + (e.getMessage() != null ? ": " + e.getMessage() : "."), e);
        }
    }
}

From source file:eionet.gdem.utils.InputFile.java

/**
 * Extracts filename from URI's path [scheme:][//authority][path][?query][#fragment].
 *
 * @param uri URI of input file.//w  w  w .  java 2s.c o m
 */
private void parseUri(URI uri) {

    this.strHostName = uri.getScheme() + "://" + uri.getAuthority();
    findFileName(uri.getPath());
}

From source file:org.wso2.carbon.apimgt.gateway.handlers.WebsocketInboundHandler.java

@SuppressWarnings("unchecked")
@Override/*  w  w  w  .  j a va 2  s. c  o  m*/
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    //check if the request is a handshake
    if (msg instanceof FullHttpRequest) {
        FullHttpRequest req = (FullHttpRequest) msg;
        uri = req.getUri();
        URI uriTemp = new URI(uri);
        apiContextUri = new URI(uriTemp.getScheme(), uriTemp.getAuthority(), uriTemp.getPath(), null,
                uriTemp.getFragment()).toString();

        if (req.getUri().contains("/t/")) {
            tenantDomain = MultitenantUtils.getTenantDomainFromUrl(req.getUri());
        } else {
            tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
        }

        String useragent = req.headers().get(HttpHeaders.USER_AGENT);
        String authorization = req.headers().get(HttpHeaders.AUTHORIZATION);

        // '-' is used for empty values to avoid possible errors in DAS side.
        // Required headers are stored one by one as validateOAuthHeader()
        // removes some of the headers from the request
        useragent = useragent != null ? useragent : "-";
        headers.add(HttpHeaders.AUTHORIZATION, authorization);
        headers.add(HttpHeaders.USER_AGENT, useragent);

        if (validateOAuthHeader(req)) {
            if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
                // carbon-mediation only support websocket invocation from super tenant APIs.
                // This is a workaround to mimic the the invocation came from super tenant.
                req.setUri(req.getUri().replaceFirst("/", "-"));
                String modifiedUri = uri.replaceFirst("/t/", "-t/");
                req.setUri(modifiedUri);
                msg = req;
            } else {
                req.setUri(uri); // Setting endpoint appended uri
            }

            if (StringUtils.isNotEmpty(token)) {
                ((FullHttpRequest) msg).headers().set(APIMgtGatewayConstants.WS_JWT_TOKEN_HEADER, token);
            }
            ctx.fireChannelRead(msg);

            // publish google analytics data
            GoogleAnalyticsData.DataBuilder gaData = new GoogleAnalyticsData.DataBuilder(null, null, null, null)
                    .setDocumentPath(uri).setDocumentHostName(DataPublisherUtil.getHostAddress())
                    .setSessionControl("end").setCacheBuster(APIMgtGoogleAnalyticsUtils.getCacheBusterId())
                    .setIPOverride(ctx.channel().remoteAddress().toString());
            APIMgtGoogleAnalyticsUtils gaUtils = new APIMgtGoogleAnalyticsUtils();
            gaUtils.init(tenantDomain);
            gaUtils.publishGATrackingData(gaData, req.headers().get(HttpHeaders.USER_AGENT), authorization);
        } else {
            ctx.writeAndFlush(
                    new TextWebSocketFrame(APISecurityConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE));
            throw new APISecurityException(APISecurityConstants.API_AUTH_INVALID_CREDENTIALS,
                    APISecurityConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE);
        }
    } else if (msg instanceof WebSocketFrame) {
        boolean isThrottledOut = doThrottle(ctx, (WebSocketFrame) msg);
        String clientIp = getRemoteIP(ctx);

        if (isThrottledOut) {
            ctx.fireChannelRead(msg);
        } else {
            ctx.writeAndFlush(new TextWebSocketFrame("Websocket frame throttled out"));
        }

        // publish analytics events if analytics is enabled
        if (APIUtil.isAnalyticsEnabled()) {
            publishRequestEvent(infoDTO, clientIp, isThrottledOut);
        }
    }
}