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

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

Introduction

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

Prototype

public static String substringBefore(String str, String separator) 

Source Link

Document

Gets the substring before the first occurrence of a separator.

Usage

From source file:$.ResourceServlet.java

@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String resourcePath = StringUtils.substringBefore(request.getPathInfo(), ";");

        if (LOG.isDebugEnabled()) {
            LOG.debug("Processing request for resource {}.", resourcePath);
        }/*w  ww .j a  v a 2s .co m*/

        URL resource = getResourceURL(resourcePath);

        if (resource == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Resource not found: {}", resourcePath);
            }
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        long ifModifiedSince = request.getDateHeader("If-Modified-Since");

        URLConnection conn = resource.openConnection();
        long lastModified = conn.getLastModified();

        if (ifModifiedSince >= lastModified) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Resource: {} Not Modified.", resourcePath);
            }
            response.setStatus(304);
            return;
        }

        int contentLength = conn.getContentLength();

        prepareResponse(response, resource, lastModified, contentLength);

        OutputStream out = selectOutputStream(request, response);

        try {
            InputStream is = conn.getInputStream();
            try {
                byte[] buffer = new byte[1024];
                int bytesRead = -1;
                while ((bytesRead = is.read(buffer)) != -1) {
                    out.write(buffer, 0, bytesRead);
                }
            } finally {
                is.close();
            }
        } finally {
            out.close();
        }

    }

From source file:ch.cyberduck.core.ftp.FTPPath.java

protected Map<String, Map<String, String>> parseFacts(String line) {
    final Pattern p = Pattern.compile("\\s?(\\S+\\=\\S+;)*\\s(.*)");
    final Matcher result = p.matcher(line);
    Map<String, Map<String, String>> file = new HashMap<String, Map<String, String>>();
    if (result.matches()) {
        final String filename = result.group(2);
        final Map<String, String> facts = new HashMap<String, String>();
        for (String fact : result.group(1).split(";")) {
            String key = StringUtils.substringBefore(fact, "=");
            if (StringUtils.isBlank(key)) {
                continue;
            }//from ww w  . ja  v  a 2  s  . c  om
            String value = StringUtils.substringAfter(fact, "=");
            if (StringUtils.isBlank(value)) {
                continue;
            }
            facts.put(key.toLowerCase(java.util.Locale.ENGLISH), value);
        }
        file.put(filename, facts);
        return file;
    }
    log.warn("No match for " + line);
    return null;
}

From source file:hudson.cli.DisablePluginCommandTest.java

/**
 * Helper method to check the output of a result with a specific method allowing two arguments (
 * StringUtils::startsWith or StringUtils::contents). This method avoid to have it hardcoded the messages. We avoid
 * having to compose the descriptive text of the message by using a <i>stop</i> flag to ignore the last characters.
 * This method supposes that the descriptive text is at the last of the string.
 * @param result the result of the command.
 * @param method a method with two string arguments to check against
 * @param plugin the plugin printed outSetting the plugin and status as parameters, the method gets
 * the string printed using the/*  w w  w  .ja va 2  s .co  m*/
 * @param status the status printed out
 * @return true if the output has been checked against the method using the plugin and status args
 */
private boolean checkResultWith(CLICommandInvoker.Result result, BiPredicate<String, String> method,
        String plugin, PluginWrapper.PluginDisableStatus status) {
    String noMatterFollowingChars = "/!$stop";
    String outExpected = Messages.DisablePluginCommand_StatusMessage(plugin, status, noMatterFollowingChars);
    outExpected = StringUtils.substringBefore(outExpected, noMatterFollowingChars);
    return method.test(result.stdout(), outExpected);
}

From source file:info.magnolia.cms.security.SecurityUtil.java

public static String stripPasswordFromUrl(String url) {
    if (StringUtils.isBlank(url)) {
        return null;
    }//  ww  w . j av a 2s  . c  o  m
    String value = null;
    value = StringUtils.substringBefore(url, "mgnlUserPSWD");
    value = value + StringUtils.substringAfter(StringUtils.substringAfter(url, "mgnlUserPSWD"), "&");
    return StringUtils.removeEnd(value, "&");
}

From source file:info.magnolia.cms.security.SecurityUtil.java

public static String stripParameterFromCacheLog(String log, String parameter) {
    if (StringUtils.isBlank(log)) {
        return null;
    } else if (!StringUtils.contains(log, parameter)) {
        return log;
    }/*from   w w w.  ja v  a  2s .c  o m*/
    String value = null;
    value = StringUtils.substringBefore(log, parameter);
    String afterString = StringUtils.substringAfter(log, parameter);
    if (StringUtils.indexOf(afterString, " ") < StringUtils.indexOf(afterString, "}")) {
        value = value + StringUtils.substringAfter(afterString, " ");
    } else {
        value = value + "}" + StringUtils.substringAfter(afterString, "}");
    }
    return value;
}

From source file:com.amalto.core.server.StorageAdminImpl.java

public boolean exist(String storageName, StorageType storageType) {
    if (storageName.contains("/")) { //$NON-NLS-1$
        // Handle legacy scenarios where callers pass container names such as 'Product/ProductFamily'
        storageName = StringUtils.substringBefore(storageName, "/"); //$NON-NLS-1$
    }/*from   ww  w .  j  av a  2  s .  c  om*/
    Storage storage;
    switch (storageType) {
    case STAGING:
        if (storageName.endsWith(STAGING_SUFFIX)) {
            storageName = StringUtils.substringBefore(storageName, STAGING_SUFFIX);
        }
        storage = getRegisteredStorage(storageName, StorageType.STAGING);
        break;
    case MASTER:
        storage = getRegisteredStorage(storageName, StorageType.MASTER);
        break;
    case SYSTEM:
        storage = getRegisteredStorage(SYSTEM_STORAGE, StorageType.SYSTEM);
        break;
    default:
        throw new NotImplementedException("No support for storage type '" + storageType + "'.");
    }
    return storage != null && storage.getType() == storageType;
}

From source file:com.amalto.core.history.accessor.record.DataRecordAccessor.java

@SuppressWarnings({ "rawtypes" })
@Override//from  www.j a  va 2  s.c  o  m
public boolean exist() {
    if (cachedExist != null) {
        return cachedExist;
    }
    StringTokenizer tokenizer = new StringTokenizer(path, "/"); //$NON-NLS-1$
    DataRecord current = dataRecord;
    while (tokenizer.hasMoreElements()) {
        String element = tokenizer.nextToken();
        if (element.indexOf('@') == 0) {
            cachedExist = true;
            return true;
        } else {
            if (current == null) {
                cachedExist = false;
                return false;
            }
            if (element.indexOf('[') > 0) {
                String fieldName = StringUtils.substringBefore(element, "["); //$NON-NLS-1$
                if (!current.getType().hasField(fieldName)) {
                    cachedExist = false;
                    return false;
                }
                FieldMetadata field = current.getType().getField(fieldName);
                if (!field.isMany()) {
                    throw new IllegalStateException(
                            "Expected a repeatable field for '" + element + "' in path '" + path //$NON-NLS-1$ //$NON-NLS-2$
                                    + "'."); //$NON-NLS-1$
                }
                int indexStart = element.indexOf('[');
                int indexEnd = element.indexOf(']');
                if (indexStart < 0 || indexEnd < 0) {
                    throw new RuntimeException(
                            "Field name '" + element + "' did not match many field pattern in path '" //$NON-NLS-1$ //$NON-NLS-2$
                                    + path + "'."); //$NON-NLS-1$
                }
                int index = Integer.parseInt(element.substring(indexStart + 1, indexEnd)) - 1;
                List list = (List) current.get(field);
                if (list == null || index > list.size() - 1) {
                    cachedExist = false;
                    return false;
                }
                Object value = list.get(index);
                if (value instanceof DataRecord) {
                    current = (DataRecord) value;
                }
            } else {
                if (!current.getType().hasField(element) || current.get(element) == null) {
                    cachedExist = false;
                    return false;
                }
                FieldMetadata field = current.getType().getField(element);
                if (field instanceof ContainedTypeFieldMetadata) {
                    Object value = current.get(field);
                    if (value instanceof DataRecord) {
                        current = (DataRecord) value;
                    }
                }
            }
        }
    }
    cachedExist = true;
    return true;
}

From source file:com.hcc.cms.util.PageUtils.java

/**
 * This method is used to get the current locale of the page which would be
 * used in setting the path for driver profile page based on the current
 * locale./*from ww  w.j  a  v a2  s .c  o m*/
 *
 * @param pagePath
 *            - path of the page whose locale is to be figured out
 * @return currentLocale - of the current page.
 */
public static String getLocale(String pagePath) {

    for (int i = 0; i <= 2; i++) {
        pagePath = StringUtils.substringAfter(pagePath, SEPARATOR);
    }

    return StringUtils.substringBefore(pagePath, SEPARATOR);
}

From source file:de.extra.client.plugins.responseprocessplugin.filesystem.FileSystemResultPackageDataResponseProcessPlugin.java

/**
 * //from  www  .  j ava 2  s .co  m
 * Wenn IncomingFileName ber DataSource Plugin gesetzt ist , wird dieser
 * Name bernommen, sonst Erzeugt einen eindeitigen Filenamen mit
 * milissekunden und ResponseID.
 * 
 * @param responseId
 * @return
 */
private String buildFilename(final String incomingFileName, final String responseId) {
    final StringBuilder fileName = new StringBuilder();
    if (incomingFileName != null) {
        fileName.append(incomingFileName);
    } else {
        String cleanResponseId = FilenameUtils.normalize(responseId);
        cleanResponseId = StringUtils.substringBefore(cleanResponseId, " ");
        fileName.append("RESPONSE_").append(cleanResponseId);
        fileName.append("_").append(System.currentTimeMillis());
    }
    return fileName.toString();
}

From source file:com.opengamma.component.ComponentManager.java

/**
 * Intelligently sets the property which is a component reference.
 * <p>/*from   www  . ja  va2 s .  c  o  m*/
 * The double colon is used in the format {@code Type::Classifier}.
 * If the type is omitted, this method will try to infer it.
 * 
 * @param bean  the bean, not null
 * @param mp  the property, not null
 * @param value  the configured value containing double colon, not null
 */
protected void setPropertyComponentRef(Bean bean, MetaProperty<?> mp, String value) {
    Class<?> propertyType = mp.propertyType();
    String type = StringUtils.substringBefore(value, "::");
    String classifier = StringUtils.substringAfter(value, "::");
    if (type.length() == 0) {
        try {
            // infer type
            mp.set(bean, getRepository().getInstance(propertyType, classifier));
            return;
        } catch (RuntimeException ex) {
            throw new OpenGammaRuntimeException(
                    "Unable to set property " + mp + " of type " + propertyType.getName(), ex);
        }
    }
    ComponentInfo info = getRepository().findInfo(type, classifier);
    if (info == null) {
        throw new OpenGammaRuntimeException(
                "Unable to find component reference '" + value + "' while setting property " + mp);
    }
    if (ComponentInfo.class.isAssignableFrom(propertyType)) {
        mp.set(bean, info);
    } else {
        mp.set(bean, getRepository().getInstance(info));
    }
}