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

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

Introduction

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

Prototype

public static boolean startsWith(String str, String prefix) 

Source Link

Document

Check if a String starts with a specified prefix.

Usage

From source file:org.apache.usergrid.chop.webapp.view.tree.ModuleTreeBuilder.java

private static void onItemClick(ItemClickEvent event, ModuleSelectListener listener) {

    String id = (String) event.getItemId();
    boolean isModuleVersion = !StringUtils.startsWith(id, PARENT_PREFIX);

    if (isModuleVersion) {
        listener.onModuleSelect(id);// ww  w  .j a  v  a2 s.co m
    }
}

From source file:org.apereo.lap.services.ConfigurationService.java

/**
 * Verifies and creates the dir if needed (OR dies if impossible)
 * @param configKey the configured path//from   w w w  .  j  a  va  2  s.  co m
 * @param defaultPath the default path if the configured path is bad
 * @return the directory
 */
private File verifyDir(String configKey, String defaultPath) {
    String dirStr = this.config.getString(configKey);
    File fileDir;
    if (StringUtils.isBlank(dirStr)) {
        dirStr = defaultPath;
        fileDir = new File(appHome(), dirStr);
    } else {
        // check if relative or absolute path
        dirStr = StringUtils.trim(dirStr);
        if (StringUtils.startsWith(dirStr, SLASH)) {
            fileDir = new File(dirStr);
        } else {
            // this is a relative path
            fileDir = new File(appHome(), dirStr);
        }
    }
    if (!fileDir.exists()) {
        // try to create it
        try {
            if (fileDir.mkdirs()) {
                logger.info("Config created " + configKey + " dir: " + fileDir.getAbsolutePath());
            } else {
                throw new RuntimeException("Could not create dir at: " + fileDir.getAbsolutePath());
            }
        } catch (Exception e) {
            throw new RuntimeException(
                    "Failure trying to create dir at: " + fileDir.getAbsolutePath() + ": " + e);
        }
    } else if (!fileDir.isDirectory()) {
        throw new RuntimeException("Configured pipeline path is not a directory: " + fileDir.getAbsolutePath());
    } else if (!fileDir.canRead()) {
        throw new RuntimeException("Configured pipeline path is not readable: " + fileDir.getAbsolutePath());
    } else {
        logger.info("Config using existing " + configKey + " dir: " + fileDir.getAbsolutePath());
    }
    // update config with absolute path
    this.config.setProperty(configKey, fileDir.getAbsolutePath());
    return fileDir;
}

From source file:org.apereo.lap.services.storage.mongo.MongoMultiTenantFilter.java

@Override
public void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain fc)
        throws ServletException, IOException {
    logger.debug("applying MongoMultiTenantFilter");
    logger.debug("allow defaultDatabase: " + useDefaultDatabaseName);

    // For now header trumps all sources        
    String tenant = req.getHeader("X-LAP-TENANT");

    if (StringUtils.isBlank(tenant)) {
        // Next try path
        String requestURI = req.getRequestURI();
        if (StringUtils.isBlank(tenant) && StringUtils.isNotBlank(requestURI)
                && StringUtils.startsWith(requestURI, "/api/output/")) {
            tenant = StringUtils.substringBetween(requestURI, "/api/output/", "/");
        }//from  ww w . ja va  2 s . co m

        if (StringUtils.isBlank(tenant)) {
            // Next try session
            tenant = tenantService.getTenant();

            // If still blank and a default db is allowed
            if (StringUtils.isBlank(tenant)) {
                if (Boolean.valueOf(useDefaultDatabaseName)) {
                    logger.warn("No tenant available in request. Using default database.");
                    tenant = defaultDatabase;
                } else {
                    throw new MissingTenantException(
                            "No tenant available in request and default database disabled.");
                }
            }
        }
    }

    tenantService.setTenant(tenant);
    logger.debug("Using tenant {}", tenantService.getTenant());

    fc.doFilter(req, res);
}

From source file:org.b3log.solo.processor.BlogProcessorTestCase.java

/**
 * getBlogInfo.//from   ww  w.  j  a  va 2  s. c  o m
 *
 * @throws Exception exception
 */
@Test(dependsOnMethods = "init")
public void getBlogInfo() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/blog/info");
    when(request.getMethod()).thenReturn("GET");

    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();

    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);

    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);

    dispatcherServlet.service(request, response);

    final String content = stringWriter.toString();
    Assert.assertTrue(StringUtils.startsWith(content, "{\"staticServePath\":\"http://localhost:8080\""));
}

From source file:org.b3log.solo.processor.BlogProcessorTestCase.java

/**
 * getArticlesTags.//from   ww  w .  ja  v  a 2  s . c om
 *
 * @throws Exception exception
 */
@Test(dependsOnMethods = "init")
public void getArticlesTags() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/blog/articles-tags");
    when(request.getParameter("pwd")).thenReturn("pass");
    when(request.getMethod()).thenReturn("GET");

    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();

    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);

    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);

    dispatcherServlet.service(request, response);

    final String content = stringWriter.toString();
    Assert.assertTrue(StringUtils.startsWith(content, "{\"data\":"));
}

From source file:org.b3log.solo.processor.BlogProcessorTestCase.java

/**
 * getInterestTags.// w  w w.  j  a v  a  2  s  .  com
 *
 * @throws Exception exception
 */
@Test(dependsOnMethods = "init")
public void getInterestTags() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/blog/interest-tags");
    when(request.getMethod()).thenReturn("GET");

    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();

    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);

    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);

    dispatcherServlet.service(request, response);

    final String content = stringWriter.toString();
    Assert.assertTrue(StringUtils.startsWith(content, "{\"data\":"));
}

From source file:org.b3log.solo.processor.CaptchaProcessorTestCase.java

/**
 * get.//from   ww w  . j  a v  a2s . com
 *
 * @throws Exception exception
 */
@Test(dependsOnMethods = "init")
public void get() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/captcha.do");
    when(request.getMethod()).thenReturn("GET");

    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();

    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getOutputStream()).thenReturn(new ServletOutputStream() {

        private long size;

        @Override
        public boolean isReady() {
            return true;
        }

        @Override
        public void setWriteListener(final WriteListener writeListener) {
        }

        @Override
        public void write(int b) throws IOException {
            size++;
        }

        @Override
        public String toString() {
            return "size: " + String.valueOf(size);
        }
    });

    dispatcherServlet.service(request, response);

    Assert.assertTrue(StringUtils.startsWith(response.getOutputStream().toString(), "size: "));
}

From source file:org.b3log.solo.processor.FeedProcessorTestCase.java

/**
 * blogArticlesAtom./*from   w  ww . ja v  a 2s. c  o  m*/
 *
 * @throws Exception exception
 */
@Test(dependsOnMethods = "init")
public void blogArticlesAtom() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/blog-articles-feed.do");
    when(request.getMethod()).thenReturn("GET");

    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();

    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);

    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);

    dispatcherServlet.service(request, response);

    final String content = stringWriter.toString();
    Assert.assertTrue(StringUtils.startsWith(content, "<?xml version=\"1.0\"?>"));
}

From source file:org.b3log.solo.processor.FeedProcessorTestCase.java

/**
 * tagArticlesAtom./*from  w ww .j a  v a2s  . c  om*/
 *
 * @throws Exception exception
 */
@Test(dependsOnMethods = "init")
public void tagArticlesAtom() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/tag-articles-feed.do");
    when(request.getMethod()).thenReturn("GET");

    final JSONObject tag = getTagRepository().get(new Query()).optJSONArray(Keys.RESULTS).optJSONObject(0);
    when(request.getQueryString()).thenReturn("tag=" + tag.optString(Keys.OBJECT_ID));

    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();

    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);

    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);

    dispatcherServlet.service(request, response);

    final String content = stringWriter.toString();
    Assert.assertTrue(StringUtils.startsWith(content, "<?xml version=\"1.0\""));
}

From source file:org.b3log.solo.processor.FeedProcessorTestCase.java

/**
 * blogArticlesRSS.// w ww  .  java 2  s.  co m
 *
 * @throws Exception exception
 */
@Test(dependsOnMethods = "init")
public void blogArticlesRSS() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/blog-articles-rss.do");
    when(request.getMethod()).thenReturn("GET");
    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();

    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);

    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);

    dispatcherServlet.service(request, response);

    final String content = stringWriter.toString();
    Assert.assertTrue(StringUtils.startsWith(content, "<?xml version=\"1.0\""));
}