Example usage for javax.servlet ServletContext getInitParameter

List of usage examples for javax.servlet ServletContext getInitParameter

Introduction

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

Prototype

public String getInitParameter(String name);

Source Link

Document

Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.

Usage

From source file:controllers.LinguagemController.java

private void adicionarOuEditarLinguagem() throws IOException, ServletException {
    String anoLancamento, nome, id, licenca, descricao, caminhoLogo;
    anoLancamento = nome = licenca = descricao = caminhoLogo = id = "";

    File file;//from  w  w w .j a  va  2 s . co  m
    int maxFileSize = 5000 * 1024;
    int maxMemSize = 5000 * 1024;
    ServletContext context = getServletContext();
    String filePath = context.getInitParameter("file-upload");

    String contentType = request.getContentType();

    if ((contentType.contains("multipart/form-data"))) {

        DiskFileItemFactory factory = new DiskFileItemFactory();

        factory.setSizeThreshold(maxMemSize);
        factory.setRepository(new File("c:\\temp"));

        ServletFileUpload upload = new ServletFileUpload(factory);

        upload.setSizeMax(maxFileSize);
        try {
            List fileItems = upload.parseRequest(request);

            Iterator i = fileItems.iterator();

            while (i.hasNext()) {
                FileItem fi = (FileItem) i.next();
                if (!fi.isFormField()) {
                    String fileName = fi.getName();
                    if (fileName != null) {
                        String name = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
                        file = new File(filePath + name);

                        caminhoLogo = file.getName();
                        fi.write(file);
                    }
                } else {
                    String campo = fi.getFieldName();
                    String valor = fi.getString("UTF-8");

                    switch (campo) {
                    case "nome":
                        nome = valor;
                        break;
                    case "ano_lancamento":
                        anoLancamento = valor;
                        break;
                    case "licenca":
                        licenca = valor;
                        break;
                    case "descricao":
                        descricao = valor;
                        break;
                    case "id":
                        id = valor;
                        break;
                    default:
                        break;
                    }
                }
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
    boolean atualizando = !id.isEmpty();

    if (atualizando) {
        Linguagem linguagem = dao.select(Integer.parseInt(id));

        linguagem.setAnoLancamento(anoLancamento);
        linguagem.setDescricao(descricao);
        linguagem.setLicenca(licenca);
        linguagem.setNome(nome);

        if (!caminhoLogo.isEmpty()) {
            File imagemAntiga = new File(filePath + linguagem.getCaminhoLogo());
            imagemAntiga.delete();

            linguagem.setCaminhoLogo(caminhoLogo);
        }

        dao.update(linguagem);

    } else {
        Linguagem linguagem = new Linguagem(nome, anoLancamento, licenca, descricao, caminhoLogo);

        dao.insert(linguagem);
    }

    response.sendRedirect("linguagens.jsp");
    //        response.getWriter().print("<script>setTimeout(function () {"
    //                    + "window.location.href='linguagens.jsp';"
    //                + "}"
    //            + ", 1500);</script>");

}

From source file:org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter.java

/**
 * Constructs a Cas20ServiceTicketValidator or a Cas20ProxyTicketValidator based on supplied parameters.
 *
 * @param filterConfig the Filter Configuration object.
 * @return a fully constructed TicketValidator.
 *//*w  ww .  j a  v a 2  s  .co m*/
protected final TicketValidator getTicketValidator(final FilterConfig filterConfig) {
    final String allowAnyProxy = getPropertyFromInitParams(filterConfig, "acceptAnyProxy", null);
    final String allowedProxyChains = getPropertyFromInitParams(filterConfig, "allowedProxyChains", null);

    // TODO ?    
    ServletContext context = filterConfig.getServletContext();
    String casServerContextName = context.getInitParameter(CAS_SERVER_NAME_CONTEXT_PARAMETER);
    CommonUtils.assertNotNull(casServerContextName, "casServerContextName cannot be null.");

    String casServerAddress = getCasServerAddress();
    logger.trace(this.getClass() + ".getTicketValidator(): casServerAddress = " + casServerAddress);

    final String casServerUrlPrefix = casServerAddress + "/" + casServerContextName + "/";
    logger.trace(this.getClass() + ".getTicketValidator(): casServerUrlPrefix = " + casServerUrlPrefix);

    final Cas20ServiceTicketValidator validator;

    if (CommonUtils.isNotBlank(allowAnyProxy) || CommonUtils.isNotBlank(allowedProxyChains)) {
        final Cas20ProxyTicketValidator v = new Cas20ProxyTicketValidator(casServerUrlPrefix);
        v.setAcceptAnyProxy(parseBoolean(allowAnyProxy));
        v.setAllowedProxyChains(CommonUtils.createProxyList(allowedProxyChains));
        validator = v;
    } else {
        validator = new Cas20ServiceTicketValidator(casServerUrlPrefix);
    }

    validator.setProxyCallbackPath(getPropertyFromInitParams(filterConfig, "proxyCallbackPath", null));

    validator.setProxyGrantingTicketStorage(this.proxyGrantingTicketStorage);
    validator.setProxyRetriever(new Cas20ProxyRetriever(casServerUrlPrefix,
            getPropertyFromInitParams(filterConfig, "encoding", null)));
    validator.setRenew(parseBoolean(getPropertyFromInitParams(filterConfig, "renew", "false")));
    validator.setEncoding(getPropertyFromInitParams(filterConfig, "encoding", null));

    final Map additionalParameters = new HashMap();
    final List params = Arrays.asList(RESERVED_INIT_PARAMS);

    for (final Enumeration e = filterConfig.getInitParameterNames(); e.hasMoreElements();) {
        final String s = (String) e.nextElement();

        if (!params.contains(s)) {
            additionalParameters.put(s, filterConfig.getInitParameter(s));
        }
    }

    validator.setCustomParameters(additionalParameters);
    validator.setHostnameVerifier(getHostnameVerifier(filterConfig));

    return validator;
}

From source file:org.mule.config.builders.WebappMuleXmlConfigurationBuilder.java

/**
 * Used to lookup parent spring ApplicationContext. This allows a parent spring
 * ApplicatonContet to be provided in the same way you would configure a parent
 * ApplicationContext for a spring WebAppplicationContext
 * /*from w ww. ja  va  2 s.co  m*/
 * @param servletContext
 * @throws BeansException
 */
protected ApplicationContext loadParentContext(ServletContext servletContext) throws BeansException {
    ApplicationContext parentContext = null;
    String locatorFactorySelector = servletContext
            .getInitParameter(ContextLoader.LOCATOR_FACTORY_SELECTOR_PARAM);
    String parentContextKey = servletContext.getInitParameter(ContextLoader.LOCATOR_FACTORY_KEY_PARAM);

    if (parentContextKey != null) {
        // locatorFactorySelector may be null, indicating the default
        // "classpath*:beanRefContext.xml"
        BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance(locatorFactorySelector);
        if (logger.isDebugEnabled()) {
            logger.debug("Getting parent context definition: using parent context key of '" + parentContextKey
                    + "' with BeanFactoryLocator");
        }
        parentContext = (ApplicationContext) locator.useBeanFactory(parentContextKey).getFactory();
    }

    return parentContext;
}

From source file:com.haulmont.cuba.web.sys.singleapp.SingleAppWebContextLoader.java

protected void initWebServletContextListener(ServletContextEvent servletContextEvent, ServletContext sc) {
    String className = sc.getInitParameter("webServletContextListener");
    if (!Strings.isNullOrEmpty(className)) {
        try {/*from www  .  j  ava  2  s . c om*/
            Class<?> clazz = this.getClass().getClassLoader().loadClass(className);
            webServletContextListener = (ServletContextListener) clazz.newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("An error occurred while starting single WAR application", e);
        }
        webServletContextListener.contextInitialized(servletContextEvent);
    }
}

From source file:com.haulmont.cuba.core.sys.AbstractWebAppContextLoader.java

protected void initAppComponents(ServletContext sc) {
    String block = getBlock();//w  w w  .j a  v a  2s  . c om

    String appComponentsParam = sc.getInitParameter(APP_COMPONENTS_PARAM);
    AppComponents appComponents;
    if (StringUtils.isEmpty(appComponentsParam)) {
        appComponents = new AppComponents(block);
    } else {
        List<String> compNames = Splitter.on(SEPARATOR_PATTERN).omitEmptyStrings()
                .splitToList(appComponentsParam);
        appComponents = new AppComponents(compNames, block);
    }
    AppContext.Internals.setAppComponents(appComponents);
}

From source file:org.lightadmin.core.config.LightAdminWebApplicationInitializer.java

private String configurationsBasePackage(final ServletContext servletContext) {
    return servletContext.getInitParameter(LIGHT_ADMINISTRATION_BASE_PACKAGE);
}

From source file:org.lightadmin.core.config.LightAdminWebApplicationInitializer.java

private String lightAdminBaseUrl(final ServletContext servletContext) {
    return servletContext.getInitParameter(LIGHT_ADMINISTRATION_BASE_URL);
}

From source file:org.lightadmin.core.config.LightAdminWebApplicationInitializer.java

private String lightAdminGlobalFileStorageDirectory(final ServletContext servletContext) {
    return servletContext.getInitParameter(LIGHT_ADMINISTRATION_FILE_STORAGE_PATH);
}

From source file:controllers.FrameworkController.java

private void adicionarOuEditarFramework() throws IOException {
    String nome, genero, paginaOficial, id, descricao, caminhoLogo;
    int idLinguagem = -1;
    genero = nome = paginaOficial = descricao = id = caminhoLogo = "";

    File file;//from   w w  w.ja  v a  2s  .com
    int maxFileSize = 5000 * 1024;
    int maxMemSize = 5000 * 1024;
    ServletContext context = getServletContext();
    String filePath = context.getInitParameter("file-upload");

    String contentType = request.getContentType();

    if ((contentType.contains("multipart/form-data"))) {

        DiskFileItemFactory factory = new DiskFileItemFactory();

        factory.setSizeThreshold(maxMemSize);
        factory.setRepository(new File("c:\\temp"));

        ServletFileUpload upload = new ServletFileUpload(factory);

        upload.setSizeMax(maxFileSize);
        try {
            List fileItems = upload.parseRequest(request);

            Iterator i = fileItems.iterator();

            while (i.hasNext()) {
                FileItem fi = (FileItem) i.next();
                if (!fi.isFormField()) {
                    String fileName = fi.getName();
                    if (fileName.lastIndexOf("\\") >= 0) {
                        //String name = fileName.substring(fileName.lastIndexOf("\\"), fileName.lastIndexOf("."));
                        String name = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
                        file = new File(filePath + name);
                    } else {
                        //String name = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.lastIndexOf("."));
                        String name = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
                        file = new File(filePath + name);
                    }
                    caminhoLogo = file.getName();
                    fi.write(file);
                } else {
                    String campo = fi.getFieldName();
                    String valor = fi.getString("UTF-8");

                    switch (campo) {
                    case "nome":
                        nome = valor;
                        break;
                    case "genero":
                        genero = valor;
                        break;
                    case "pagina_oficial":
                        paginaOficial = valor;
                        break;
                    case "descricao":
                        descricao = valor;
                        break;
                    case "linguagem":
                        idLinguagem = Integer.parseInt(valor);
                        break;
                    case "id":
                        id = valor;
                        break;
                    default:
                        break;
                    }
                }
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
    boolean atualizando = !id.isEmpty();

    if (atualizando) {
        Framework framework = dao.select(Integer.parseInt(id));

        framework.setDescricao(descricao);
        framework.setGenero(genero);
        framework.setIdLinguagem(idLinguagem);
        framework.setNome(nome);
        framework.setPaginaOficial(paginaOficial);

        if (!caminhoLogo.isEmpty()) {
            File imagemAntiga = new File(filePath + framework.getCaminhoLogo());
            imagemAntiga.delete();

            framework.setCaminhoLogo(caminhoLogo);
        }

        dao.update(framework);

    } else {
        Framework framework = new Framework(nome, descricao, genero, paginaOficial, idLinguagem, caminhoLogo);

        dao.insert(framework);
    }

    response.sendRedirect("frameworks.jsp");
    //response.getWriter().print("<script>window.location.href='frameworks.jsp';</script>");
}

From source file:org.ajax4jsf.webapp.ConfigurableXMLFilter.java

public void init(FilterConfig config) throws ServletException {
    super.init(config);
    ServletContext servletContext = config.getServletContext();
    String parsersParameter = servletContext.getInitParameter(PARSERS_LIST_PARAMETER);
    if (null != parsersParameter) {
        configureParsers(servletContext, parsersParameter);
    }//from  w  w  w. j a  v a 2 s  . c  o m
}