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.kawanfw.file.api.util.client.FileChunkStore.java

/**
 * Cleans all references of username/*from  w  ww  .  ja va2s.  c  o m*/
 * 
 * @param username
 *            the username to clean all references for
 */
public static void clean(String username) {

    if (username == null) {
        throw new IllegalArgumentException("username is null!");
    }

    if (mapFiles == null || mapFiles.isEmpty()) {
        return;
    }

    Set<String> keys = mapFiles.keySet();

    // Duplicate the Set otherwise we will have a
    // java.util.ConcurrentModificationException...
    Set<String> keysDuplicate = new HashSet<String>();
    keysDuplicate.addAll(keys);

    for (Iterator<String> iterator = keysDuplicate.iterator(); iterator.hasNext();) {
        String key = iterator.next();
        String usernameKey = StringUtils.substringBefore(key, KAWANFW_SEP);

        if (username.equals(usernameKey)) {
            mapFiles.remove(username);
        }

    }
}

From source file:org.kawanfw.file.api.util.client.RemoteFilePartStore.java

/**
 * Cleans all references of username//from www .  j  a v  a  2  s  .  c o m
 * @param username the username to clean all references for
 */
public static void clean(String username) {

    if (username == null) {
        throw new IllegalArgumentException("username is null!");
    }

    if (mapFiles == null || mapFiles.isEmpty()) {
        return;
    }

    Set<String> keys = mapFiles.keySet();

    // Duplicate the Set otherwise we will have a java.util.ConcurrentModificationException...
    Set<String> keysDuplicate = new HashSet<String>();
    keysDuplicate.addAll(keys);

    for (Iterator<String> iterator = keysDuplicate.iterator(); iterator.hasNext();) {
        String key = iterator.next();
        String usernameKey = StringUtils.substringBefore(key, KAWANFW_SEP);

        if (username.equals(usernameKey)) {
            mapFiles.remove(username);
        }

    }
}

From source file:org.kawanfw.file.reflection.ClassFileLocatorNew.java

/**
 * Gets the container of the class, ie the .jar file or the .class file
 * @return the container of the class, ie the .jar file or the .class file
 * @throws IOException/*from www .  ja va 2s.  co m*/
 */
public File getContainerFile() throws IOException {

    if (isContainerJar()) {

        //url : jar:file:/I:/_dev_awake/awake-file-3.0/lib/stringtemplate.jar!/org/antlr/stringtemplate/StringTemplate.class
        URL url = getUrl();
        String urlString = url.toString();

        urlString = StringUtils.substringBefore(urlString, "!");
        urlString = StringUtils.substringAfter(urlString, "jar:");

        File file = null;

        try {
            file = new File(new URL(urlString).toURI());
        } catch (URISyntaxException e) {
            throw new IOException(e);
        }
        return file;
    } else {
        return getDotClassFile();
    }
}

From source file:org.kawanfw.file.servlet.ServerUserThrowable.java

/**
 * Return the class where the Exception has been thrown
 * //  ww w .  j a  v  a2 s .com
 * @param throwable
 *            the exception thrown
 * @return the class that throws the Exception
 */
private static Class<?> extractThrowingClassFromThrowable(Throwable throwable) {

    try {
        String statckTrace = ExceptionUtils.getStackTrace(throwable);
        String className = StringUtils.substringAfter(statckTrace, "at ");
        className = StringUtils.substringBefore(className, "(");
        className = StringUtils.substringBeforeLast(className, ".");

        debug("className: " + ":" + className + ":");

        Class<?> theClass = Class.forName(className);
        return theClass;
    } catch (ClassNotFoundException e1) {
        e1.printStackTrace(System.out);
        return null;
    }

}

From source file:org.kawanfw.file.servlet.ServerUserThrowable.java

/**
 * Return the class where the Exception has been thrown
 * /*w ww .  jav  a  2  s .  c  o  m*/
 * @param e
 *            the exception thrown
 * @return the class that throws the Exception
 */
private static String extractThrowingMethodNameFromException(Throwable e) {

    String statckTrace = ExceptionUtils.getStackTrace(e);
    String className = StringUtils.substringAfter(statckTrace, "at ");
    className = StringUtils.substringBefore(className, "(");

    String methodName = StringUtils.substringAfterLast(className, ".");

    debug("className : " + ":" + className + ":");
    debug("methodName: " + ":" + methodName + ":");

    return methodName;

}

From source file:org.kawanfw.file.test.parms.ProxyLoader.java

public Proxy getProxy() throws IOException, URISyntaxException {
    if (FrameworkSystemUtil.isAndroid()) {
        return null;
    }/*from w  w  w.  java 2  s .  com*/

    System.setProperty("java.net.useSystemProxies", "true");
    List<Proxy> proxies = ProxySelector.getDefault().select(new URI("http://www.google.com/"));

    if (proxies != null && proxies.size() >= 1) {

        System.out.println("Proxy in use: " + proxies.get(0));

        if (proxies.get(0).type().equals(Proxy.Type.DIRECT)) {
            return null;
        }

        System.out.println("Loading proxy file info...");
        // System.setProperty("java.net.useSystemProxies", "false");
        File file = new File(NEOTUNNEL_TXT);
        if (file.exists()) {
            String proxyValues = FileUtils.readFileToString(file);
            String username = StringUtils.substringBefore(proxyValues, " ");
            String password = StringUtils.substringAfter(proxyValues, " ");

            //      proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
            //         "127.0.0.1", 8080));
            proxy = proxies.get(0);

            passwordAuthentication = new PasswordAuthentication(username, password.toCharArray());

            System.out.println("USING PROXY WITH AUTHENTICATION: " + proxy + " / " + username + "-" + password);
        } else {
            throw new FileNotFoundException("proxy values not found. No file " + file);
        }
    }

    return proxy;
}

From source file:org.kawanfw.file.test.parms.TestParms.java

/**
 * Build the server root file from info dynamically stored in FILE_CONFIGURATOR_TXT
 * @return//w  ww .  j a v  a  2s.c om
 */
public static String getServerRootFromFile() {

    try {
        String content = FileUtils.readFileToString(FILE_CONFIGURATOR_TXT);

        if (content == null || content.equals("null")) {
            return "c:\\";
        }

        String root = StringUtils.substringBefore(content, "!");
        String withUsername = StringUtils.substringAfter(content, "!");

        if (withUsername.equals("true")) {
            return root + File.separator + "username" + File.separator;
        } else {
            return root + File.separator;
        }

    } catch (IOException e) {

        e.printStackTrace();
    }

    return null;
}

From source file:org.kawanfw.sql.api.client.RemoteDriver.java

/**
 * Attempts to make a database connection to the given URL.
 * /*from  w  w w .  j  a  v a  2  s.  c om*/
 * The driver will return "null" if it realizes it is the wrong kind of
 * driver to connect to the given URL. {@link #acceptsURL} will return null.
 * 
 * <P>
 * The driver will throw<code>SQLException</code> if it is the right driver
 * to connect to the given URL but has trouble connecting to the database.
 * 
 * <P>
 * The <code>java.util.Properties</code> argument can be used to pass
 * arbitrary string tag/value pairs as connection arguments. At least "user"
 * and "password" properties should be included in the
 * <code>Properties</code> object.
 * 
 * @param url
 *            the URL of the database to which to connect
 * @param info
 *            a list of arbitrary string tag/value pairs as connection
 *            arguments. At least a "user" and "password" property should be
 *            included.
 * @return a <code>Connection</code> object that represents a connection to
 *         the URL
 * @exception SQLException
 *                if a database access error occurs
 */
@Override
public Connection connect(String url, Properties info) throws SQLException {

    if (url == null) {
        throw new SQLException("url not set. Please provide an url.");
    }

    if (!acceptsURL(url)) {
        return null;
    }

    Properties info2 = new Properties();
    RemoteDriverUtil.copyProperties(info, info2);

    // Properties may be passed in url
    if (url.contains("?")) {
        String query = StringUtils.substringAfter(url, "?");
        Map<String, String> mapProps = RemoteDriverUtil.getQueryMap(query);

        Set<String> set = mapProps.keySet();
        for (String propName : set) {
            info2.setProperty(propName, mapProps.get(propName));
        }

        url = StringUtils.substringBefore(url, "?");
    }

    String username = info2.getProperty("user");
    String password = info2.getProperty("password");

    if (username == null) {
        throw new SQLException("user not set. Please provide a user.");
    }

    if (password == null) {
        throw new SQLException("password not set. Please provide a password.");
    }

    // Add proxy lookup
    String proxyType = info2.getProperty("proxyType");
    String proxyHostname = info2.getProperty("proxyHostname");
    String proxyPort = info2.getProperty("proxyPort");
    String proxyUsername = info2.getProperty("proxyUsername");
    String proxyPassword = info2.getProperty("proxyPassword");

    String statelessMode = info2.getProperty("statelessMode");
    String joinResultSetMetaData = info2.getProperty("joinResultSetMetaData");

    String zipResultSet = info2.getProperty("zipResultSet");

    int port = -1;

    Proxy proxy = null;

    if (proxyHostname != null) {
        try {
            port = Integer.parseInt(proxyPort);
        } catch (NumberFormatException e) {
            throw new SQLException("Invalid proxy port. Port is not numeric: " + proxyPort);
        }

        if (proxyType == null) {
            proxyType = "HTTP";
        }

        proxy = new Proxy(Type.valueOf(proxyType), new InetSocketAddress(proxyHostname, port));
    }

    boolean statelessModeBoolean = Boolean.parseBoolean(statelessMode);

    SessionParameters sessionParameters = getSessionParameters(info2);

    debug(sessionParameters.toString());

    // if (url.startsWith("jdbc:kawanfw://")) {
    // url = url.replace("jdbc:kawanfw", "http");
    // }

    // If we have passed the "proxy" property, build back the
    // instance from the property value
    // 1) Treat the case the user did a property.put(proxy) instead of
    // property.setProperty(proxy.toString())

    if (proxy == null) {
        Object objProxy = info2.get("proxy");
        if (objProxy != null && objProxy instanceof Proxy) {
            proxy = (Proxy) proxy;
        }
        // 2) Treat the case the user as correctly used
        // property.setProperty(httpProxy.toString())
        else {
            String proxyStr = info2.getProperty("proxy");
            debug("proxyStr:" + proxyStr);
            if (proxyStr != null) {
                proxy = RemoteDriverUtil.buildProxy(proxyStr);
            }
        }
    }

    // If we have passed the "sessionParameters" property, build back
    // the
    // instance from the property value
    // 1) Treat the case the user did a property.put(sessionParameters)
    // instead of property.setProperty(sessionParameters.toString())
    Object objSessionParameters = info2.get("sessionParameters");
    if (objSessionParameters != null && objSessionParameters instanceof SessionParameters) {
        String jsonString = SessionParametersGson.toJson((SessionParameters) (objSessionParameters));

        if (jsonString != null) {
            sessionParameters = SessionParametersGson.fromJson(jsonString);
        }
    }
    // 2) Treat the case the user as correctly used
    // property.setProperty(sessionParameters.toString())
    else {
        String jsonString = info2.getProperty("sessionParameters");
        if (jsonString != null) {
            sessionParameters = SessionParametersGson.fromJson(jsonString);
        }
    }

    debug("url                   : " + url);
    debug("Proxy                 : " + proxy);
    debug("sessionParameters: " + sessionParameters);

    boolean doJoinResultSetMetaData = false;

    if (joinResultSetMetaData != null) {
        doJoinResultSetMetaData = Boolean.parseBoolean(joinResultSetMetaData);
        debug("joinResultSetMetaData: " + doJoinResultSetMetaData);
    }

    PasswordAuthentication passwordAuthentication = null;

    if (proxy != null && proxyUsername != null) {
        passwordAuthentication = new PasswordAuthentication(proxyUsername, proxyPassword.toCharArray());
    }

    boolean doZipResultSet = true;

    if (zipResultSet != null) {
        doZipResultSet = Boolean.parseBoolean(zipResultSet);
        debug("zipResultSet: " + doZipResultSet);
    }

    Connection connection = new RemoteConnection(url, username, password.toCharArray(), proxy,
            passwordAuthentication, sessionParameters, statelessModeBoolean, doJoinResultSetMetaData,
            doZipResultSet);

    return connection;
}

From source file:org.kawanfw.sql.api.client.RemoteDriverUtil.java

/**
 * Create a Proxy instance from a Proxy.toString() representation 
 * Example: HTTP @ www.kawansoft.com/195.154.226.82:8080
 * @param proxyToString   the proxy in Proxy.toString() format
 * @return the build proxy/*from  w w  w. j a v a 2  s .  com*/
 */

public static Proxy buildProxy(String proxyToString) {
    if (proxyToString == null) {
        throw new NullPointerException("ip is null!");
    }

    if (!proxyToString.contains(" @ ")) {
        throw new IllegalArgumentException("Malformed Proxy.toString() format: @ separator is missing.");
    }

    // Get the type
    String type = StringUtils.substringBefore(proxyToString, " @ ");
    if (type == null) {
        throw new IllegalArgumentException("Malformed Proxy.toString() format: no Type");
    }

    type = type.trim();

    debug("Proxy.Type.DIRECT: " + Proxy.Type.DIRECT.toString());

    if (!type.equals(Proxy.Type.DIRECT.toString()) && !type.equals(Proxy.Type.HTTP.toString())
            && !type.equals(Proxy.Type.SOCKS.toString())) {
        throw new IllegalArgumentException(
                "Malformed Proxy.toString() format: Type does not contain DIRECT / HTTP / SOCKS: " + type
                        + ":");
    }

    String hostname = null;
    String portStr = null;
    int port = 0;

    if (proxyToString.contains("@ /")) {
        // Case 1 : HTTP @ /195.154.226.82:8080
        // no hostname IP only
        hostname = StringUtils.substringBetween(proxyToString, "/", ":");
    } else {
        // Case 2 : HTTP @ localhost/127.0.0.1:8080
        // hostname followed by ip or /subaddress
        hostname = StringUtils.substringBetween(proxyToString, " @ ", "/");
        String ip = StringUtils.substringBetween(proxyToString, "/", ":");

        // if ip string is in IP format, dont take in account the ip after /
        // If not, following / is the hostname
        if (validateIpFormat(ip)) {
            hostname = StringUtils.substringBetween(proxyToString, " @ ", "/");
        } else {
            hostname = StringUtils.substringBetween(proxyToString, " @ ", ":");
        }

    }

    portStr = StringUtils.substringAfter(proxyToString, ":");

    if (StringUtils.isNumeric(portStr)) {
        port = Integer.parseInt(portStr);
    } else {
        throw new IllegalArgumentException(
                "Malformed Proxy.toString() format: does not contain numeric port: " + proxyToString);
    }

    Proxy proxy = new Proxy(Type.valueOf(type), new InetSocketAddress(hostname, port));
    return proxy;
}

From source file:org.kawanfw.sql.api.server.StatementAnalyser.java

/**
 * Constructor./*from   www . ja  va  2  s. c o  m*/
 * 
 * @param sql
 *            the string content of the SQL statement.
 * @param parameterValues
 *            the parameter values of a prepared statement in the natural
 *            order, empty list for a (non prepared) statement
 */
public StatementAnalyser(String sql, List<Object> parameterValues) {

    if (sql == null) {
        throw new IllegalArgumentException(Tag.PRODUCT_PRODUCT_FAIL + "sql can not be null!");
    }

    if (parameterValues == null) {
        throw new IllegalArgumentException(Tag.PRODUCT_PRODUCT_FAIL + "parameterValues can not be null!");
    }

    sql = sql.trim();

    // Remove last ";" because may cause a problem for getting table
    // name with getTableNameFromDmlStatement()

    sql = removeTrailingSemicolons(sql);

    this.sql = sql;
    this.statementType = StringUtils.substringBefore(this.sql, BLANK);

    this.parameterValues = parameterValues;
}