Example usage for org.apache.commons.lang3 StringUtils substringAfter

List of usage examples for org.apache.commons.lang3 StringUtils substringAfter

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils substringAfter.

Prototype

public static String substringAfter(final String str, final String separator) 

Source Link

Document

Gets the substring after the first occurrence of a separator.

Usage

From source file:org.apache.nifi.authorization.StandardAuthorizableLookup.java

@Override
public Authorizable getAuthorizableFromResource(String resource) {
    // parse the resource type
    ResourceType resourceType = null;/*from   w  w w.ja v a  2  s  . c om*/
    for (ResourceType type : ResourceType.values()) {
        if (resource.equals(type.getValue()) || resource.startsWith(type.getValue() + "/")) {
            resourceType = type;
        }
    }

    if (resourceType == null) {
        throw new ResourceNotFoundException("Unrecognized resource: " + resource);
    }

    // if this is a policy or a provenance event resource, there should be another resource type
    if (ResourceType.Policy.equals(resourceType) || ResourceType.Data.equals(resourceType)
            || ResourceType.DataTransfer.equals(resourceType)) {
        final ResourceType primaryResourceType = resourceType;

        // get the resource type
        resource = StringUtils.substringAfter(resource, resourceType.getValue());

        for (ResourceType type : ResourceType.values()) {
            if (resource.equals(type.getValue()) || resource.startsWith(type.getValue() + "/")) {
                resourceType = type;
            }
        }

        if (resourceType == null) {
            throw new ResourceNotFoundException("Unrecognized resource: " + resource);
        }

        // must either be a policy, event, or data transfer
        if (ResourceType.Policy.equals(primaryResourceType)) {
            return new AccessPolicyAuthorizable(getAccessPolicy(resourceType, resource));
        } else if (ResourceType.Data.equals(primaryResourceType)) {
            return new DataAuthorizable(getAccessPolicy(resourceType, resource));
        } else {
            return new DataTransferAuthorizable(getAccessPolicy(resourceType, resource));
        }
    } else {
        return getAccessPolicy(resourceType, resource);
    }
}

From source file:org.apache.nifi.authorization.StandardAuthorizableLookup.java

private Authorizable getAccessPolicy(final ResourceType resourceType, final String resource) {
    final String slashComponentId = StringUtils.substringAfter(resource, resourceType.getValue());
    if (slashComponentId.startsWith("/")) {
        return getAccessPolicyByResource(resourceType, slashComponentId.substring(1));
    } else {/*w ww  .j  av a2  s.  c  om*/
        return getAccessPolicyByResource(resourceType);
    }
}

From source file:org.apache.nifi.authorization.util.IdentityMappingUtil.java

/**
 * Builds the identity mappings from NiFiProperties.
 *
 * @param properties the NiFiProperties instance
 * @return a list of identity mappings/*w w w  . j av  a 2  s . c om*/
 */
public static List<IdentityMapping> getIdentityMappings(final NiFiProperties properties) {
    final List<IdentityMapping> mappings = new ArrayList<>();

    // go through each property
    for (String propertyName : properties.getPropertyKeys()) {
        if (StringUtils.startsWith(propertyName, NiFiProperties.SECURITY_IDENTITY_MAPPING_PATTERN_PREFIX)) {
            final String key = StringUtils.substringAfter(propertyName,
                    NiFiProperties.SECURITY_IDENTITY_MAPPING_PATTERN_PREFIX);
            final String identityPattern = properties.getProperty(propertyName);

            if (StringUtils.isBlank(identityPattern)) {
                LOGGER.warn("Identity Mapping property {} was found, but was empty",
                        new Object[] { propertyName });
                continue;
            }

            final String identityValueProperty = NiFiProperties.SECURITY_IDENTITY_MAPPING_VALUE_PREFIX + key;
            final String identityValue = properties.getProperty(identityValueProperty);

            if (StringUtils.isBlank(identityValue)) {
                LOGGER.warn("Identity Mapping property {} was found, but corresponding value {} was not found",
                        new Object[] { propertyName, identityValueProperty });
                continue;
            }

            final IdentityMapping identityMapping = new IdentityMapping(key, Pattern.compile(identityPattern),
                    identityValue);
            mappings.add(identityMapping);

            LOGGER.debug("Found Identity Mapping with key = {}, pattern = {}, value = {}",
                    new Object[] { key, identityPattern, identityValue });
        }
    }

    // sort the list by the key so users can control the ordering in nifi.properties
    Collections.sort(mappings, new Comparator<IdentityMapping>() {
        @Override
        public int compare(IdentityMapping m1, IdentityMapping m2) {
            return m1.getKey().compareTo(m2.getKey());
        }
    });

    return mappings;
}

From source file:org.apache.nifi.registry.properties.util.IdentityMappingUtil.java

/**
 * Builds the identity mappings from NiFiRegistryProperties.
 *
 * @param properties the NiFiRegistryProperties instance
 * @return a list of identity mappings//from  ww  w.j  ava2  s.  com
 */
public static List<IdentityMapping> getIdentityMappings(final NiFiRegistryProperties properties) {
    final List<IdentityMapping> mappings = new ArrayList<>();

    // go through each property
    for (String propertyName : properties.getPropertyKeys()) {
        if (StringUtils.startsWith(propertyName,
                NiFiRegistryProperties.SECURITY_IDENTITY_MAPPING_PATTERN_PREFIX)) {
            final String key = StringUtils.substringAfter(propertyName,
                    NiFiRegistryProperties.SECURITY_IDENTITY_MAPPING_PATTERN_PREFIX);
            final String identityPattern = properties.getProperty(propertyName);

            if (StringUtils.isBlank(identityPattern)) {
                LOGGER.warn("Identity Mapping property {} was found, but was empty",
                        new Object[] { propertyName });
                continue;
            }

            final String identityValueProperty = NiFiRegistryProperties.SECURITY_IDENTITY_MAPPING_VALUE_PREFIX
                    + key;
            final String identityValue = properties.getProperty(identityValueProperty);

            if (StringUtils.isBlank(identityValue)) {
                LOGGER.warn("Identity Mapping property {} was found, but corresponding value {} was not found",
                        new Object[] { propertyName, identityValueProperty });
                continue;
            }

            final IdentityMapping identityMapping = new IdentityMapping(key, Pattern.compile(identityPattern),
                    identityValue);
            mappings.add(identityMapping);

            LOGGER.debug("Found Identity Mapping with key = {}, pattern = {}, value = {}",
                    new Object[] { key, identityPattern, identityValue });
        }
    }

    // sort the list by the key so users can control the ordering in nifi-registry.properties
    Collections.sort(mappings, new Comparator<IdentityMapping>() {
        @Override
        public int compare(IdentityMapping m1, IdentityMapping m2) {
            return m1.getKey().compareTo(m2.getKey());
        }
    });

    return mappings;
}

From source file:org.apache.nifi.registry.security.authorization.StandardAuthorizableLookup.java

private Authorizable getAuthorizableByResource(final ResourceType resourceType, final String resource) {
    Authorizable authorizable = null;/*from   w  w w.j  a  va2  s. c om*/
    switch (resourceType) {

    /* Access to these resources are always authorized by the top-level resource */
    case Policy:
        authorizable = getPoliciesAuthorizable();
        break;
    case Tenant:
        authorizable = getTenantsAuthorizable();
        break;
    case Proxy:
        authorizable = getProxyAuthorizable();
        break;
    case Actuator:
        authorizable = getActuatorAuthorizable();
        break;
    case Swagger:
        authorizable = getSwaggerAuthorizable();
        break;

    /* Access to buckets can be authorized by the top-level /buckets resource or an individual /buckets/{id} resource */
    case Bucket:
        final String childResourceId = StringUtils.substringAfter(resource, resourceType.getValue());
        if (childResourceId.startsWith("/")) {
            authorizable = getAuthorizableByChildResource(resourceType, childResourceId);
        } else {
            authorizable = getBucketsAuthorizable();
        }
    }

    if (authorizable == null) {
        logger.debug("Could not determine the Authorizable for resource type='{}', path='{}', ",
                resourceType.getValue(), resource);
        throw new IllegalArgumentException(
                "This an unexpected type of authorizable resource: " + resourceType.getValue());
    }

    return authorizable;
}

From source file:org.apache.nifi.web.api.config.WebApplicationExceptionMapper.java

@Override
public Response toResponse(WebApplicationException exception) {
    // get the message and ensure it is not blank
    String message = exception.getMessage();
    if (message == null) {
        message = StringUtils.EMPTY;/*from  w ww .  j a v a  2 s  . c  o m*/
    }

    // format the message
    if (message.contains(EXCEPTION_SEPARATOR)) {
        message = StringUtils.substringAfter(message, EXCEPTION_SEPARATOR);
    }

    // get the response
    final Response response = exception.getResponse();

    // log the error
    logger.info(String.format("%s. Returning %s response.", exception, response.getStatus()));

    if (logger.isDebugEnabled()) {
        logger.debug(StringUtils.EMPTY, exception);
    }

    // generate the response
    return Response.status(response.getStatus()).entity(message).type("text/plain").build();
}

From source file:org.apache.nifi.web.StandardAuthorizableLookup.java

@Override
public Authorizable getAuthorizableFromResource(String resource) {
    // parse the resource type
    ResourceType resourceType = null;//from ww  w.j a v a  2  s  .com
    for (ResourceType type : ResourceType.values()) {
        if (resource.equals(type.getValue()) || resource.startsWith(type.getValue() + "/")) {
            resourceType = type;
        }
    }

    if (resourceType == null) {
        throw new ResourceNotFoundException("Unrecognized resource: " + resource);
    }

    // if this is a policy or a provenance event resource, there should be another resource type
    if (ResourceType.Policy.equals(resourceType) || ResourceType.ProvenanceEvent.equals(resourceType)
            || ResourceType.DataTransfer.equals(resourceType)) {
        final ResourceType primaryResourceType = resourceType;

        // get the resource type
        resource = StringUtils.substringAfter(resource, resourceType.getValue());

        for (ResourceType type : ResourceType.values()) {
            if (resource.equals(type.getValue()) || resource.startsWith(type.getValue() + "/")) {
                resourceType = type;
            }
        }

        if (resourceType == null) {
            throw new ResourceNotFoundException("Unrecognized resource: " + resource);
        }

        // must either be a policy, event, or data transfer
        if (ResourceType.Policy.equals(primaryResourceType)) {
            return new AccessPolicyAuthorizable(getAccessPolicy(resourceType, resource));
        } else if (ResourceType.ProvenanceEvent.equals(primaryResourceType)) {
            return new ProvenanceEventAuthorizable(getAccessPolicy(resourceType, resource));
        } else {
            return new DataTransferAuthorizable(getAccessPolicy(resourceType, resource));
        }
    } else {
        return getAccessPolicy(resourceType, resource);
    }
}

From source file:org.apache.nifi.web.StandardNiFiContentAccess.java

@Override
public DownloadableContent getContent(final ContentRequestContext request) {
    // if clustered, send request to cluster manager
    if (properties.isClustered() && clusterCoordinator != null && clusterCoordinator.isConnected()) {
        // get the URI
        URI dataUri;/*ww w.j av a 2  s  . c o  m*/
        try {
            dataUri = new URI(request.getDataUri());
        } catch (final URISyntaxException use) {
            throw new ClusterRequestException(use);
        }

        // set the request parameters
        final MultivaluedMap<String, String> parameters = new MultivaluedMapImpl();
        parameters.add(CLIENT_ID_PARAM, request.getClientId());

        // set the headers
        final Map<String, String> headers = new HashMap<>();

        // ensure we were able to detect the cluster node id
        if (request.getClusterNodeId() == null) {
            throw new IllegalArgumentException("Unable to determine the which node has the content.");
        }

        // get the target node and ensure it exists
        final NodeIdentifier nodeId = clusterCoordinator.getNodeIdentifier(request.getClusterNodeId());

        // replicate the request to the cluster coordinator, indicating the target node
        NodeResponse nodeResponse;
        try {
            headers.put(RequestReplicator.REPLICATION_TARGET_NODE_UUID_HEADER, nodeId.getId());
            final NodeIdentifier coordinatorNode = clusterCoordinator.getElectedActiveCoordinatorNode();
            if (coordinatorNode == null) {
                throw new NoClusterCoordinatorException();
            }
            final Set<NodeIdentifier> coordinatorNodes = Collections.singleton(coordinatorNode);
            nodeResponse = requestReplicator
                    .replicate(coordinatorNodes, HttpMethod.GET, dataUri, parameters, headers, false, true)
                    .awaitMergedResponse();
        } catch (InterruptedException e) {
            throw new IllegalClusterStateException("Interrupted while waiting for a response from node");
        }

        final ClientResponse clientResponse = nodeResponse.getClientResponse();
        final MultivaluedMap<String, String> responseHeaders = clientResponse.getHeaders();

        // ensure an appropriate response
        if (Status.NOT_FOUND.getStatusCode() == clientResponse.getStatusInfo().getStatusCode()) {
            throw new ResourceNotFoundException(clientResponse.getEntity(String.class));
        } else if (Status.FORBIDDEN.getStatusCode() == clientResponse.getStatusInfo().getStatusCode()
                || Status.UNAUTHORIZED.getStatusCode() == clientResponse.getStatusInfo().getStatusCode()) {
            throw new AccessDeniedException(clientResponse.getEntity(String.class));
        } else if (Status.OK.getStatusCode() != clientResponse.getStatusInfo().getStatusCode()) {
            throw new IllegalStateException(clientResponse.getEntity(String.class));
        }

        // get the file name
        final String contentDisposition = responseHeaders.getFirst("Content-Disposition");
        final String filename = StringUtils.substringBetween(contentDisposition, "filename=\"", "\"");

        // get the content type
        final String contentType = responseHeaders.getFirst("Content-Type");

        // create the downloadable content
        return new DownloadableContent(filename, contentType, clientResponse.getEntityInputStream());
    } else {
        // example URIs:
        // http://localhost:8080/nifi-api/provenance/events/{id}/content/{input|output}
        // http://localhost:8080/nifi-api/flowfile-queues/{uuid}/flowfiles/{uuid}/content

        // get just the context path for comparison
        final String dataUri = StringUtils.substringAfter(request.getDataUri(), "/nifi-api");
        if (StringUtils.isBlank(dataUri)) {
            throw new IllegalArgumentException("The specified data reference URI is not valid.");
        }

        // flowfile listing content
        final Matcher flowFileMatcher = FLOWFILE_CONTENT_URI_PATTERN.matcher(dataUri);
        if (flowFileMatcher.matches()) {
            final String connectionId = flowFileMatcher.group(1);
            final String flowfileId = flowFileMatcher.group(2);

            return getFlowFileContent(connectionId, flowfileId, dataUri);
        }

        // provenance event content
        final Matcher provenanceMatcher = PROVENANCE_CONTENT_URI_PATTERN.matcher(dataUri);
        if (provenanceMatcher.matches()) {
            try {
                final Long eventId = Long.parseLong(provenanceMatcher.group(1));
                final ContentDirection direction = ContentDirection
                        .valueOf(provenanceMatcher.group(2).toUpperCase());

                return getProvenanceEventContent(eventId, dataUri, direction);
            } catch (final IllegalArgumentException iae) {
                throw new IllegalArgumentException("The specified data reference URI is not valid.");
            }
        }

        // invalid uri
        throw new IllegalArgumentException("The specified data reference URI is not valid.");
    }
}

From source file:org.apache.nutch.protocol.htmlunit.HttpResponse.java

private void readPlainContent(URL url) throws IOException {
    Http.LOG.trace("Htmlunit fetching: " + url);
    String urlStr = url.toString();
    HtmlPage page = HttpWebClient.getHtmlPage(urlStr, conf);
    String pageAsXml = page.asXml();
    try {//from w w  w .  ja  va  2 s  .com
        int i = 0;
        boolean ok = true;
        while (i++ < MAX_AJAX_WAIT_SECONDS) {
            ok = true;
            //?Javascript?
            //false?
            if (htmlParseFilters != null) {
                try {
                    for (HtmlParseFilter htmlParseFilter : htmlParseFilters) {
                        /**
                         * ?????
                         * @see  AbstractHtmlParseFilter#isParseDataFetchLoaded
                         * ?@see S2jhHtmlParseFilter#isParseDataFetchLoaded
                         */
                        Method isParseDataFetchLoaded = MethodUtils.getAccessibleMethod(
                                htmlParseFilter.getClass(), "isParseDataFetchLoaded", String.class,
                                page.getClass());
                        if (isParseDataFetchLoaded != null) {
                            Boolean ret = (Boolean) isParseDataFetchLoaded.invoke(htmlParseFilter, urlStr,
                                    page);
                            if (ret == false) {
                                ok = false;
                                break;
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (ok == true) {
                Http.LOG.debug("Parse page description success for: {}", url);
                break;
            }
            Http.LOG.info("Sleep " + i + " seconds to wait execution...");
            Thread.sleep(1000);
        }
        if (!ok) {
            Http.LOG.warn("Parse page description failure for: {}", url);
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    //???????
    List toboRemoveNodes = Lists.newArrayList();
    toboRemoveNodes.addAll(page.getByXPath("//SCRIPT"));
    toboRemoveNodes.addAll(page.getByXPath("//STYLE"));
    toboRemoveNodes.addAll(page.getByXPath("//LINK"));
    toboRemoveNodes.addAll(page.getByXPath("//comment()"));
    for (Object node : toboRemoveNodes) {
        ((DomNode) node).remove();
    }

    pageAsXml = page.asXml();
    String charsetName = page.getPageEncoding();
    //xml
    pageAsXml = StringUtils.substringAfter(pageAsXml, "?>").trim();

    //System.out.println("URL: " + urlStr + ", CharsetName: " + charsetName + " , Page HTML=\n" + pageAsXml);
    if (Http.LOG.isTraceEnabled()) {
        Http.LOG.trace("URL: " + urlStr + ", CharsetName: " + charsetName + " , Page HTML=\n" + pageAsXml);
    }
    content = pageAsXml.getBytes(charsetName);
}

From source file:org.apache.nutch.protocol.s2jh.HttpResponse.java

private boolean readPlainContentByHtmlunit(URL url) throws Exception {

    String urlStr = url.toString();
    if (urlStr.indexOf("detail.tmall.com") > -1) {
        return false;
    }/*from w  w  w .  j  a va 2 s  . com*/
    Http.LOG.debug("Htmlunit fetching: " + url);
    HtmlPage page = (HtmlPage) HttpWebClient.getHtmlPage(urlStr, conf);
    charset = page.getPageEncoding();
    String html = null;
    boolean ok = true;

    int i = 0;
    while (i++ < MAX_AJAX_WAIT_SECONDS) {
        html = page.asXml();
        ok = isParseDataFetchLoaded(urlStr, html);
        if (ok) {
            break;
        }
        Http.LOG.info("Sleep " + i + " seconds to wait Htmlunit execution...");
        Thread.sleep(1000);
    }
    if (ok == true) {
        this.code = 200;
        Http.LOG.debug("Success parse page by Htmlunit  for: {}", url);
        html = StringUtils.substringAfter(html, "?>").trim();
    }

    if (ok) {
        this.code = 200;
        content = html.getBytes(charset);
    } else {
        Http.LOG.warn("Failure Htmlunit parse page for: {}", url);
        Http.LOG.warn(
                "Htmlunit Fetch Failure URL: " + url + ", CharsetName: " + charset + " , Page HTML=\n" + html);
    }
    return ok;
}