Example usage for javax.servlet ServletContext setAttribute

List of usage examples for javax.servlet ServletContext setAttribute

Introduction

In this page you can find the example usage for javax.servlet ServletContext setAttribute.

Prototype

public void setAttribute(String name, Object object);

Source Link

Document

Binds an object to a given attribute name in this ServletContext.

Usage

From source file:com.openkm.servlet.admin.CheckTextExtractionServlet.java

@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    log.debug("doPost({}, {})", request, response);
    request.setCharacterEncoding("UTF-8");
    updateSessionManager(request);/*from   w ww  .j  a v a  2 s .c om*/
    InputStream is = null;

    try {
        if (ServletFileUpload.isMultipartContent(request)) {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List<FileItem> items = upload.parseRequest(request);
            String docUuid = null;
            String repoPath = null;
            String text = null;
            String error = null;
            String mimeType = null;
            String extractor = null;

            for (Iterator<FileItem> it = items.iterator(); it.hasNext();) {
                FileItem item = it.next();

                if (item.isFormField()) {
                    if (item.getFieldName().equals("docUuid")) {
                        docUuid = item.getString("UTF-8");
                    } else if (item.getFieldName().equals("repoPath")) {
                        repoPath = item.getString("UTF-8");
                    }
                } else {
                    is = item.getInputStream();
                    String name = FilenameUtils.getName(item.getName());
                    mimeType = MimeTypeConfig.mimeTypes.getContentType(name.toLowerCase());

                    if (!name.isEmpty() && item.getSize() > 0) {
                        docUuid = null;
                        repoPath = null;
                    } else if (docUuid.isEmpty() && repoPath.isEmpty()) {
                        mimeType = null;
                    }
                }
            }

            if (docUuid != null && !docUuid.isEmpty()) {
                repoPath = OKMRepository.getInstance().getNodePath(null, docUuid);
            }

            if (repoPath != null && !repoPath.isEmpty()) {
                String name = PathUtils.getName(repoPath);
                mimeType = MimeTypeConfig.mimeTypes.getContentType(name.toLowerCase());
                is = OKMDocument.getInstance().getContent(null, repoPath, false);
            }

            long begin = System.currentTimeMillis();

            if (is != null) {
                if (!MimeTypeConfig.MIME_UNDEFINED.equals(mimeType)) {
                    TextExtractor extClass = RegisteredExtractors.getTextExtractor(mimeType);

                    if (extClass != null) {
                        try {
                            extractor = extClass.getClass().getCanonicalName();
                            text = RegisteredExtractors.getText(mimeType, null, is);
                        } catch (Exception e) {
                            error = e.getMessage();
                        }
                    } else {
                        extractor = "Undefined text extractor";
                    }
                }
            }

            ServletContext sc = getServletContext();
            sc.setAttribute("docUuid", docUuid);
            sc.setAttribute("repoPath", repoPath);
            sc.setAttribute("text", text);
            sc.setAttribute("time", System.currentTimeMillis() - begin);
            sc.setAttribute("mimeType", mimeType);
            sc.setAttribute("error", error);
            sc.setAttribute("extractor", extractor);
            sc.getRequestDispatcher("/admin/check_text_extraction.jsp").forward(request, response);
        }
    } catch (DatabaseException e) {
        sendErrorRedirect(request, response, e);
    } catch (FileUploadException e) {
        sendErrorRedirect(request, response, e);
    } catch (PathNotFoundException e) {
        sendErrorRedirect(request, response, e);
    } catch (AccessDeniedException e) {
        sendErrorRedirect(request, response, e);
    } catch (RepositoryException e) {
        sendErrorRedirect(request, response, e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:test.pl.chilldev.facelets.taglib.spring.web.form.ErrorsTagTest.java

@Test
public void applyNewRequestContext() throws IOException, FacesException {
    String path = "foo";
    String var = "error";

    Map<String, Object> config = new HashMap<>();
    config.put(ErrorsTag.ATTRIBUTE_PATH, path);
    config.put(ErrorsTag.ATTRIBUTE_VAR, var);

    ErrorsTag tag = new ErrorsTag(MockTagConfig.factory(config, this.nextHandler));

    // set up context
    ServletContext servletContext = new MockServletContext();
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
            this.applicationContext);
    FaceletContext context = new MockFaceletContext(new MockHttpServletRequest(), new MockHttpServletResponse(),
            servletContext);/*from  w w  w  .j a  v a 2 s  . co m*/

    // run the tag
    tag.apply(context, this.parent);
}

From source file:org.rhq.enterprise.gui.startup.Configurator.java

/**
 * The table tag GUI component needs the taglib definitions. This loads those definitions.
 *//*w w w .  j  a  v a  2  s .c  o m*/
private void loadTablePreferences(ServletContext ctx) {
    try {
        Properties tableProps = loadProperties(ctx, TAGLIB_PROPERTIES_FILE);
        ctx.setAttribute(Constants.PROPS_TAGLIB_NAME, tableProps);
    } catch (Exception e) {
        log.error("failed to load the taglib properties at [" + TAGLIB_PROPERTIES_FILE + "]: ", e);
    }
}

From source file:net.sourceforge.vulcan.web.VulcanContextListener.java

public void contextInitialized(ServletContextEvent event) {
    final ServletContext context = event.getServletContext();

    wac = WebApplicationContextUtils.getRequiredWebApplicationContext(context);

    stateManager = (StateManager) wac.getBean(Keys.STATE_MANAGER, StateManager.class);

    context.setAttribute(Keys.STATE_MANAGER, stateManager);
    context.setAttribute(Keys.EVENT_POOL, wac.getBean(Keys.EVENT_POOL));
    context.setAttribute(Keys.BUILD_OUTCOME_STORE, wac.getBean(Keys.BUILD_OUTCOME_STORE));

    JstlFunctions.setWebApplicationContext(wac);
    XslHelper.setWebApplicationContext(wac);

    try {/*  w w w  .j a  v  a2s  . c om*/
        stateManager.start();
    } catch (Exception e) {
        final EventHandler eventHandler = (EventHandler) wac.getBean(Keys.EVENT_HANDLER, EventHandler.class);

        eventHandler
                .reportEvent(new ErrorEvent(this, "errors.load.failure", new String[] { e.getMessage() }, e));
    }
}

From source file:jease.cms.web.servlet.JeaseServletListener.java

protected void initLocale(ServletContext context) {
    String localeCode = context.getInitParameter(Names.JEASE_DEFAULT_LOCALE);
    if (StringUtils.isNotBlank(localeCode)) {
        Locale locale = new Locale(localeCode);
        I18N.load(locale);/*from   www . j  a va 2 s .co  m*/
        context.setAttribute("org.zkoss.web.preferred.locale", locale);
    }
}

From source file:org.geoserver.test.GeoServerTestApplicationContext.java

public GeoServerTestApplicationContext(String[] configLocation, ServletContext servletContext)
        throws BeansException {
    super(configLocation, false);
    try {//w  ww  . jav  a2 s  .c om
        contextTmp = IOUtils.createRandomDirectory("./target", "mock", "tmp");
        servletContext.setAttribute("javax.servlet.context.tempdir", contextTmp);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    this.servletContext = servletContext;
}

From source file:com.ikon.servlet.admin.CronTabServlet.java

/**
 * List registered reports/*  ww  w  .j a  v a  2  s  .co m*/
 */
private void list(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, DatabaseException {
    log.debug("list({}, {})", new Object[] { request, response });
    ServletContext sc = getServletContext();
    List<CronTab> list = CronTabDAO.findAll();
    sc.setAttribute("crontabs", list);
    sc.getRequestDispatcher("/admin/crontab_list.jsp").forward(request, response);
    log.debug("list: void");
}

From source file:com.ikon.servlet.admin.CssServlet.java

/**
 * Delete CSS//from ww  w  .java 2s .  c  o m
 */
private void delete(String userId, HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, DatabaseException {
    log.debug("delete({}, {}, {})", new Object[] { userId, request, response });
    ServletContext sc = getServletContext();
    long id = WebUtils.getLong(request, "css_id");

    sc.setAttribute("action", WebUtils.getString(request, "action"));
    sc.setAttribute("persist", true);
    sc.setAttribute("css", CssDAO.getInstance().findByPk(id));
    sc.getRequestDispatcher("/admin/css_edit.jsp").forward(request, response);

    log.debug("edit: void");
}

From source file:com.ikon.servlet.admin.CssServlet.java

/**
 * Edit CSS/*from   w  w  w  .ja  v  a 2 s  .  co m*/
 */
private void edit(String userId, HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, DatabaseException {
    log.debug("edit({}, {}, {})", new Object[] { userId, request, response });
    ServletContext sc = getServletContext();
    long id = WebUtils.getLong(request, "css_id");

    sc.setAttribute("action", WebUtils.getString(request, "action"));
    sc.setAttribute("persist", true);
    sc.setAttribute("css", CssDAO.getInstance().findByPk(id));
    sc.getRequestDispatcher("/admin/css_edit.jsp").forward(request, response);

    log.debug("edit: void");
}

From source file:nl.nn.adapterframework.webcontrol.ConfigurationServlet.java

@Override
public void init() throws ServletException {
    super.init();
    setUploadPathInServletContext();//from  w ww.ja  va  2s  . c  om
    ibisContext = new IbisContext();
    setDefaultApplicationServerType(ibisContext);
    ServletContext servletContext = getServletContext();
    AppConstants appConstants = AppConstants.getInstance();
    String attributeKey = appConstants.getResolvedProperty(KEY_CONTEXT);
    servletContext.setAttribute(attributeKey, ibisContext);
    log.debug("stored IbisContext [" + ClassUtils.nameOf(ibisContext) + "][" + ibisContext
            + "] in ServletContext under key [" + attributeKey + "]");
    String realPath = servletContext.getRealPath("/");
    if (realPath != null) {
        appConstants.put("webapp.realpath", realPath);
    } else {
        log.warn("Could not determine webapp.realpath");
    }
    ibisContext.init();
    log.debug("Servlet init finished");
}