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

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

Introduction

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

Prototype

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

Source Link

Document

Gets the substring before the first occurrence of a separator.

Usage

From source file:org.apache.olingo.fit.AbstractServices.java

protected Response getEntityInternal(final String location, final String accept, final String entitySetName,
        final String entityId, final String format, String expand, final String select) {

    try {//from  w  ww.j  ava2s . com
        final Map.Entry<Accept, AbstractUtilities> utils = getUtilities(accept, format);

        if (utils.getKey() == Accept.XML || utils.getKey() == Accept.TEXT) {
            throw new UnsupportedMediaTypeException("Unsupported media type");
        }

        final Map.Entry<String, InputStream> entityInfo = utils.getValue().readEntity(entitySetName, entityId,
                Accept.ATOM);

        final InputStream entity = entityInfo.getValue();

        ResWrap<Entity> container = atomDeserializer.toEntity(entity);
        if (container.getContextURL() == null) {
            container = new ResWrap<Entity>(
                    URI.create(Constants.get(version, ConstantKey.ODATA_METADATA_PREFIX) + entitySetName
                            + Constants.get(version, ConstantKey.ODATA_METADATA_ENTITY_SUFFIX)),
                    container.getMetadataETag(), container.getPayload());
        }
        final Entity entry = container.getPayload();

        if ((this instanceof V3KeyAsSegment) || (this instanceof V4KeyAsSegment)) {
            final Link editLink = new LinkImpl();
            editLink.setRel("edit");
            editLink.setTitle(entitySetName);
            editLink.setHref(
                    Constants.get(version, ConstantKey.DEFAULT_SERVICE_URL) + entitySetName + "/" + entityId);

            entry.setEditLink(editLink);
        }

        if (StringUtils.isNotBlank(select)) {
            final List<String> properties = Arrays.asList(select.split(","));
            final Set<Property> toBeRemoved = new HashSet<Property>();

            for (Property property : entry.getProperties()) {
                if (!properties.contains(property.getName())) {
                    toBeRemoved.add(property);
                }
            }

            entry.getProperties().removeAll(toBeRemoved);

            final Set<Link> linkToBeRemoved = new HashSet<Link>();

            for (Link link : entry.getNavigationLinks()) {
                if (!properties.contains(link.getTitle().replaceAll("@.*$", ""))
                        && !properties.contains(link.getTitle())) {
                    linkToBeRemoved.add(link);
                }
            }

            entry.getNavigationLinks().removeAll(linkToBeRemoved);
        }

        if (StringUtils.isNotBlank(expand)) {
            expand = StringUtils.substringBefore(expand, "(");
            final List<String> links = Arrays.asList(expand.split(","));

            final Map<Link, Link> replace = new HashMap<Link, Link>();

            for (Link link : entry.getNavigationLinks()) {
                if (links.contains(link.getTitle())) {
                    // expand link
                    final Link rep = new LinkImpl();
                    rep.setHref(link.getHref());
                    rep.setRel(link.getRel());
                    rep.setTitle(link.getTitle());
                    rep.setType(link.getType());
                    if (link.getType().equals(Constants.get(version, ConstantKey.ATOM_LINK_ENTRY))) {
                        // inline entry
                        final Entity inline = atomDeserializer
                                .toEntity(xml.expandEntity(entitySetName, entityId, link.getTitle()))
                                .getPayload();
                        rep.setInlineEntity(inline);
                    } else if (link.getType().equals(Constants.get(version, ConstantKey.ATOM_LINK_FEED))) {
                        // inline feed
                        final EntitySet inline = atomDeserializer
                                .toEntitySet(xml.expandEntity(entitySetName, entityId, link.getTitle()))
                                .getPayload();
                        rep.setInlineEntitySet(inline);
                    }
                    replace.put(link, rep);
                }
            }

            for (Map.Entry<Link, Link> link : replace.entrySet()) {
                entry.getNavigationLinks().remove(link.getKey());
                entry.getNavigationLinks().add(link.getValue());
            }
        }

        return xml.createResponse(location, xml.writeEntity(utils.getKey(), container),
                Commons.getETag(entityInfo.getKey(), version), utils.getKey());
    } catch (Exception e) {
        LOG.error("Error retrieving entity", e);
        return xml.createFaultResponse(accept, e);
    }
}

From source file:org.apache.olingo.fit.rest.OAuth2RequestFilter.java

@Override
public void filter(final ContainerRequestContext context) {
    final String svcName = StringUtils
            .substringBefore(StringUtils.substringAfter(context.getUriInfo().getPath(), "/"), "/");
    if ("OAuth2.svc".equals(svcName)) {
        super.filter(context);
    }/*w w w .ja v a 2 s. c  o m*/
}

From source file:org.apache.olingo.fit.rest.ServiceNameResponseFilter.java

@Override
public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext)
        throws IOException {

    final String svcName = StringUtils
            .substringBefore(StringUtils.substringAfter(requestContext.getUriInfo().getPath(), "/"), "/");

    if ("OAuth2.svc".equals(svcName) && responseContext.getEntity() != null) {
        final String content = IOUtils.toString((InputStream) responseContext.getEntity(), Constants.ENCODING)
                .replaceAll("Static\\.svc", svcName);

        final InputStream toBeStreamedBack = IOUtils.toInputStream(content, Constants.ENCODING);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IOUtils.copy(toBeStreamedBack, baos);
        IOUtils.closeQuietly(toBeStreamedBack);

        responseContext.setEntity(new ByteArrayInputStream(baos.toByteArray()));
    }/*from  ww  w .  jav a 2  s  .c om*/
}

From source file:org.apache.olingo.fit.Services.java

protected Response getEntityInternal(final String location, final String accept, final String entitySetName,
        final String entityId, final String format, final String expand, final String select) {

    try {/*from   w  w w .j av  a 2  s  .co m*/
        final Map.Entry<Accept, AbstractUtilities> utils = getUtilities(accept, format);

        if (utils.getKey() == Accept.XML || utils.getKey() == Accept.TEXT) {
            throw new UnsupportedMediaTypeException("Unsupported media type");
        }

        final Map.Entry<String, InputStream> entityInfo = utils.getValue().readEntity(entitySetName, entityId,
                Accept.ATOM);

        final InputStream entity = entityInfo.getValue();

        ResWrap<Entity> container = atomDeserializer.toEntity(entity);
        if (container.getContextURL() == null) {
            container = new ResWrap<Entity>(
                    URI.create(Constants.get(ConstantKey.ODATA_METADATA_PREFIX) + entitySetName
                            + Constants.get(ConstantKey.ODATA_METADATA_ENTITY_SUFFIX)),
                    container.getMetadataETag(), container.getPayload());
        }
        final Entity entry = container.getPayload();

        if ((this instanceof KeyAsSegment)) {
            final Link editLink = new Link();
            editLink.setRel("edit");
            editLink.setTitle(entitySetName);
            editLink.setHref(Constants.get(ConstantKey.DEFAULT_SERVICE_URL) + entitySetName + "/" + entityId);

            entry.setEditLink(editLink);
        }

        if (StringUtils.isNotBlank(select)) {
            final List<String> properties = Arrays.asList(select.split(","));
            final Set<Property> toBeRemoved = new HashSet<Property>();

            for (Property property : entry.getProperties()) {
                if (!properties.contains(property.getName())) {
                    toBeRemoved.add(property);
                }
            }

            entry.getProperties().removeAll(toBeRemoved);

            final Set<Link> linkToBeRemoved = new HashSet<Link>();

            for (Link link : entry.getNavigationLinks()) {
                if (!properties.contains(link.getTitle().replaceAll("@.*$", ""))
                        && !properties.contains(link.getTitle())) {
                    linkToBeRemoved.add(link);
                }
            }

            entry.getNavigationLinks().removeAll(linkToBeRemoved);
        }

        String tempExpand = expand;
        if (StringUtils.isNotBlank(tempExpand)) {
            tempExpand = StringUtils.substringBefore(tempExpand, "(");
            final List<String> links = Arrays.asList(tempExpand.split(","));

            final Map<Link, Link> replace = new HashMap<Link, Link>();

            for (Link link : entry.getNavigationLinks()) {
                if (links.contains(link.getTitle())) {
                    // expand link
                    final Link rep = new Link();
                    rep.setHref(link.getHref());
                    rep.setRel(link.getRel());
                    rep.setTitle(link.getTitle());
                    rep.setType(link.getType());
                    if (link.getType().equals(Constants.get(ConstantKey.ATOM_LINK_ENTRY))) {
                        // inline entry
                        final Entity inline = atomDeserializer
                                .toEntity(xml.expandEntity(entitySetName, entityId, link.getTitle()))
                                .getPayload();
                        rep.setInlineEntity(inline);
                    } else if (link.getType().equals(Constants.get(ConstantKey.ATOM_LINK_FEED))) {
                        // inline feed
                        final EntityCollection inline = atomDeserializer
                                .toEntitySet(xml.expandEntity(entitySetName, entityId, link.getTitle()))
                                .getPayload();
                        rep.setInlineEntitySet(inline);
                    }
                    replace.put(link, rep);
                }
            }

            for (Map.Entry<Link, Link> link : replace.entrySet()) {
                entry.getNavigationLinks().remove(link.getKey());
                entry.getNavigationLinks().add(link.getValue());
            }
        }

        return xml.createResponse(location, xml.writeEntity(utils.getKey(), container),
                Commons.getETag(entityInfo.getKey()), utils.getKey());
    } catch (Exception e) {
        LOG.error("Error retrieving entity", e);
        return xml.createFaultResponse(accept, e);
    }
}

From source file:org.apache.sling.servlethelpers.MockSlingHttpServletRequest.java

public void setContentType(String type) {
    this.contentType = type;
    if (StringUtils.contains(this.contentType, CHARSET_SEPARATOR)) {
        this.characterEncoding = StringUtils.substringAfter(this.contentType, CHARSET_SEPARATOR);
        this.contentType = StringUtils.substringBefore(this.contentType, CHARSET_SEPARATOR);
    }/*from  w ww . java2  s.  c  o m*/
}

From source file:org.apache.sling.servlethelpers.MockSlingHttpServletResponse.java

@Override
public void setContentType(String type) {
    this.contentType = type;
    if (StringUtils.contains(this.contentType, CHARSET_SEPARATOR)) {
        this.characterEncoding = StringUtils.substringAfter(this.contentType, CHARSET_SEPARATOR);
        this.contentType = StringUtils.substringBefore(this.contentType, CHARSET_SEPARATOR);
    }/* w w w .  j ava  2  s .  c  o  m*/
}

From source file:org.apache.sling.testing.junit.rules.util.IgnoreTestsConfig.java

public Match match(String fqdn) {
    if (null == fqdn || "".equals(fqdn)) {
        throw new IllegalArgumentException("The ignore class/method String must not be null or empty");
    }/*from w  w  w . j  a va  2s. c  o  m*/
    String className = StringUtils.substringBefore(fqdn, "#");
    Match match = matchToken(fqdn);
    if (!match.isIgnored() && (fqdn.indexOf('#') > 0)) {
        return matchToken(className);
    } else {
        return match;
    }
}

From source file:org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.java

public static String getMetadataPath(Class clazz) {
    return "OSGI-INF/" + StringUtils.substringBefore(clazz.getName(), "$") + ".xml";
}

From source file:org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.java

/**
 * @param clazz OSGi component//from w w  w  .  java2  s.  c om
 * @return XPath query fragment to find matching XML node in SCR metadata
 */
private static String getComponentXPathQuery(Class clazz) {
    String className = StringUtils.substringBefore(clazz.getName(), "$$Enhancer");
    return "//*[implementation/@class='" + className + "' or @name='" + className + "']";
}

From source file:org.apache.streams.util.schema.UriUtil.java

public static URI removeFragment(URI id) {
    return URI.create(StringUtils.substringBefore(id.toString(), "#"));
}