Example usage for org.apache.commons.lang StringUtils splitPreserveAllTokens

List of usage examples for org.apache.commons.lang StringUtils splitPreserveAllTokens

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils splitPreserveAllTokens.

Prototype

public static String[] splitPreserveAllTokens(String str, String separatorChars) 

Source Link

Document

Splits the provided text into an array, separators specified, preserving all tokens, including empty tokens created by adjacent separators.

Usage

From source file:org.apache.shindig.gadgets.rewrite.TemplateRewriter.java

/**
 * Register templates with a "tag" attribute.
 *//* w  w  w.  j av a2  s .com*/
private TagRegistry registerCustomTags(List<Element> allTemplates) {
    ImmutableSet.Builder<TagHandler> handlers = ImmutableSet.builder();
    for (Element template : allTemplates) {
        // Only process templates with a tag attribute
        if (template.getAttribute("tag").length() == 0) {
            continue;
        }

        String[] nameParts = StringUtils.splitPreserveAllTokens(template.getAttribute("tag"), ':');
        // At this time, we only support 
        if (nameParts.length != 2) {
            continue;
        }
        String namespaceUri = template.lookupNamespaceURI(nameParts[0]);
        if (namespaceUri != null) {
            handlers.add(new TemplateBasedTagHandler(template, namespaceUri, nameParts[1]));
        }
    }

    return new DefaultTagRegistry(handlers.build());
}

From source file:org.apache.shindig.gadgets.servlet.MakeRequestHandler.java

/**
 * Generate a remote content request based on the parameters
 * sent from the client./*from   w w w .  j  a  v  a  2s .c om*/
 * @throws GadgetException
 */
protected HttpRequest buildHttpRequest(HttpServletRequest request) throws GadgetException {
    String urlStr = request.getParameter(Param.URL.getKey());
    if (urlStr == null) {
        throw new GadgetException(GadgetException.Code.INVALID_PARAMETER,
                Param.URL.getKey() + " parameter is missing.", HttpResponse.SC_BAD_REQUEST);
    }

    Uri url = null;
    try {
        url = ServletUtil.validateUrl(Uri.parse(urlStr));
    } catch (IllegalArgumentException e) {
        throw new GadgetException(GadgetException.Code.INVALID_PARAMETER,
                "Invalid " + Param.URL.getKey() + " parameter", HttpResponse.SC_BAD_REQUEST);
    }

    HttpRequest req = new HttpRequest(url).setMethod(getParameter(request, METHOD_PARAM, "GET"))
            .setContainer(getContainer(request));

    setPostData(request, req);

    String headerData = getParameter(request, HEADERS_PARAM, "");
    if (headerData.length() > 0) {
        String[] headerList = StringUtils.split(headerData, '&');
        for (String header : headerList) {
            String[] parts = StringUtils.splitPreserveAllTokens(header, '=');
            if (parts.length != 2) {
                throw new GadgetException(GadgetException.Code.INVALID_PARAMETER,
                        "Malformed header param specified:" + header, HttpResponse.SC_BAD_REQUEST);
            }
            String headerName = Utf8UrlCoder.decode(parts[0]);
            if (!HttpRequestHandler.BAD_HEADERS.contains(headerName.toUpperCase())) {
                req.addHeader(headerName, Utf8UrlCoder.decode(parts[1]));
            }
        }
    }

    // Set the default content type  for post requests when a content type is not specified
    if ("POST".equals(req.getMethod()) && req.getHeader("Content-Type") == null) {
        req.addHeader("Content-Type", "application/x-www-form-urlencoded");
    }

    req.setIgnoreCache("1".equals(request.getParameter(Param.NO_CACHE.getKey())));

    if (request.getParameter(Param.GADGET.getKey()) != null) {
        req.setGadget(Uri.parse(request.getParameter(Param.GADGET.getKey())));
    }

    // If the proxy request specifies a refresh param then we want to force the min TTL for
    // the retrieved entry in the cache regardless of the headers on the content when it
    // is fetched from the original source.
    if (request.getParameter(Param.REFRESH.getKey()) != null) {
        try {
            req.setCacheTtl(Integer.parseInt(request.getParameter(Param.REFRESH.getKey())));
        } catch (NumberFormatException nfe) {
            // Ignore
        }
    }
    // Allow the rewriter to use an externally forced mime type. This is needed
    // allows proper rewriting of <script src="x"/> where x is returned with
    // a content type like text/html which unfortunately happens all too often
    req.setRewriteMimeType(request.getParameter(Param.REWRITE_MIME_TYPE.getKey()));

    // Figure out whether authentication is required
    AuthType auth = AuthType.parse(getParameter(request, AUTHZ_PARAM, null));
    req.setAuthType(auth);
    if (auth != AuthType.NONE) {
        req.setSecurityToken(extractAndValidateToken(request));
        req.setOAuthArguments(new OAuthArguments(auth, request));
    }

    ServletUtil.setXForwardedForHeader(request, req);
    return req;
}

From source file:org.apache.shindig.gadgets.templates.XmlTemplateLibrary.java

private TagHandler createHandler(String tagName, Element template, Set<TemplateResource> resources)
        throws TemplateParserException {
    String[] nameParts = StringUtils.splitPreserveAllTokens(tagName, ':');
    // At this time, we only support namespaced tags
    if (nameParts.length != 2) {
        return null;
    }/*w ww  .j a v  a2s . c o m*/
    String namespaceUri = template.lookupNamespaceURI(nameParts[0]);
    if (!nsPrefix.equals(nameParts[0]) || !nsUri.equals(namespaceUri)) {
        throw new TemplateParserException("Can't create tags in undeclared namespace: " + nameParts[0]);
    }

    if (isSafe()) {
        bypassTemplateSanitization(template);
    }

    return new LibraryTagHandler(createTagHandler(template, namespaceUri, nameParts[1]), resources);
}

From source file:org.apache.shindig.protocol.DefaultHandlerRegistry.java

/**
 * Get a REST request handler//from   w w w .jav a  2s .  co  m
 */
@Override
public RestHandler getRestHandler(String path, String method) {
    method = method.toUpperCase();
    if (path != null) {
        if (path.startsWith("/")) {
            path = path.substring(1);
        }
        String[] pathParts = StringUtils.splitPreserveAllTokens(path, '/');
        Map<String, SortedSet<RestPath>> methods = serviceMethodPathMap.get(pathParts[0]);
        if (methods != null) {
            SortedSet<RestPath> paths = methods.get(method);
            if (paths != null) {
                for (RestPath restPath : paths) {
                    RestHandler handler = restPath.accept(pathParts);
                    if (handler != null) {
                        return handler;
                    }
                }
            }
        }
    }
    return new ErrorRestHandler(new AipoProtocolException(
            AipoErrorCode.NOT_IMPLEMENTED.customMessage("No service defined for path " + path)));
}

From source file:org.apache.tajo.parser.sql.SQLErrorListener.java

public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
        String msg, RecognitionException e) {
    CommonTokenStream tokens = (CommonTokenStream) recognizer.getInputStream();
    String input = tokens.getTokenSource().getInputStream().toString();
    Token token = (Token) offendingSymbol;
    String[] lines = StringUtils.splitPreserveAllTokens(input, '\n');
    String errorLine = lines[line - 1];

    String simpleMessage = "syntax error at or near \"" + token.getText() + "\"";
    throw new SQLParseError(token, line, charPositionInLine, simpleMessage, errorLine);
}

From source file:org.apache.tajo.util.TestBytes.java

@Test
public void testSplitBytes() {
    String text = "abcde|12345|ABCDE";
    char separatorChar = '|';

    String[] textArray = StringUtils.splitPreserveAllTokens(text, separatorChar);
    byte[][] bytesArray = Bytes.splitPreserveAllTokens(text.getBytes(), separatorChar);

    assertEquals(textArray.length, bytesArray.length);
    for (int i = 0; i < textArray.length; i++) {
        assertArrayEquals(textArray[i].getBytes(), bytesArray[i]);
    }//w  ww.j  a  va 2 s. c o  m
}

From source file:org.apache.tajo.util.TestBytes.java

@Test
public void testSplitProjectionBytes() {
    String text = "abcde|12345|ABCDE";
    int[] target = new int[] { 1 };
    char separatorChar = '|';

    String[] textArray = StringUtils.splitPreserveAllTokens(text, separatorChar);
    byte[][] bytesArray = Bytes.splitPreserveAllTokens(text.getBytes(), separatorChar, target);

    assertEquals(textArray.length, bytesArray.length);

    assertNull(bytesArray[0]);/*  w w  w  . j a v  a  2s.c  o m*/
    assertNotNull(bytesArray[1]);
    assertArrayEquals(textArray[1].getBytes(), bytesArray[1]);
    assertNull(bytesArray[2]);
}

From source file:org.calrissian.accumulorecipes.thirdparty.pig.loader.EntityLoader.java

@Override
public void setLocation(String uri, Job job) throws IOException {

    Configuration conf = job.getConfiguration();

    if (!isConnectorInfoSet(AccumuloInputFormat.class, job.getConfiguration())) {

        String path = uri.substring(uri.indexOf("://") + 3, uri.indexOf("?"));

        String[] indexAndShardTable = StringUtils.splitPreserveAllTokens(path, "/");
        if (indexAndShardTable.length != 2)
            throw new IOException("Path portion of URI must contain the index and shard tables. " + USAGE);

        if (uri.startsWith("entity")) {
            String queryPortion = uri.substring(uri.indexOf("?") + 1, uri.length());
            Multimap<String, String> queryParams = UriUtils.splitQuery(queryPortion);

            String accumuloUser = getProp(queryParams, "user");
            String accumuloPass = getProp(queryParams, "pass");
            String accumuloInst = getProp(queryParams, "inst");
            String zookeepers = getProp(queryParams, "zk");
            if (accumuloUser == null || accumuloPass == null || accumuloInst == null || zookeepers == null)
                throw new IOException(
                        "Some Accumulo connection information is missing. Must supply username, password, instance, and zookeepers. "
                                + USAGE);

            String types = getProp(queryParams, "types");
            if (types == null)
                throw new IOException("A comma-separated list of entity types to load is required. " + USAGE);

            String auths = getProp(queryParams, "auths");
            if (auths == null)
                auths = ""; // default auths to empty

            String selectFields = getProp(queryParams, "fields");

            Set<String> fields = selectFields != null
                    ? newHashSet(asList(splitPreserveAllTokens(selectFields, ",")))
                    : null;/*from  www.  j  a  v  a 2  s.co  m*/
            Set<String> entitytypes = newHashSet(asList(splitPreserveAllTokens(types, ",")));

            EntityInputFormat.setZooKeeperInstance(job, accumuloInst, zookeepers);
            try {
                EntityInputFormat.setInputInfo(job, accumuloUser, accumuloPass.getBytes(),
                        new Authorizations(auths.getBytes()));
            } catch (AccumuloSecurityException e) {
                throw new RuntimeException(e);
            }
            try {
                if (qb != null)
                    EntityInputFormat.setQueryInfo(job, entitytypes, qb.build());
                else
                    EntityInputFormat.setQueryInfo(job, entitytypes);

                if (fields != null)
                    EntityInputFormat.setSelectFields(conf, fields);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            throw new IOException("Location uri must begin with event://");
        }
    }
}

From source file:org.calrissian.accumulorecipes.thirdparty.pig.loader.EventLoader.java

@Override
public void setLocation(String uri, Job job) throws IOException {

    Configuration conf = job.getConfiguration();
    if (!ConfiguratorBase.isConnectorInfoSet(AccumuloInputFormat.class, job.getConfiguration())) {
        String path = uri.substring(uri.indexOf("://") + 3, uri.indexOf("?"));

        String[] indexAndShardTable = StringUtils.splitPreserveAllTokens(path, "/");
        if (indexAndShardTable.length != 2)
            throw new IOException("Path portion of URI must contain the index and shard tables. " + USAGE);

        if (uri.startsWith("event")) {
            String queryPortion = uri.substring(uri.indexOf("?") + 1, uri.length());
            Multimap<String, String> queryParams = UriUtils.splitQuery(queryPortion);

            String accumuloUser = getProp(queryParams, "user");
            String accumuloPass = getProp(queryParams, "pass");
            String accumuloInst = getProp(queryParams, "inst");
            String zookeepers = getProp(queryParams, "zk");
            if (accumuloUser == null || accumuloPass == null || accumuloInst == null || zookeepers == null)
                throw new IOException(
                        "Some Accumulo connection information is missing. Must supply username, password, instance, and zookeepers. "
                                + USAGE);

            String startTime = getProp(queryParams, "start");
            String endTime = getProp(queryParams, "end");
            if (startTime == null || endTime == null)
                throw new IOException("Start and end times are required. " + USAGE);

            String auths = getProp(queryParams, "auths");
            if (auths == null)
                auths = ""; // default auths to empty
            String selectFields = getProp(queryParams, "fields");

            String types = getProp(queryParams, "types");

            Set<String> fields = selectFields != null
                    ? Sets.newHashSet(StringUtils.splitPreserveAllTokens(selectFields, ","))
                    : null;//w  w w . j  av a2  s  .  c o m
            Set<String> finalTypes = types != null
                    ? Sets.newHashSet(StringUtils.splitPreserveAllTokens(types, ","))
                    : null;

            DateTime startDT = new DateTime(startTime);
            DateTime endDT = new DateTime(endTime);

            AbstractInputFormat.setZooKeeperInstance(job, accumuloInst, zookeepers);
            try {
                EventInputFormat.setInputInfo(job, accumuloUser, accumuloPass.getBytes(),
                        new Authorizations(auths.getBytes()));
            } catch (AccumuloSecurityException e) {
                throw new RuntimeException(e);
            }
            try {
                EventInputFormat.setQueryInfo(job, startDT.toDate(), endDT.toDate(), finalTypes, qb.build());
                if (fields != null)
                    EventInputFormat.setSelectFields(conf, fields);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            throw new IOException("Location uri must begin with event://");
        }
    }
}

From source file:org.calrissian.accumulorecipes.thirdparty.pig.loader.MetricFeatureLoader.java

@Override
public void setLocation(String uri, Job job) throws IOException {

    String path = uri.substring(uri.indexOf("://") + 3, uri.indexOf("?"));

    if (!isConnectorInfoSet(AccumuloInputFormat.class, job.getConfiguration())) {

        String[] indexAndShardTable = StringUtils.splitPreserveAllTokens(path, "/");
        if (indexAndShardTable.length != 1)
            throw new IOException("Path portion of URI must contain the metric table prefix " + USAGE);

        if (uri.startsWith("metrics")) {
            String queryPortion = uri.substring(uri.indexOf("?") + 1, uri.length());
            Multimap<String, String> queryParams = UriUtils.splitQuery(queryPortion);

            String accumuloUser = getProp(queryParams, "user");
            String accumuloPass = getProp(queryParams, "pass");
            String accumuloInst = getProp(queryParams, "inst");
            String zookeepers = getProp(queryParams, "zk");
            if (accumuloUser == null || accumuloPass == null || accumuloInst == null || zookeepers == null)
                throw new IOException(
                        "Some Accumulo connection information is missing. Must supply username, password, instance, and zookeepers. "
                                + USAGE);

            String timeUnitStr = getProp(queryParams, "timeUnit");
            if (timeUnitStr != null)
                timeUnit = TimeUnit.valueOf(timeUnitStr.toUpperCase());
            else/*from  ww w.j a  v  a  2s  . c  o m*/
                throw new IOException("A valid TimeUnit must be supplied. " + USAGE);

            String group = getProp(queryParams, "group");
            String type = getProp(queryParams, "type");
            String name = getProp(queryParams, "name");

            String startTime = getProp(queryParams, "start");
            String endTime = getProp(queryParams, "end");
            if (startTime == null || endTime == null)
                throw new IOException("Start and end times are required. " + USAGE);

            String auths = getProp(queryParams, "auths");
            if (auths == null)
                auths = ""; // default to empty auths

            DateTime startDT = DateTime.parse(startTime);
            DateTime endDT = DateTime.parse(endTime);

            FeaturesInputFormat.setZooKeeperInstance(job, accumuloInst, zookeepers);
            try {
                FeaturesInputFormat.setInputInfo(job, accumuloUser, accumuloPass.getBytes(),
                        new Authorizations(auths.getBytes()));
            } catch (AccumuloSecurityException e) {
                throw new RuntimeException(e);
            }
            try {
                FeaturesInputFormat.setQueryInfo(job, startDT.toDate(), endDT.toDate(), timeUnit, group, type,
                        name, MetricFeature.class);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            throw new IOException("Location uri must begin with metrics://");
        }
    }
}