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:zipkin.autoconfigure.metrics.PrometheusMetricsAutoConfiguration.java

private String metricName(String name, String type) {
    switch (type) {
    case "counter":
        return name.replaceFirst("^counter_", "");
    case "gauge":
        return name.replaceFirst("^gauge_", "");
    default:/*from   w ww  . j av  a 2  s.c om*/
        return name;
    }
}

From source file:foam.nanos.blob.HttpBlobService.java

protected void download(X x) {
    OutputStream os = null;// ww w.  j  av a 2  s.co  m
    HttpServletRequest req = x.get(HttpServletRequest.class);
    HttpServletResponse resp = x.get(HttpServletResponse.class);

    try {
        String path = req.getRequestURI();
        String id = path.replaceFirst("/service/" + nspec_.getName() + "/", "");

        Blob blob = getDelegate().find(id);
        if (blob == null) {
            resp.setStatus(resp.SC_NOT_FOUND);
            return;
        }

        long size = blob.getSize();
        resp.setStatus(resp.SC_OK);
        if (blob instanceof FileBlob) {
            File file = ((FileBlob) blob).getFile();
            resp.setContentType(Files.probeContentType(Paths.get(file.toURI())));
        } else {
            resp.setContentType("application/octet-stream");
        }
        resp.setHeader("Content-Length", Long.toString(size, 10));
        resp.setHeader("ETag", id);
        resp.setHeader("Cache-Control", "public");

        os = resp.getOutputStream();
        blob.read(os, 0, size);
        os.close();
    } catch (Throwable t) {
        t.printStackTrace();
        throw new RuntimeException(t);
    } finally {
        IOUtils.closeQuietly(os);
    }
}

From source file:be.ac.ua.comp.scarletnebula.core.CloudProvider.java

/**
 * @return Returns a collection of all providernames that exist on disk.
 *//*from w w w  .  j a v  a2s . com*/
static Collection<String> getProviderNames() {
    final File dir = new File("providers");

    if (!dir.exists() || !dir.isDirectory()) {
        return new ArrayList<String>();
    }

    final Collection<String> files = Arrays.asList(dir.list());
    final Collection<String> rv = new ArrayList<String>(files.size());

    for (final String file : files) {
        rv.add(file.replaceFirst(".properties$", ""));
    }
    return rv;
}

From source file:com.ineunet.knife.persist.NamedParamJdbcOperator.java

/**
 * @param sql sql paramName sql. e.g. update user set name=:name where id=:id
 * @param args /*from   w w  w .j  a va 2 s . com*/
 * @param values valid args of sql
 * @return sql with '?'
 * @since 2.0.2
 */
@SuppressWarnings("unchecked")
static String processParameter(String sql, Map<String, Object> args, List<Object> values) {
    String sqlOther = sql;
    List<String> params = SqlStrUtils.getNamedParameters(sqlOther);
    for (String paramName : params) {
        String escaped = ExpressionStrUtils.escapeRegex$(paramName);
        Object value = args.get(paramName);
        Asserts.notNull(value, "Lack of parameter " + paramName);
        // transform list to array
        if (value instanceof List)
            value = ((List<Object>) value).toArray();
        if (value instanceof Object[]) {
            Object[] inValues = (Object[]) value;
            int valLength = inValues.length;
            // parameter is an array, check keyword 'in'
            if (sqlOther.matches(".+ +in *\\( *: *" + escaped + " *\\).*")) {
                if (valLength == 0) {
                    // parameter is empty, remove 'in(:..)'
                    sqlOther = sqlOther.replaceFirst("in *\\( *: *" + escaped + " *\\)", " is null and 1<1");
                } else {
                    StringBuilder sb = new StringBuilder("?");
                    values.add(inValues[0]);
                    for (int i = 1; i < valLength; i++) {
                        sb.append(",?");
                        values.add(inValues[i]);
                    }
                    sqlOther = sqlOther.replaceFirst(": *" + escaped, sb.toString());
                }
            }
        } else {
            sqlOther = sqlOther.replaceFirst(": *" + escaped, "?");
            values.add(value);
        }
    }
    if (Configs.isDevMode() && ConfigFactory.getKnifeConfig().get("sqllog", false)) {
        log.info("#SQL#: " + sqlOther);
    }
    return sqlOther;
}

From source file:net.sourceforge.subsonic.controller.M3UController.java

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setContentType("audio/x-mpegurl");
    response.setCharacterEncoding(StringUtil.ENCODING_UTF8);

    Player player = playerService.getPlayer(request, response);

    String url = request.getRequestURL().toString();
    url = url.replaceFirst("play.m3u.*", "stream?");

    // Rewrite URLs in case we're behind a proxy.
    if (settingsService.isRewriteUrlEnabled()) {
        String referer = request.getHeader("referer");
        url = StringUtil.rewriteUrl(url, referer);
    }/*from ww w  . ja v  a2 s  .c o m*/

    url = settingsService.rewriteRemoteUrl(url);

    if (player.isExternalWithPlaylist()) {
        createClientSidePlaylist(response.getWriter(), player, url);
    } else {
        createServerSidePlaylist(response.getWriter(), player, url);
    }
    return null;
}

From source file:br.com.munif.personalsecurity.aplicacao.autorizacao.GenericTokenApi.java

protected String[] getUrlParameters(HttpServletRequest request) {
    String paramters = request.getRequestURI().replaceFirst(url, "");
    paramters = paramters.replaceFirst("/", "");
    if (paramters.isEmpty()) {
        return new String[0];
    }/* w w w .j av a  2s  .  c om*/
    return paramters.split("/");
}

From source file:com.thoughtworks.go.util.command.UrlArgument.java

private String clean(String scheme, String userInfo) {
    if (userInfo.contains(":")) {
        return userInfo.replaceFirst(":.*", ":******");
    } else if ("ssh".equals(scheme) || "svn+ssh".equals(scheme)) {
        return userInfo;
    }//  ww w .j  ava  2 s . c  o m
    return "******";
}

From source file:com.boundary.sdk.event.notification.WebhookNotificationTest.java

@Test
public void testNotification() throws InterruptedException, IOException {
    String body = readFile(NOTIFICATION_JSON, Charset.defaultCharset());
    out.setExpectedMessageCount(1);//from   w w  w.  j a v a2  s.c  o m

    CamelContext context = context();
    RouteDefinition routeDefinition = context.getRouteDefinition("WEBHOOK-TEST");

    assertNotNull("RouteDefinition is null", routeDefinition);
    List<FromDefinition> inputs = routeDefinition.getInputs();
    FromDefinition from = inputs.get(0);
    String uri = from.getEndpointUri();
    uri = uri.replaceFirst("jetty:", "");
    LOG.debug("uri: {}", uri);

    // Send HTTP notification
    HttpClient httpclient = new HttpClient();
    PostMethod httppost = new PostMethod(uri);
    Header contentHeader = new Header("Content-Type", "application/json");
    httppost.setRequestHeader(contentHeader);
    StringRequestEntity reqEntity = new StringRequestEntity(body, null, null);
    httppost.setRequestEntity(reqEntity);
    int status = httpclient.executeMethod(httppost);

    assertEquals("Received wrong response status", 200, status);

    out.assertIsSatisfied();

    List<Exchange> exchanges = out.getExchanges();
    LOG.debug("EXCHANGE COUNT: {}", exchanges.size());
    for (Exchange exchange : exchanges) {
        Message message = exchange.getIn();
        String messageBody = message.getBody(String.class);
        Object o = message.getBody();
        LOG.debug("class: " + o.getClass().toString());
        LOG.debug("messageBody: " + messageBody);
        LOG.debug("id: " + exchange.getExchangeId());
        //assertEquals("Body not equal",body,messageBody);
    }
}

From source file:com.redhat.red.build.koji.model.xmlrpc.messages.AbstractKojiMessageTest.java

protected String formalizeXMLString(String xml) {
    xml = xml.replaceFirst("<\\?.*\\?>", "<?xml version=\"1.0\"?>");
    xml = xml.replaceAll("<nil/>", "<nil></nil>");
    return xml;/* w w w .  ja va 2s.  co  m*/
}