Example usage for org.apache.commons.lang3 StringUtils substringBeforeLast

List of usage examples for org.apache.commons.lang3 StringUtils substringBeforeLast

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils substringBeforeLast.

Prototype

public static String substringBeforeLast(final String str, final String separator) 

Source Link

Document

Gets the substring before the last occurrence of a separator.

Usage

From source file:org.structr.schema.SchemaHelper.java

private static String stem(final String term) {

    String lastWord;//  w w w  . j a  va 2  s. co  m
    String begin = "";

    if (StringUtils.contains(term, WORD_SEPARATOR)) {

        lastWord = StringUtils.substringAfterLast(term, WORD_SEPARATOR);
        begin = StringUtils.substringBeforeLast(term, WORD_SEPARATOR);

    } else {

        lastWord = term;

    }

    lastWord = PlingStemmer.stem(lastWord);

    return begin.concat(WORD_SEPARATOR).concat(lastWord);

}

From source file:org.structr.web.advanced.UserSelfRegistrationTest.java

@Test
public void testUserSelfRegistrationWithRedirect() {

    // since we cannot test the mail confirmation workflow, we just disable sending an e-mail
    Settings.SmtpTesting.setValue(true);

    // enable self-registration and auto-login
    Settings.RestUserAutocreate.setValue(true);
    Settings.RestUserAutologin.setValue(true);

    final SessionFilter sessionFilter = new SessionFilter();
    final String eMail = "test@structr.com";
    String id = null;/*from  w  ww  .j av  a 2 s.c  o  m*/
    String confKey = null;

    // switch to REST servlet
    RestAssured.basePath = restUrl;

    grant("_registration", UiAuthenticator.NON_AUTH_USER_POST, true);
    grant("_login", UiAuthenticator.NON_AUTH_USER_POST, false);

    // verify self registration
    RestAssured.given().filter(sessionFilter).body("{ name: '" + eMail + "',  eMail: '" + eMail + "' }")
            .expect().statusCode(201).when().post("/registration");

    try (final Tx tx = app.tx()) {

        final User user = app.nodeQuery(User.class).getFirst();

        assertNotNull("User was not created", user);

        // store ID for later user
        id = user.getProperty(StructrApp.key(User.class, "id"));
        confKey = user.getProperty(StructrApp.key(User.class, "confirmationKey"));

        assertNotNull("Confirmation key was not set", confKey);

        tx.success();

    } catch (FrameworkException t) {
        fail("Unexpected exception.");
    }

    // create redirect page
    try (final Tx tx = app.tx()) {

        makeVisible(Page.createSimplePage(securityContext, "error"), true);
        makeVisible(Page.createSimplePage(securityContext, "success"), false);

        tx.success();

    } catch (FrameworkException fex) {
    }

    // switch to HTML servlet
    RestAssured.basePath = htmlUrl;

    // expect 404 Not Found when logging in because Jetty or
    // RestAssured don't preserve the session ID
    RestAssured.given().filter(sessionFilter).param(HtmlServlet.CONFIRM_KEY_KEY, confKey)
            .param(HtmlServlet.TARGET_PAGE_KEY, "success").expect().statusCode(200)
            .body("html.head.title", Matchers.equalTo("Success"))
            .body("html.body.h1", Matchers.equalTo("Success"))
            .body("html.body.div", Matchers.equalTo("Initial body text")).when()
            .get(HtmlServlet.CONFIRM_REGISTRATION_PAGE);

    // verify that the user has no confirmation key
    try (final Tx tx = app.tx()) {

        final User user = app.nodeQuery(User.class).getFirst();

        assertNotNull("User was not created", user);

        assertNull("Confirmation key was set after confirmation",
                user.getProperty(StructrApp.key(User.class, "confirmationKey")));

        final String[] sessionIds = user.getProperty(StructrApp.key(User.class, "sessionIds"));

        Assert.assertEquals("Invalid number of sessions after user confirmation", 1, sessionIds.length);
        Assert.assertEquals("Invalid session ID after user confirmation",
                StringUtils.substringBeforeLast(sessionFilter.getSessionId(), "."), sessionIds[0]);

        tx.success();

    } catch (FrameworkException t) {
        fail("Unexpected exception.");
    }
}

From source file:org.structr.web.importer.Importer.java

private Linkable downloadFile(final String downloadAddress, final URL base) {

    URL downloadUrl = null;//from www.  j  a va 2 s . c  om

    try {

        downloadUrl = new URL(base, downloadAddress);

    } catch (MalformedURLException ex) {

        logger.error("Could not resolve address {}", address != null ? address.concat("/") : "");
        return null;
    }

    final String alreadyDownloadedKey = downloadUrl.getPath();

    // Don't download the same file twice
    if (alreadyDownloaded.containsKey(alreadyDownloadedKey)) {
        return alreadyDownloaded.get(alreadyDownloadedKey);
    }

    long size;
    long checksum;
    String contentType;
    java.io.File tmpFile;

    try {
        // create temporary file on disk
        final Path tmpFilePath = Files.createTempFile("structr", "download");
        tmpFile = tmpFilePath.toFile();

    } catch (IOException ioex) {

        logger.error("Unable to create temporary file for download, aborting.");
        return null;
    }

    try {

        logger.info("Starting download from {}", downloadUrl);

        copyURLToFile(downloadUrl.toString(), tmpFile);

    } catch (IOException ioe) {

        if (originalUrl == null || address == null) {

            logger.info("Cannot download from {} without base address", downloadAddress);
            return null;

        }

        logger.warn("Unable to download from {} {}", new Object[] { originalUrl, downloadAddress });

        try {
            // Try alternative baseUrl with trailing "/"
            if (address.endsWith("/")) {

                // don't append a second slash!
                logger.info("Starting download from alternative URL {} {} {}",
                        new Object[] { originalUrl, address, downloadAddress });
                downloadUrl = new URL(new URL(originalUrl, address), downloadAddress);

            } else {

                // append a slash
                logger.info("Starting download from alternative URL {} {} {}",
                        new Object[] { originalUrl, address.concat("/"), downloadAddress });
                downloadUrl = new URL(new URL(originalUrl, address.concat("/")), downloadAddress);
            }

            copyURLToFile(downloadUrl.toString(), tmpFile);

        } catch (MalformedURLException ex) {
            logger.error("Could not resolve address {}", address.concat("/"));
            return null;
        } catch (IOException ex) {
            logger.warn("Unable to download from {}", address.concat("/"));
            return null;
        }

        logger.info("Starting download from alternative URL {}", downloadUrl);

    }

    //downloadAddress = StringUtils.substringBefore(downloadAddress, "?");
    final String downloadName = cleanFileName(downloadUrl.getFile());
    final String fileName = PathHelper.getName(downloadName);

    if (StringUtils.isBlank(fileName)) {

        logger.warn("Can't figure out filename from download address {}, aborting.", downloadAddress);

        return null;
    }

    // TODO: Add security features like null/integrity/virus checking before copying it to
    // the files repo
    try {

        contentType = FileHelper.getContentMimeType(tmpFile, fileName);
        checksum = FileHelper.getChecksum(tmpFile);
        size = tmpFile.length();

    } catch (IOException ioe) {

        logger.warn("Unable to determine MIME type, size or checksum of {}", tmpFile);
        return null;
    }

    logger.info("Download URL: {}, address: {}, cleaned address: {}, filename: {}",
            new Object[] { downloadUrl, address, StringUtils.substringBeforeLast(address, "/"), fileName });

    String relativePath = StringUtils.substringAfter(downloadUrl.toString(),
            StringUtils.substringBeforeLast(address, "/"));
    if (StringUtils.isBlank(relativePath)) {

        relativePath = downloadAddress;
    }

    final String path;
    final String httpPrefix = "http://";
    final String httpsPrefix = "https://";
    final String flexiblePrefix = "//";

    if (downloadAddress.startsWith(httpsPrefix)) {

        path = StringUtils.substringBefore((StringUtils.substringAfter(downloadAddress, httpsPrefix)), "/");

    } else if (downloadAddress.startsWith(httpPrefix)) {

        path = StringUtils.substringBefore((StringUtils.substringAfter(downloadAddress, httpPrefix)), "/");

    } else if (downloadAddress.startsWith(flexiblePrefix)) {

        path = StringUtils.substringBefore((StringUtils.substringAfter(downloadAddress, flexiblePrefix)), "/");

    } else {

        path = StringUtils.substringBeforeLast(relativePath, "/");
    }

    logger.info("Relative path: {}, final path: {}", relativePath, path);

    if (contentType.equals("text/plain")) {

        contentType = StringUtils.defaultIfBlank(
                contentTypeForExtension.get(StringUtils.substringAfterLast(fileName, ".")), "text/plain");
    }

    try {

        final String fullPath = path + "/" + fileName;

        File fileNode = fileExists(fullPath, checksum);
        if (fileNode == null) {

            if (ImageHelper.isImageType(fileName)) {

                fileNode = createImageNode(fullPath, contentType, size, checksum);

            } else {

                fileNode = createFileNode(fullPath, contentType, size, checksum);
            }

            final java.io.File imageFile = fileNode.getFileOnDisk(false);
            final Path imagePath = imageFile.toPath();

            // rename / move file to final location
            Files.move(tmpFile.toPath(), imagePath);

            if (contentType.equals("text/css")) {

                processCssFileNode(fileNode, downloadUrl);
            }

            // set export flag according to user preference
            fileNode.setProperty(StructrApp.key(File.class, "includeInFrontendExport"), includeInExport);

        } else {

            tmpFile.delete();
        }

        alreadyDownloaded.put(alreadyDownloadedKey, fileNode);
        return fileNode;

    } catch (final FrameworkException | IOException ex) {

        logger.warn("Could not create file node.", ex);

    }

    return null;

}

From source file:org.structr.web.Importer.java

private Linkable downloadFile(String downloadAddress, final URL base) {

    final String uuid = UUID.randomUUID().toString().replaceAll("[\\-]+", "");
    String contentType;//from   w  ww  . j a  v a 2 s.co  m

    // Create temporary file with new uuid
    // FIXME: This is much too dangerous!
    final String relativeFilePath = File.getDirectoryPath(uuid) + "/" + uuid;
    final String filePath = FileHelper.getFilePath(relativeFilePath);
    final java.io.File fileOnDisk = new java.io.File(filePath);

    fileOnDisk.getParentFile().mkdirs();

    long size;
    long checksum;
    URL downloadUrl;

    try {

        downloadUrl = new URL(base, downloadAddress);

        logger.log(Level.INFO, "Starting download from {0}", downloadUrl);

        copyURLToFile(downloadUrl.toString(), fileOnDisk);

    } catch (IOException ioe) {

        if (originalUrl == null || address == null) {

            logger.log(Level.INFO, "Cannot download from {0} without base address", downloadAddress);
            return null;

        }

        logger.log(Level.WARNING, "Unable to download from {0} {1}",
                new Object[] { originalUrl, downloadAddress });
        ioe.printStackTrace();

        try {
            // Try alternative baseUrl with trailing "/"
            if (address.endsWith("/")) {

                // don't append a second slash!
                logger.log(Level.INFO, "Starting download from alternative URL {0} {1} {2}",
                        new Object[] { originalUrl, address, downloadAddress });
                downloadUrl = new URL(new URL(originalUrl, address), downloadAddress);

            } else {

                // append a slash
                logger.log(Level.INFO, "Starting download from alternative URL {0} {1} {2}",
                        new Object[] { originalUrl, address.concat("/"), downloadAddress });
                downloadUrl = new URL(new URL(originalUrl, address.concat("/")), downloadAddress);
            }

            copyURLToFile(downloadUrl.toString(), fileOnDisk);

        } catch (MalformedURLException ex) {
            ex.printStackTrace();
            logger.log(Level.SEVERE, "Could not resolve address {0}", address.concat("/"));
            return null;
        } catch (IOException ex) {
            ex.printStackTrace();
            logger.log(Level.WARNING, "Unable to download from {0}", address.concat("/"));
            return null;
        }

        logger.log(Level.INFO, "Starting download from alternative URL {0}", downloadUrl);

    }

    downloadAddress = StringUtils.substringBefore(downloadAddress, "?");
    final String fileName = PathHelper.getName(downloadAddress);

    if (StringUtils.isBlank(fileName)) {

        logger.log(Level.WARNING, "Can't figure out filename from download address {0}, aborting.",
                downloadAddress);

        return null;
    }

    // TODO: Add security features like null/integrity/virus checking before copying it to
    // the files repo
    try {

        contentType = FileHelper.getContentMimeType(fileOnDisk, fileName);
        size = fileOnDisk.length();
        checksum = FileUtils.checksumCRC32(fileOnDisk);

    } catch (IOException ioe) {

        logger.log(Level.WARNING, "Unable to determine MIME type, size or checksum of {0}", fileOnDisk);
        return null;
    }

    String httpPrefix = "http://";

    logger.log(Level.INFO, "Download URL: {0}, address: {1}, cleaned address: {2}, filename: {3}",
            new Object[] { downloadUrl, address, StringUtils.substringBeforeLast(address, "/"), fileName });

    String relativePath = StringUtils.substringAfter(downloadUrl.toString(),
            StringUtils.substringBeforeLast(address, "/"));
    if (StringUtils.isBlank(relativePath)) {
        relativePath = downloadAddress;
    }

    final String path = StringUtils.substringBefore(
            ((downloadAddress.contains(httpPrefix)) ? StringUtils.substringAfter(downloadAddress, "http://")
                    : relativePath),
            fileName);

    logger.log(Level.INFO, "Relative path: {0}, final path: {1}", new Object[] { relativePath, path });

    if (contentType.equals("text/plain")) {

        contentType = StringUtils.defaultIfBlank(
                contentTypeForExtension.get(StringUtils.substringAfterLast(fileName, ".")), "text/plain");
    }

    final String ct = contentType;

    try {

        FileBase fileNode = fileExists(fileName, checksum);
        if (fileNode == null) {

            if (ImageHelper.isImageType(fileName)) {

                fileNode = createImageNode(uuid, fileName, ct, size, checksum);
            } else {

                fileNode = createFileNode(uuid, fileName, ct, size, checksum);
            }

            if (fileNode != null) {

                Folder parent = FileHelper.createFolderPath(securityContext, path);

                if (parent != null) {

                    fileNode.setProperty(File.parent, parent);

                }

                if (contentType.equals("text/css")) {

                    processCssFileNode(fileNode, downloadUrl);
                }

                if (!fileNode.validatePath(securityContext, null)) {
                    fileNode.setProperty(AbstractNode.name,
                            fileName.concat("_").concat(FileHelper.getDateString()));
                }

            }

            return fileNode;

        } else {

            fileOnDisk.delete();
            return fileNode;
        }

    } catch (FrameworkException | IOException fex) {

        logger.log(Level.WARNING, "Could not create file node.", fex);

    }

    return null;

}

From source file:org.structr.web.maintenance.deploy.ComponentImportVisitor.java

private void createComponent(final Path file, final String fileName) throws IOException, FrameworkException {

    final String componentName = StringUtils.substringBeforeLast(fileName, ".html");

    // either the template was exported with name + uuid or just the uuid
    final boolean byNameAndId = DeployCommand.endsWithUuid(componentName);
    final boolean byId = DeployCommand.isUuid(componentName);

    try (final Tx tx = app.tx(true, false, false)) {

        final DOMNode existingComponent = (byId ? app.get(DOMNode.class, componentName)
                : (byNameAndId ? app.get(DOMNode.class, componentName.substring(componentName.length() - 32))
                        : getExistingComponent(componentName)));

        final PropertyMap properties = getPropertiesForComponent(componentName);

        if (properties == null) {

            logger.info("Ignoring {} (not in components.json)", fileName);

        } else {/*from   w  w  w  .  j a  v  a2s.  c o m*/

            if (existingComponent != null) {

                final PropertyKey<String> contentKey = StructrApp.key(Template.class, "content");

                properties.put(contentKey, existingComponent.getProperty(contentKey));

                existingComponent.setOwnerDocument(null);

                if (existingComponent instanceof Template) {

                    properties.put(contentKey, existingComponent.getProperty(contentKey));
                    existingComponent.setOwnerDocument(null);

                } else {

                    deleteComponent(app, componentName);
                }
            }

            final String src = new String(Files.readAllBytes(file), Charset.forName("UTF-8"));
            boolean visibleToPublic = get(properties, GraphObject.visibleToPublicUsers, false);
            boolean visibleToAuth = get(properties, GraphObject.visibleToAuthenticatedUsers, false);
            final Importer importer = new Importer(securityContext, src, null, componentName, visibleToPublic,
                    visibleToAuth, false);

            // enable literal import of href attributes
            importer.setIsDeployment(true);

            final boolean parseOk = importer.parse(false);
            if (parseOk) {

                logger.info("Importing component {} from {}..", new Object[] { componentName, fileName });

                // set comment handler that can parse and apply special Structr comments in HTML source files
                importer.setCommentHandler(new DeploymentCommentHandler());

                // parse page
                final ShadowDocument shadowDocument = CreateComponentCommand.getOrCreateHiddenDocument();
                final DOMNode rootElement = importer.createComponentChildNodes(shadowDocument);

                if (rootElement != null) {

                    if (byId) {

                        // set UUID
                        rootElement.unlockSystemPropertiesOnce();
                        rootElement.setProperty(GraphObject.id, componentName);

                    } else if (byNameAndId) {

                        // the last characters in the name string are the uuid
                        final String uuid = componentName.substring(componentName.length() - 32);
                        final String name = componentName.substring(0, componentName.length() - 33);

                        rootElement.unlockSystemPropertiesOnce();
                        rootElement.setProperty(GraphObject.id, uuid);
                        properties.put(AbstractNode.name, name);

                    } else {

                        // set name
                        rootElement.setProperty(AbstractNode.name, componentName);
                    }

                    // store properties from components.json if present
                    rootElement.setProperties(securityContext, properties);
                }
            }
        }

        tx.success();
    }
}

From source file:org.structr.web.maintenance.deploy.PageImportVisitor.java

private void createPage(final Path file, final String fileName) throws IOException, FrameworkException {

    final String name = StringUtils.substringBeforeLast(fileName, ".html");

    try (final Tx tx = app.tx(true, false, false)) {

        final PropertyMap properties = getPropertiesForPage(name);

        if (properties == null) {

            logger.info("Ignoring {} (not in pages.json)", fileName);
        } else {//from  w  w  w. java  2  s.c o  m

            final Page existingPage = getExistingPage(name);

            if (existingPage != null) {

                deletePage(app, name);
            }

            final String src = new String(Files.readAllBytes(file), Charset.forName("UTF-8"));
            final String contentType = get(properties, StructrApp.key(Page.class, "contentType"), "text/html");

            boolean visibleToPublic = get(properties, GraphObject.visibleToPublicUsers, false);
            boolean visibleToAuth = get(properties, GraphObject.visibleToAuthenticatedUsers, false);

            final Importer importer = new Importer(securityContext, src, null, name, visibleToPublic,
                    visibleToAuth, false);

            // enable literal import of href attributes
            importer.setIsDeployment(true);

            if (StringUtils.startsWithIgnoreCase(src, DoctypeString) && "text/html".equals(contentType)) {

                // Import document starts with <!DOCTYPE> definition, so we treat it as an HTML
                // document and use the Structr HTML importer.

                final boolean parseOk = importer.parse();
                if (parseOk) {

                    logger.info("Importing page {} from {}..", new Object[] { name, fileName });

                    // set comment handler that can parse and apply special Structr comments in HTML source files
                    importer.setCommentHandler(new DeploymentCommentHandler());

                    // parse page
                    final Page newPage = importer.readPage();

                    // remove duplicate elements
                    fixDocumentElements(newPage);

                    // store properties from pages.json
                    newPage.setProperties(securityContext, properties);
                }

            } else {

                // Import document does NOT start with a <!DOCTYPE> definition, so we assume a
                // template or shared component that we need to parse.

                final boolean parseOk = importer.parse(true);
                if (parseOk) {

                    logger.info("Importing page {} from {}..", new Object[] { name, fileName });

                    // set comment handler that can parse and apply special Structr comments in HTML source files
                    importer.setCommentHandler(new DeploymentCommentHandler());

                    // parse page
                    final Page newPage = app.create(Page.class, name);

                    // store properties from pages.json
                    newPage.setProperties(securityContext, properties);

                    // add children
                    importer.createChildNodes(newPage, newPage);
                }
            }
        }

        tx.success();
    }
}

From source file:org.structr.web.maintenance.deploy.TemplateImportVisitor.java

private void createTemplate(final Path file, final String fileName) throws IOException, FrameworkException {

    final String templateName = StringUtils.substringBeforeLast(fileName, ".html");

    // either the template was exported with name + uuid or just the uuid
    final boolean byNameAndId = DeployCommand.endsWithUuid(templateName);
    final boolean byId = DeployCommand.isUuid(templateName);

    try (final Tx tx = app.tx(true, false, false)) {

        final PropertyMap properties = getPropertiesForTemplate(templateName);

        if (properties == null) {

            logger.info("Ignoring {} (not in templates.json)", fileName);
        } else {//  w  ww  .  j av  a  2s.com

            final String src = new String(Files.readAllBytes(file), Charset.forName("UTF-8"));

            final Template template;

            if (byId) {

                logger.info("Importing template {} from {}..", new Object[] { templateName, fileName });

                final DOMNode existingTemplate = app.get(DOMNode.class, templateName);
                if (existingTemplate != null) {

                    deleteTemplate(app, existingTemplate);
                }

                template = app.create(Template.class, new NodeAttribute(AbstractNode.id, templateName));

            } else if (byNameAndId) {

                // the last characters in the name string are the uuid
                final String uuid = templateName.substring(templateName.length() - 32);
                final String name = templateName.substring(0, templateName.length() - 33);

                logger.info("Importing template {} from {}..", new Object[] { name, fileName });

                final DOMNode existingTemplate = app.get(DOMNode.class, uuid);
                if (existingTemplate != null) {

                    deleteTemplate(app, existingTemplate);
                }

                template = app.create(Template.class, new NodeAttribute(AbstractNode.id, uuid));
                properties.put(Template.name, name);

            } else {

                // old export format: only name of template in filename
                logger.info("Importing template {} from {}..", new Object[] { templateName, fileName });

                final DOMNode existingTemplate = getExistingTemplate(templateName);
                if (existingTemplate != null) {

                    deleteTemplate(app, existingTemplate);
                }

                template = app.create(Template.class);
                properties.put(Template.name, templateName);
            }

            properties.put(StructrApp.key(Template.class, "content"), src);

            // insert "shared" templates into ShadowDocument
            final Object value = properties.get(internalSharedTemplateKey);
            if (value != null) {

                if ("true".equals(value)) {

                    template.setOwnerDocument(CreateComponentCommand.getOrCreateHiddenDocument());
                }

                properties.remove(internalSharedTemplateKey);
            }

            // store properties from templates.json if present
            template.setProperties(securityContext, properties);

        }

        tx.success();

    } catch (Throwable t) {

        logger.debug("Error trying to create template {}", fileName);
    }
}

From source file:org.structr.web.servlet.HtmlServlet.java

@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) {

    final Authenticator auth = getConfig().getAuthenticator();
    List<Page> pages = null;
    boolean requestUriContainsUuids = false;

    SecurityContext securityContext;/*from  w w w . j a  v  a  2  s . c o  m*/
    final App app;

    try {
        final String path = request.getPathInfo();

        // check for registration (has its own tx because of write access
        if (checkRegistration(auth, request, response, path)) {

            return;
        }

        // check for registration (has its own tx because of write access
        if (checkResetPassword(auth, request, response, path)) {

            return;
        }

        // isolate request authentication in a transaction
        try (final Tx tx = StructrApp.getInstance().tx()) {
            securityContext = auth.initializeAndExamineRequest(request, response);
            tx.success();
        }

        app = StructrApp.getInstance(securityContext);

        try (final Tx tx = app.tx()) {

            // Ensure access mode is frontend
            securityContext.setAccessMode(AccessMode.Frontend);

            request.setCharacterEncoding("UTF-8");

            // Important: Set character encoding before calling response.getWriter() !!, see Servlet Spec 5.4
            response.setCharacterEncoding("UTF-8");

            boolean dontCache = false;

            logger.log(Level.FINE, "Path info {0}", path);

            // don't continue on redirects
            if (response.getStatus() == 302) {

                tx.success();
                return;
            }

            final Principal user = securityContext.getUser(false);
            if (user != null) {

                // Don't cache if a user is logged in
                dontCache = true;

            }

            final RenderContext renderContext = RenderContext.getInstance(securityContext, request, response);

            renderContext.setResourceProvider(config.getResourceProvider());

            final EditMode edit = renderContext.getEditMode(user);

            DOMNode rootElement = null;
            AbstractNode dataNode = null;

            final String[] uriParts = PathHelper.getParts(path);
            if ((uriParts == null) || (uriParts.length == 0)) {

                // find a visible page
                rootElement = findIndexPage(securityContext, pages, edit);

                logger.log(Level.FINE, "No path supplied, trying to find index page");

            } else {

                if (rootElement == null) {

                    rootElement = findPage(securityContext, pages, path, edit);

                } else {
                    dontCache = true;
                }
            }

            if (rootElement == null) { // No page found

                // Look for a file
                final File file = findFile(securityContext, request, path);
                if (file != null) {

                    streamFile(securityContext, file, request, response, edit);
                    tx.success();
                    return;

                }

                // store remaining path parts in request
                final Matcher matcher = threadLocalUUIDMatcher.get();

                for (int i = 0; i < uriParts.length; i++) {

                    request.setAttribute(uriParts[i], i);
                    matcher.reset(uriParts[i]);

                    // set to "true" if part matches UUID pattern
                    requestUriContainsUuids |= matcher.matches();

                }

                if (!requestUriContainsUuids) {

                    // Try to find a data node by name
                    dataNode = findFirstNodeByName(securityContext, request, path);

                } else {

                    dataNode = findNodeByUuid(securityContext, PathHelper.getName(path));

                }

                //if (dataNode != null && !(dataNode instanceof Linkable)) {
                if (dataNode != null) {

                    // Last path part matches a data node
                    // Remove last path part and try again searching for a page
                    // clear possible entry points
                    request.removeAttribute(POSSIBLE_ENTRY_POINTS_KEY);

                    rootElement = findPage(securityContext, pages,
                            StringUtils.substringBeforeLast(path, PathHelper.PATH_SEP), edit);

                    renderContext.setDetailsDataObject(dataNode);

                    // Start rendering on data node
                    if (rootElement == null && dataNode instanceof DOMNode) {

                        rootElement = ((DOMNode) dataNode);
                    }
                }
            }

            // look for pages with HTTP Basic Authentication (must be done as superuser)
            if (rootElement == null) {

                final HttpBasicAuthResult authResult = checkHttpBasicAuth(request, response, path);

                switch (authResult.authState()) {

                // Element with Basic Auth found and authentication succeeded
                case Authenticated:
                    final Linkable result = authResult.getRootElement();
                    if (result instanceof Page) {

                        rootElement = (DOMNode) result;
                        securityContext = authResult.getSecurityContext();
                        renderContext.pushSecurityContext(securityContext);

                    } else if (result instanceof File) {

                        streamFile(authResult.getSecurityContext(), (File) result, request, response,
                                EditMode.NONE);
                        tx.success();
                        return;

                    }
                    break;

                // Page with Basic Auth found but not yet authenticated
                case MustAuthenticate:
                    tx.success();
                    return;

                // no Basic Auth for given path, go on
                case NoBasicAuth:
                    break;
                }

            }

            // Still nothing found, do error handling
            if (rootElement == null) {
                rootElement = notFound(response, securityContext);
            }

            if (rootElement == null) {
                tx.success();
                return;
            }

            // check dont cache flag on page (if root element is a page)
            // but don't modify true to false
            dontCache |= rootElement.getProperty(Page.dontCache);

            if (EditMode.WIDGET.equals(edit) || dontCache) {

                setNoCacheHeaders(response);

            }

            if (!securityContext.isVisible(rootElement)) {

                rootElement = notFound(response, securityContext);
                if (rootElement == null) {

                    tx.success();
                    return;
                }

            } else {

                if (!EditMode.WIDGET.equals(edit) && !dontCache
                        && notModifiedSince(request, response, rootElement, dontCache)) {

                    ServletOutputStream out = response.getOutputStream();
                    out.flush();
                    //response.flushBuffer();
                    out.close();

                } else {

                    // prepare response
                    response.setCharacterEncoding("UTF-8");

                    String contentType = rootElement.getProperty(Page.contentType);

                    if (contentType == null) {

                        // Default
                        contentType = "text/html;charset=UTF-8";
                    }

                    if (contentType.equals("text/html")) {
                        contentType = contentType.concat(";charset=UTF-8");
                    }

                    response.setContentType(contentType);

                    setCustomResponseHeaders(response);

                    final boolean createsRawData = rootElement.getProperty(Page.pageCreatesRawData);

                    // async or not?
                    if (isAsync && !createsRawData) {

                        final AsyncContext async = request.startAsync();
                        final ServletOutputStream out = async.getResponse().getOutputStream();
                        final AtomicBoolean finished = new AtomicBoolean(false);
                        final DOMNode rootNode = rootElement;

                        threadPool.submit(new Runnable() {

                            @Override
                            public void run() {

                                try (final Tx tx = app.tx()) {

                                    //final long start = System.currentTimeMillis();
                                    // render
                                    rootNode.render(renderContext, 0);
                                    finished.set(true);

                                    //final long end = System.currentTimeMillis();
                                    //System.out.println("Done in " + (end-start) + " ms");
                                    tx.success();

                                } catch (Throwable t) {
                                    t.printStackTrace();
                                    final String errorMsg = t.getMessage();
                                    try {
                                        //response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                                        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                                                errorMsg);
                                        finished.set(true);
                                    } catch (IOException ex) {
                                        ex.printStackTrace();
                                    }
                                }
                            }

                        });

                        // start output write listener
                        out.setWriteListener(new WriteListener() {

                            @Override
                            public void onWritePossible() throws IOException {

                                try {

                                    final Queue<String> queue = renderContext.getBuffer().getQueue();
                                    while (out.isReady()) {

                                        String buffer = null;

                                        synchronized (queue) {
                                            buffer = queue.poll();
                                        }

                                        if (buffer != null) {

                                            out.print(buffer);

                                        } else {

                                            if (finished.get()) {

                                                async.complete();
                                                response.setStatus(HttpServletResponse.SC_OK);

                                                // prevent this block from being called again
                                                break;
                                            }

                                            Thread.sleep(1);
                                        }
                                    }

                                } catch (Throwable t) {
                                    t.printStackTrace();
                                }
                            }

                            @Override
                            public void onError(Throwable t) {
                                t.printStackTrace();
                            }
                        });

                    } else {

                        final StringRenderBuffer buffer = new StringRenderBuffer();
                        renderContext.setBuffer(buffer);

                        // render
                        rootElement.render(renderContext, 0);

                        try {

                            response.getOutputStream().write(buffer.getBuffer().toString().getBytes("utf-8"));
                            response.getOutputStream().flush();
                            response.getOutputStream().close();

                        } catch (IOException ioex) {
                            ioex.printStackTrace();
                        }
                    }
                }
            }

            tx.success();

        } catch (FrameworkException fex) {
            fex.printStackTrace();
            logger.log(Level.SEVERE, "Exception while processing request", fex);
        }

    } catch (IOException | FrameworkException t) {

        t.printStackTrace();
        logger.log(Level.SEVERE, "Exception while processing request", t);
        UiAuthenticator.writeInternalServerError(response);
    }
}

From source file:org.structr.web.servlet.HtmlServlet.java

@Override
protected void doHead(final HttpServletRequest request, final HttpServletResponse response) {

    final Authenticator auth = getConfig().getAuthenticator();
    SecurityContext securityContext;//from ww w.  j av  a2 s.  c  o  m
    List<Page> pages = null;
    boolean requestUriContainsUuids = false;
    final App app;

    try {
        String path = request.getPathInfo();

        // isolate request authentication in a transaction
        try (final Tx tx = StructrApp.getInstance().tx()) {
            securityContext = auth.initializeAndExamineRequest(request, response);
            tx.success();
        }

        app = StructrApp.getInstance(securityContext);

        try (final Tx tx = app.tx()) {

            // Ensure access mode is frontend
            securityContext.setAccessMode(AccessMode.Frontend);

            request.setCharacterEncoding("UTF-8");

            // Important: Set character encoding before calling response.getWriter() !!, see Servlet Spec 5.4
            response.setCharacterEncoding("UTF-8");
            response.setContentLength(0);

            boolean dontCache = false;

            logger.log(Level.FINE, "Path info {0}", path);

            // don't continue on redirects
            if (response.getStatus() == 302) {

                tx.success();
                return;
            }

            final Principal user = securityContext.getUser(false);
            if (user != null) {

                // Don't cache if a user is logged in
                dontCache = true;

            }

            final RenderContext renderContext = RenderContext.getInstance(securityContext, request, response);

            renderContext.setResourceProvider(config.getResourceProvider());

            final EditMode edit = renderContext.getEditMode(user);

            DOMNode rootElement = null;
            AbstractNode dataNode = null;

            String[] uriParts = PathHelper.getParts(path);
            if ((uriParts == null) || (uriParts.length == 0)) {

                // find a visible page
                rootElement = findIndexPage(securityContext, pages, edit);

                logger.log(Level.FINE, "No path supplied, trying to find index page");

            } else {

                if (rootElement == null) {

                    rootElement = findPage(securityContext, pages, path, edit);

                } else {
                    dontCache = true;
                }
            }

            if (rootElement == null) { // No page found

                // Look for a file
                File file = findFile(securityContext, request, path);
                if (file != null) {

                    //streamFile(securityContext, file, request, response, edit);
                    tx.success();
                    return;

                }

                // store remaining path parts in request
                Matcher matcher = threadLocalUUIDMatcher.get();

                for (int i = 0; i < uriParts.length; i++) {

                    request.setAttribute(uriParts[i], i);
                    matcher.reset(uriParts[i]);

                    // set to "true" if part matches UUID pattern
                    requestUriContainsUuids |= matcher.matches();

                }

                if (!requestUriContainsUuids) {

                    // Try to find a data node by name
                    dataNode = findFirstNodeByName(securityContext, request, path);

                } else {

                    dataNode = findNodeByUuid(securityContext, PathHelper.getName(path));

                }

                if (dataNode != null && !(dataNode instanceof Linkable)) {

                    // Last path part matches a data node
                    // Remove last path part and try again searching for a page
                    // clear possible entry points
                    request.removeAttribute(POSSIBLE_ENTRY_POINTS_KEY);

                    rootElement = findPage(securityContext, pages,
                            StringUtils.substringBeforeLast(path, PathHelper.PATH_SEP), edit);

                    renderContext.setDetailsDataObject(dataNode);

                    // Start rendering on data node
                    if (rootElement == null && dataNode instanceof DOMNode) {

                        rootElement = ((DOMNode) dataNode);

                    }

                }

            }

            // look for pages with HTTP Basic Authentication (must be done as superuser)
            if (rootElement == null) {

                final HttpBasicAuthResult authResult = checkHttpBasicAuth(request, response, path);

                switch (authResult.authState()) {

                // Element with Basic Auth found and authentication succeeded
                case Authenticated:
                    final Linkable result = authResult.getRootElement();
                    if (result instanceof Page) {

                        rootElement = (DOMNode) result;
                        renderContext.pushSecurityContext(authResult.getSecurityContext());

                    } else if (result instanceof File) {

                        //streamFile(authResult.getSecurityContext(), (File)result, request, response, EditMode.NONE);
                        tx.success();
                        return;

                    }
                    break;

                // Page with Basic Auth found but not yet authenticated
                case MustAuthenticate:
                    return;

                // no Basic Auth for given path, go on
                case NoBasicAuth:
                    break;
                }
            }

            // Still nothing found, do error handling
            if (rootElement == null) {

                // Check if security context has set an 401 status
                if (response.getStatus() == HttpServletResponse.SC_UNAUTHORIZED) {

                    try {

                        UiAuthenticator.writeUnauthorized(response);

                    } catch (IllegalStateException ise) {
                    }

                } else {

                    rootElement = notFound(response, securityContext);

                }

            }

            if (rootElement == null) {

                // no content
                response.setContentLength(0);
                response.getOutputStream().close();

                tx.success();
                return;
            }

            // check dont cache flag on page (if root element is a page)
            // but don't modify true to false
            dontCache |= rootElement.getProperty(Page.dontCache);

            if (EditMode.WIDGET.equals(edit) || dontCache) {

                setNoCacheHeaders(response);

            }

            if (!securityContext.isVisible(rootElement)) {

                rootElement = notFound(response, securityContext);
                if (rootElement == null) {

                    tx.success();
                    return;
                }

            }

            if (securityContext.isVisible(rootElement)) {

                if (!EditMode.WIDGET.equals(edit) && !dontCache
                        && notModifiedSince(request, response, rootElement, dontCache)) {

                    response.getOutputStream().close();

                } else {

                    // prepare response
                    response.setCharacterEncoding("UTF-8");

                    String contentType = rootElement.getProperty(Page.contentType);

                    if (contentType == null) {

                        // Default
                        contentType = "text/html;charset=UTF-8";
                    }

                    if (contentType.equals("text/html")) {
                        contentType = contentType.concat(";charset=UTF-8");
                    }

                    response.setContentType(contentType);

                    setCustomResponseHeaders(response);

                    response.getOutputStream().close();
                }

            } else {

                notFound(response, securityContext);

                response.getOutputStream().close();
            }

            tx.success();

        } catch (Throwable fex) {
            fex.printStackTrace();
            logger.log(Level.SEVERE, "Exception while processing request", fex);
        }

    } catch (FrameworkException t) {

        t.printStackTrace();
        logger.log(Level.SEVERE, "Exception while processing request", t);
        UiAuthenticator.writeInternalServerError(response);
    }
}

From source file:org.talend.updates.runtime.nexus.component.ComponentIndexManager.java

public ComponentIndexBean createIndexBean4Patch(File patchZipFile, Type type) {
    if (patchZipFile == null || !patchZipFile.exists() || patchZipFile.isDirectory()
            || !patchZipFile.getName().endsWith(FileExtensions.ZIP_FILE_SUFFIX) || type == null) {
        return null;
    }/*from   w ww.j a v a2s  . c o  m*/
    String name = StringUtils.removeEnd(patchZipFile.getName(), FileExtensions.ZIP_FILE_SUFFIX);
    String bundleId = null;
    String bundleVersion = null;
    String mvnUri = null;
    if (type == Type.PLAIN_ZIP) {
        bundleId = "org.talend.studio.patch.plainzip"; //$NON-NLS-1$
        bundleVersion = name;
    } else if (type == Type.P2_PATCH) {
        bundleId = "org.talend.studio.patch.updatesite"; //$NON-NLS-1$
        bundleVersion = P2Manager.getInstance().getP2Version(patchZipFile);
    }
    String artifactId = StringUtils.substringBeforeLast(name, "-"); //$NON-NLS-1$
    String artifactVersion = StringUtils.substringAfterLast(name, "-"); //$NON-NLS-1$
    mvnUri = MavenUrlHelper.generateMvnUrl(bundleId, artifactId, artifactVersion, FileExtensions.ZIP_EXTENSION,
            null);
    if (name != null && bundleId != null && bundleVersion != null && mvnUri != null) {
        ComponentIndexBean indexBean = new ComponentIndexBean();
        boolean set = indexBean.setRequiredFieldsValue(name, bundleId, bundleVersion, mvnUri);
        indexBean.setValue(ComponentIndexNames.types, PathUtils.convert2StringTypes(Arrays.asList(type)));
        if (set) {
            return indexBean;
        }
    }
    return null;
}