Ejemplo n.º 1
0
  private String getParamOrCookieProperty(final String key) {
    final ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    String property = externalContext.getInitParameter(Config.EXTERNAL_SOURCE_PARAM);
    if (property != null && property != "") {
      return property;
    }

    Object cookie = externalContext.getRequestCookieMap().get(Config.EXTERNAL_SOURCE_PARAM);
    property = ((cookie != null && cookie instanceof Cookie) ? ((Cookie) cookie).getValue() : "");
    return property;
  }
Ejemplo n.º 2
0
  public List<Cookie> cookiesFromUrl(List<String> paramsNamesList) {
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();

    List<Cookie> cookiesList = new ArrayList<Cookie>();

    for (String paramName : paramsNamesList) {
      String initParameter = externalContext.getInitParameter(paramName);
      if (initParameter != null && initParameter.length() > 8) {
        Cookie cookie = new Cookie(paramName, initParameter);
        cookiesList.add(cookie);
      }
    }
    return cookiesList;
  }
Ejemplo n.º 3
0
  public String createEscursione() {

    String filename = FilenameUtils.getName(imgEscursione.getFileName());

    if (filename.equals("")) {
      escursione.setImmagine("Edefault.jpg");
    } else {
      InputStream inputStr = null;
      try {
        inputStr = imgEscursione.getInputstream();
      } catch (IOException e) {
        // log error
      }

      ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
      String directory = externalContext.getInitParameter("uploadDirectory");
      File destFile = new File(directory, filename);

      // use org.apache.commons.io.FileUtils to copy the File
      try {
        FileUtils.copyInputStreamToFile(inputStr, destFile);
      } catch (IOException e) {
        // log error
      }
      escursione.setImmagine(imgEscursione.getFileName());
    }
    if (escursione.getCosto() <= 0) {
      FacesContext.getCurrentInstance()
          .addMessage(
              null,
              new FacesMessage(
                  FacesMessage.SEVERITY_WARN,
                  "Info message",
                  "Il costo giornaliero deve essere > 0"));
      return "addEscursione.xhtml";
    }

    CMB.saveEscursione(escursione);
    FacesContext.getCurrentInstance()
        .addMessage(
            null,
            new FacesMessage(FacesMessage.SEVERITY_INFO, "Info message", "Escursione aggiunta!"));
    return "addEscursione.xhtml";
  }
Ejemplo n.º 4
0
  // -------------------------CREAZIONE COMPONENTI------------------------------
  public String createHotel() throws IOException {

    String filename = FilenameUtils.getName(imgHotel.getFileName());
    if (filename.equals("")) {
      hotel.setHotelImg("Hdefault.jpeg");
    } else {
      InputStream inputStr = null;
      try {
        inputStr = imgHotel.getInputstream();
      } catch (IOException e) {
        // log error
      }

      ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
      String directory = externalContext.getInitParameter("uploadDirectory");
      File destFile = new File(directory, filename);

      // use org.apache.commons.io.FileUtils to copy the File
      try {
        FileUtils.copyInputStreamToFile(inputStr, destFile);
      } catch (IOException e) {
        // log error
      }
      hotel.setHotelImg(imgHotel.getFileName());
    }

    if (hotel.getCosto_giornaliero() <= 0 || hotel.getData_inizio().after(hotel.getData_fine())) {
      FacesContext.getCurrentInstance()
          .addMessage(
              null,
              new FacesMessage(
                  FacesMessage.SEVERITY_WARN,
                  "Info message",
                  "Il costo giornaliero deve essere > 0"));
      return "addHotel.xhtml";
    }

    CMB.saveHotel(hotel);
    FacesContext.getCurrentInstance()
        .addMessage(
            null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info message", "Hotel aggiunto!"));
    return "addHotel.xhtml";
  }
Ejemplo n.º 5
0
  public String createVolo() {

    String filename = FilenameUtils.getName(imgVolo.getFileName());

    if (filename.equals("")) {
      volo.setImmagine("Vdefault.jpg");
    } else {
      InputStream inputStr = null;
      try {
        inputStr = imgVolo.getInputstream();
      } catch (IOException e) {
        // log error
      }

      ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
      String directory = externalContext.getInitParameter("uploadDirectory");
      File destFile = new File(directory, filename);

      // use org.apache.commons.io.FileUtils to copy the File
      try {
        FileUtils.copyInputStreamToFile(inputStr, destFile);
      } catch (IOException e) {
        // log error
      }
      volo.setImmagine(imgVolo.getFileName());
    }

    if (volo.getCosto() <= 0 || (volo.getLuogo_arrivo().equals(volo.getLuogo_partenza()))) {
      FacesContext.getCurrentInstance()
          .addMessage(
              null,
              new FacesMessage(
                  FacesMessage.SEVERITY_WARN, "Info message", "Errori nell'immissione dei dati"));
      return "addVolo.xhtml";
    }

    CMB.saveVolo(volo);
    FacesContext.getCurrentInstance()
        .addMessage(
            null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info message", "Volo aggiunto!"));
    return "addVolo.xhtml";
  }
  /**
   * Returns the value of the PPR optimization parameter. We currently support "on" and "off"
   *
   * @param context
   * @return
   */
  private static String _getPprOptimization(FacesContext context) {
    ExternalContext external = context.getExternalContext();

    Map<String, Object> applicationMap = external.getApplicationMap();

    // first check if this has been overridden at the application level
    String pprOptimization = (String) applicationMap.get(_PPR_OPTIMIZATION_PROP);

    if (pprOptimization == null) {
      // the value hasn't been set, so check the initialization parameter
      pprOptimization = external.getInitParameter(_PPR_OPTIMIZATION_PROP);

      // default to "off"
      if (pprOptimization == null) pprOptimization = "off";

      // cache in the application so that we don't need to fetch this again
      applicationMap.put(_PPR_OPTIMIZATION_PROP, pprOptimization);
    }

    return pprOptimization;
  }
  @Override
  protected List<UIComponent> getFirstResources(
      FacesContext facesContext, UIComponent uiComponent) {

    List<UIComponent> resources = super.getFirstResources(facesContext, uiComponent);

    // PrimeFaces Theme
    ExternalContext externalContext = facesContext.getExternalContext();
    String primeFacesThemeName = externalContext.getInitParameter(PRIMEFACES_THEME_PARAM);

    if (primeFacesThemeName != null) {
      ELContext elContext = facesContext.getELContext();
      ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory();
      ValueExpression valueExpression =
          expressionFactory.createValueExpression(elContext, primeFacesThemeName, String.class);
      primeFacesThemeName = (String) valueExpression.getValue(elContext);

    } else {
      primeFacesThemeName = PRIMEFACES_THEME_DEFAULT;
    }

    if ((primeFacesThemeName != null) && !primeFacesThemeName.equals(PRIMEFACES_THEME_NONE)) {

      if (resources == null) {
        resources = new ArrayList<UIComponent>();
      }

      String resourceLibrary = PRIMEFACES_THEME_PREFIX + primeFacesThemeName;
      ResourceComponent primeFacesStyleSheet =
          new ResourceComponent(
              facesContext,
              PRIMEFACES_THEME_RESOURCE_NAME,
              resourceLibrary,
              HeadResponseWriter.ELEMENT_HEAD);
      resources.add(primeFacesStyleSheet);
    }

    return resources;
  }
Ejemplo n.º 8
0
  public static void encodeFormSubmissionAjaxInactivityTimeout(FacesContext context) {
    ExternalContext externalContext = context.getExternalContext();
    String paramStr =
        externalContext.getInitParameter(SUBMISSION_AJAX_INACTIVITY_TIMEOUT_CONTEXT_PARAM);

    long inactivityTimeout = DEFAULT_SUBMISSION_AJAX_INACTIVITY_TIMEOUT;
    if (paramStr != null) {
      try {
        final long parameterValue = Long.parseLong(paramStr);
        inactivityTimeout = Math.abs(parameterValue);
      } catch (NumberFormatException e) {
        externalContext.log(
            "Invalid value specified for context parameter named "
                + SUBMISSION_AJAX_INACTIVITY_TIMEOUT_CONTEXT_PARAM
                + ": it must be a number");
      }
    }

    final ScriptBuilder script =
        new ScriptBuilder("O$.setSubmissionAjaxInactivityTimeout(" + inactivityTimeout + ");");

    Rendering.appendOnLoadScript(context, script);
  }
Ejemplo n.º 9
0
  @PostConstruct
  protected void initialize() {
    if (logger.isInfoEnabled()) {
      logger.info("Reading configuration parameters.");
    }

    FacesContext context = FacesContext.getCurrentInstance();
    ExternalContext externalContext = context.getExternalContext();

    ProjectStage stage = context.getApplication().getProjectStage();

    String path = StringUtils.trimToNull(externalContext.getInitParameter(APPLICATION_HOME));
    if (path == null) {
      if (logger.isInfoEnabled()) {
        logger.info("Parameter 'applicationHome' is not set. Using the default path.");
      }

      path = System.getProperty("user.home") + File.separator + ".pivot4j";
    } else if (path.endsWith(File.separator)) {
      path = path.substring(0, path.length() - File.separator.length());
    }

    if (logger.isInfoEnabled()) {
      logger.info("Using application home : {}", path);
    }

    this.applicationHome = new File(path);

    if (!applicationHome.exists()) {
      applicationHome.mkdirs();
    }

    InputStream in = null;

    try {
      String configPath = StringUtils.trimToNull(externalContext.getInitParameter(CONFIG_FILE));
      if (configPath == null || stage == ProjectStage.UnitTest) {
        configPath = path + File.separator + "pivot4j-config.xml";

        File configFile = new File(configPath);

        if (!configFile.exists() || stage == ProjectStage.UnitTest) {
          String defaultConfig = "/WEB-INF/pivot4j-config.xml";

          if (logger.isInfoEnabled()) {
            logger.info("Config file does not exist. Using default : " + defaultConfig);
          }

          ServletContext servletContext = (ServletContext) externalContext.getContext();

          String location = servletContext.getRealPath(defaultConfig);

          if (location != null) {
            configFile = new File(location);
          }
        }

        if (!configFile.exists()) {
          String msg = "Unable to read the default config : " + configFile;
          throw new FacesException(msg);
        }

        in = new FileInputStream(configFile);
      } else {
        URL url;

        if (configPath.startsWith("classpath:")) {
          url = new URL(null, configPath, new ClasspathStreamHandler());
        } else {
          url = new URL(configPath);
        }

        in = url.openStream();

        if (in == null) {
          String msg = "Unable to read config from URL : " + url;
          throw new FacesException(msg);
        }
      }

      this.configuration = readConfiguration(context, in);
    } catch (IOException e) {
      String msg = "Failed to read application config : " + e;
      throw new FacesException(msg, e);
    } catch (ConfigurationException e) {
      String msg = "Invalid application config : " + e;
      throw new FacesException(msg, e);
    } finally {
      IOUtils.closeQuietly(in);
    }

    if (logger.isInfoEnabled()) {
      logger.info("Pivot4J Analytics has been initialized successfully.");
    }

    configuration.addConfigurationListener(
        new ConfigurationListener() {

          @Override
          public void configurationChanged(ConfigurationEvent event) {
            onConfigurationChanged(event);
          }
        });
  }
Ejemplo n.º 10
0
  public static void initProcessor(FacesContext context, ValidationProcessor processor) {
    ExternalContext externalContext = context.getExternalContext();
    String appClientValidation =
        externalContext.getInitParameter(APPLICATION_PARAM_CLIENT_VALIDATION);
    ClientValidationMode clientValidationObj = ClientValidationMode.fromString(appClientValidation);
    processor.setApplicationClientValidation(clientValidationObj);

    String useDefaultServerPresentation =
        externalContext.getInitParameter(APPLICATION_PARAM_DEFAULT_SERVER_VALIDATION_PRESENTATION);
    processor.setUseDefaultServerValidationPresentationForApplication(
        useDefaultServerPresentation == null || Boolean.valueOf(useDefaultServerPresentation));
    String useDefaultClientPresentation =
        externalContext.getInitParameter(APPLICATION_PARAM_DEFAULT_CLIENT_VALIDATION_PRESENTATION);
    processor.setUseDefaultClientValidationPresentationForApplication(
        useDefaultClientPresentation == null || Boolean.valueOf(useDefaultClientPresentation));

    String defaultValidationPresentationClass =
        externalContext.getInitParameter(APPLICATION_PARAM_DEFAULT_VALIDATION_PRESENTATION_CLASS);
    if (defaultValidationPresentationClass != null
        && defaultValidationPresentationClass.length() > 0) {
      try {
        Class presentationClass = Class.forName(defaultValidationPresentationClass);
        Field field = presentationClass.getField("COMPONENT_TYPE");
        if (field == null)
          throw new FacesException(
              "Invalid value of "
                  + APPLICATION_PARAM_DEFAULT_VALIDATION_PRESENTATION_CLASS
                  + " init parameter in web.xml. Seems that the specified class is not a component class since the "
                  + "COMPONENT_TYPE constant field cannot be found in this class: "
                  + defaultValidationPresentationClass);
        String componentType = (String) field.get(null);
        Object presentationInstance = context.getApplication().createComponent(componentType);

        if (!(presentationInstance instanceof UIMessage)) {
          throw new FacesException(
              "Invalid value of "
                  + APPLICATION_PARAM_DEFAULT_VALIDATION_PRESENTATION_CLASS
                  + " init parameter in web.xml. The specified class is not a validation message class as it "
                  + "doesn't extend the javax.faces.component.UIMessage class: "
                  + defaultValidationPresentationClass);
        }
        configureDefaultPresentationInstance(externalContext, presentationInstance);
        processor.setDefaultPresentationInstance((UIMessage) presentationInstance);

      } catch (ClassNotFoundException e) {
        throw new FacesException(
            "Invalid value of "
                + APPLICATION_PARAM_DEFAULT_VALIDATION_PRESENTATION_CLASS
                + " init parameter in web.xml. Could not find the specified class: "
                + defaultValidationPresentationClass,
            e);
      } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
      } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
      } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
      } catch (IntrospectionException e) {
        throw new RuntimeException(e);
      }
    }
  }
Ejemplo n.º 11
0
 public static boolean isOpenFacesValidationDisabled(FacesContext context) {
   ExternalContext externalContext = context.getExternalContext();
   String validationDisabled =
       externalContext.getInitParameter(APPLICATION_PARAM_OPENFACES_VALIDATION_DISABLED);
   return validationDisabled != null && Boolean.valueOf(validationDisabled);
 }