Example usage for java.lang String replaceFirst

List of usage examples for java.lang String replaceFirst

Introduction

In this page you can find the example usage for java.lang String replaceFirst.

Prototype

public String replaceFirst(String regex, String replacement) 

Source Link

Document

Replaces the first substring of this string that matches the given regular expression with the given replacement.

Usage

From source file:com.centurylink.mdw.util.AuthUtils.java

/**
 * @return true if no authentication at all or authentication is successful
 *///from   www.ja  v a 2s . c  o  m
public static boolean checkBasicAuthenticationHeader(Map<String, String> headers) {

    String authorizationHeader = headers.get(Listener.AUTHORIZATION_HEADER_NAME);
    if (authorizationHeader == null)
        authorizationHeader = headers.get(Listener.AUTHORIZATION_HEADER_NAME.toLowerCase());

    if (authorizationHeader != null) {
        authorizationHeader = authorizationHeader.replaceFirst("Basic ", "");

        byte[] valueDecoded = Base64.decodeBase64(authorizationHeader.getBytes());
        authorizationHeader = new String(valueDecoded);

        String[] creds = authorizationHeader.split(":");
        String user = creds[0];
        String pass = creds[1];

        try {
            if (AuthConstants.getOAuthTokenLocation() != null) {
                oauthAuthenticate(user, pass);
            } else {
                ldapAuthenticate(user, pass);
            }

            /**
             * Authentication passed so take credentials out
             * of the metadata and just put user name in.
             */
            headers.remove(Listener.AUTHORIZATION_HEADER_NAME);
            headers.remove(Listener.AUTHORIZATION_HEADER_NAME.toLowerCase());
            headers.put(Listener.AUTHENTICATED_USER_HEADER, user);
            if (logger.isDebugEnabled()) {
                logger.debug("authentication successful for user '" + user + "'");
            }
        } catch (Exception ex) {
            headers.remove(Listener.AUTHORIZATION_HEADER_NAME);
            headers.remove(Listener.AUTHORIZATION_HEADER_NAME.toLowerCase());
            headers.put(Listener.AUTHENTICATION_FAILED,
                    "Authentication failed for '" + user + "' " + ex.getMessage());
            logger.severeException("Authentication failed for user '" + user + "'" + ex.getMessage(), ex);
            return false;
        }
    }
    return true;
}

From source file:ambroafb.general.AnnotiationUtils.java

private static boolean checkValidationForContentISOAnnotation(Field field, Object currSceneController) {
    boolean contentIsCorrect = false;
    Annotations.ContentISO annotation = field.getAnnotation(Annotations.ContentISO.class);
    Object[] typeAndContent = getNodesTypeAndContent(field, currSceneController);

    if (!Pattern.matches(annotation.value(), (String) typeAndContent[1])) {
        String explainFromBundle = GeneralConfig.getInstance().getTitleFor(annotation.explain());
        String explain = explainFromBundle.replaceFirst("#", "" + Annotations.ContentISO.isoLen);
        changeNodeTitleLabelVisual((Node) typeAndContent[0], explain);
    } else {//from   w ww. j  a va 2s  .  c o m
        changeNodeTitleLabelVisual((Node) typeAndContent[0], "");
        contentIsCorrect = true;
    }
    return contentIsCorrect;
}

From source file:Main.java

private static String justifyOperation(String s, float width, Paint p) {
    float holder = (float) (COMPLEXITY * Math.random());
    while (s.contains(Float.toString(holder))) {
        holder = (float) (COMPLEXITY * Math.random());
    }/* w ww .j av a2s.com*/
    String holder_string = Float.toString(holder);
    float lessThan = width;
    int timeOut = 100;
    int current = 0;
    while ((p.measureText(s) < lessThan) && (current < timeOut)) {
        s = s.replaceFirst(" ([^" + holder_string + "])", " " + holder_string + "$1");
        lessThan = (p.measureText(holder_string) + lessThan) - p.measureText(" ");
        current++;
    }
    String cleaned = s.replaceAll(holder_string, " ");
    return cleaned;
}

From source file:Commander.Main.java

private static List<String> getOptions(String string) {
    List<String> options = new ArrayList<>();
    if (string.startsWith(" ")) {
        string = string.replaceFirst(" ", "");
    }//from  w ww .j  ava 2  s  . c om
    while (string.startsWith("/")) {
        int nextIndex = string.indexOf(" ");
        String option = string.substring(0, nextIndex);
        options.add(option);
        string = string.substring(nextIndex, string.length());
        if (string.startsWith(" ")) {
            string = string.replaceFirst(" ", "");
        }
    }
    //Add rest of the string to the list
    options.add(string);
    return options;
}

From source file:com.nimbits.MainClass.java

private static void processArgs(final String[] args, final Map<String, String> argsMap) {
    for (String s : args) {

        String[] a = s.split("=");
        if (a.length == 2) {
            argsMap.put(a[0].replaceFirst("-", "").toLowerCase(), a[1].trim());
        } else {/*ww w  .  j  a  v a2s . co  m*/
            argsMap.put(s.replaceFirst("-", "").toLowerCase(), "");
        }

    }
}

From source file:io.confluent.kafka.schemaregistry.client.rest.RestService.java

static String buildRequestUrl(String baseUrl, String path) {
    // Join base URL and path, collapsing any duplicate forward slash delimiters
    return baseUrl.replaceFirst("/$", "") + "/" + path.replaceFirst("^/", "");
}

From source file:org.gytheio.messaging.amqp.AmqpDirectEndpoint.java

private static Destination getDestination(Session session, String endpoint) throws JMSException {
    Destination destination = null;
    if (endpoint.startsWith(ENDPOINT_PREFIX_QUEUE)) {
        destination = session.createQueue(endpoint.replaceFirst(ENDPOINT_PREFIX_QUEUE, ""));
    } else if (endpoint.startsWith(ENDPOINT_PREFIX_TOPIC)) {
        destination = session.createTopic(endpoint.replaceFirst(ENDPOINT_PREFIX_TOPIC, ""));
    } else {/* ww  w. j  a  v  a2  s  . c  o  m*/
        destination = session.createQueue(endpoint);
    }
    return destination;
}

From source file:ca.sqlpower.testutil.TestUtils.java

/**
 * Returns the set of property names which have both a getter and a setter
 * method and are annotated to be persisted through the {@link SPPersister}
 * classes./*from   w  w  w . j a va 2 s. c o m*/
 * 
 * @param objectUnderTest
 *            The object that contains the persistable properties we want to
 *            find.
 * @param includeTransient
 *            If true the properties marked as transient will also be
 *            included. If false only the properties that are persisted and
 *            not transient are returned.
 * @param includeConstructorMutators
 *            If true the properties that have getters but can only be set
 *            through a constructor due to being final will be included. If
 *            false the persisted properties provided must have a setter.
 */
public static Set<String> findPersistableBeanProperties(SPObject objectUnderTest, boolean includeTransient,
        boolean includeConstructorMutators) throws Exception {
    Set<String> getters = new HashSet<String>();
    Set<String> setters = new HashSet<String>();
    for (Method m : objectUnderTest.getClass().getMethods()) {
        if (m.getName().equals("getClass"))
            continue;

        //skip non-public methods as they are not visible for persisting anyways.
        if (!Modifier.isPublic(m.getModifiers()))
            continue;
        //skip static methods
        if (Modifier.isStatic(m.getModifiers()))
            continue;

        if (m.getName().startsWith("get") || m.getName().startsWith("is")) {
            Class<?> parentClass = objectUnderTest.getClass();
            boolean accessor = false;
            boolean ignored = false;
            boolean isTransient = false;
            parentClass.getMethod(m.getName(), m.getParameterTypes());//test
            while (parentClass != null) {
                Method parentMethod;
                try {
                    parentMethod = parentClass.getMethod(m.getName(), m.getParameterTypes());
                } catch (NoSuchMethodException e) {
                    parentClass = parentClass.getSuperclass();
                    continue;
                }
                if (parentMethod.getAnnotation(Accessor.class) != null) {
                    accessor = true;
                    if (parentMethod.getAnnotation(Transient.class) != null) {
                        isTransient = true;
                    }
                    break;
                } else if (parentMethod.getAnnotation(NonProperty.class) != null
                        || parentMethod.getAnnotation(NonBound.class) != null) {
                    ignored = true;
                    break;
                }
                parentClass = parentClass.getSuperclass();
            }
            if (accessor) {
                if (includeTransient || !isTransient) {
                    if (m.getName().startsWith("get")) {
                        getters.add(m.getName().substring(3));
                    } else if (m.getName().startsWith("is")) {
                        getters.add(m.getName().substring(2));
                    }
                }
            } else if (ignored) {
                //ignored so skip
            } else {
                fail("The method " + m.getName() + " of " + objectUnderTest.toString()
                        + " is a getter that is not annotated "
                        + "to be an accessor or transient. The exiting annotations are "
                        + Arrays.toString(m.getAnnotations()));
            }
        } else if (m.getName().startsWith("set")) {
            if (m.getAnnotation(Mutator.class) != null) {
                if ((includeTransient || m.getAnnotation(Transient.class) == null)
                        && (includeConstructorMutators
                                || !m.getAnnotation(Mutator.class).constructorMutator())) {
                    setters.add(m.getName().substring(3));
                }
            } else if (m.getAnnotation(NonProperty.class) != null || m.getAnnotation(NonBound.class) != null) {
                //ignored so skip and pass
            } else {
                fail("The method " + m.getName() + " is a setter that is not annotated "
                        + "to be a mutator or transient.");
            }
        }
    }

    Set<String> beanNames = new HashSet<String>();
    for (String beanName : getters) {
        if (setters.contains(beanName)) {
            String firstLetter = new String(new char[] { beanName.charAt(0) });
            beanNames.add(beanName.replaceFirst(firstLetter, firstLetter.toLowerCase()));
        }
    }
    return beanNames;
}

From source file:lux.index.XmlIndexer.java

private static String normalizeUri(String uri) {
    String path = uri.replaceFirst("^\\w+:/+", "/"); // strip the scheme part (file:/, lux:/, etc), if any
    path = path.replace('\\', '/');
    return path;/*  ww w  .  j a  va  2 s.c  om*/
}

From source file:autocorrelator.apps.SDFGroovy.java

private static String fileToString(String fName) throws IOException {
    InputStream in = new BufferedInputStream(new FileInputStream(fName));
    StringBuilder sb = new StringBuilder(2000);
    byte[] buf = new byte[2048];

    int len;//from w  ww  . j a  v a 2 s .c om
    while ((len = in.read(buf)) > -1)
        sb.append(new String(buf, 0, len));

    in.close();
    String grvy = sb.toString();
    // deal with unix #! execution option
    while (grvy.startsWith("#"))
        grvy = grvy.replaceFirst("^[^\n\r]*[\n\r]+", "");

    return grvy;
}