public void createNavigation(
      String type, String owner, String prefix, int startIndex, int endIndex) {
    if (startIndex < endIndex) {
      SiteKey key = new SiteKey(type, owner);
      NavigationContext navCtx = navService.loadNavigation(key);
      if (navCtx != null) {
        LOG.error("Navigation type= " + type + " , owner= " + owner + " already exists");
      } else {
        try {
          if (dataStorage.getPortalConfig(type, owner) == null) {
            dataStorage.create(new PortalConfig(type, owner));
          }
        } catch (Exception ex) {
          LOG.error("Error while load/create site ( type= " + type + " , owner = " + owner, ex);
        }

        navService.saveNavigation(new NavigationContext(key, new NavigationState(0)));
        navCtx = navService.loadNavigation(key);
        NavNode rootNode =
            navService.loadNode(new NavNodeModel(), navCtx, Scope.CHILDREN, null).getNode();
        for (int i = startIndex; i < endIndex; i++) {
          rootNode.addChild(i, prefix + "_" + i);
        }
        navService.saveNode(rootNode.context, null);
      }
    }
  }
  public void createNavigation(String type, String owner, List<String> children) {
    SiteKey key = new SiteKey(type, owner);
    NavigationContext navCtx = navService.loadNavigation(key);
    if (navCtx != null) {
      LOG.error("Navigation type= " + type + " , owner= " + owner + " already exists");
    } else {
      try {
        if (dataStorage.getPortalConfig(type, owner) == null) {
          dataStorage.create(new PortalConfig(type, owner));
        }
      } catch (Exception ex) {
        LOG.error("Error while load/create site ( type= " + type + " , owner = " + owner, ex);
      }

      navCtx = new NavigationContext(key, new NavigationState(0));
      NavNode rootNode =
          navService.loadNode(new NavNodeModel(), navCtx, Scope.CHILDREN, null).getNode();
      int index = 0;
      for (String child : children) {
        rootNode.addChild(index, child);
        index++;
      }
      navService.saveNode(rootNode.context, null);
    }
  }
  @Override
  public final void runBare() throws Throwable {
    // Patch a bug with maven that does not pass properly the system property
    // with an empty value
    if ("org.hsqldb.jdbcDriver".equals(System.getProperty("gatein.test.datasource.driver"))) {
      System.setProperty("gatein.test.datasource.password", "");
    }

    //
    log.info("Running unit test:" + getName());
    for (Map.Entry<?, ?> entry : System.getProperties().entrySet()) {
      if (entry.getKey() instanceof String) {
        String key = (String) entry.getKey();
        log.debug(key + "=" + entry.getValue());
      }
    }

    //
    beforeRunBare();

    //
    try {
      super.runBare();
      log.info("Unit test " + getName() + " completed");
    } catch (Throwable throwable) {
      log.error("Unit test " + getName() + " did not complete", throwable);

      //
      throw throwable;
    } finally {
      afterRunBare();
    }
  }
 @Managed
 @ManagedDescription("Delete node specified by path in existing navigation")
 @Impact(ImpactType.READ)
 public void deleteNode(
     @ManagedDescription("Type of target navigation") @ManagedName("navType") String navType,
     @ManagedDescription("Owner of target navigation") @ManagedName("navOwner") String navOwner,
     @ManagedDescription("Path from root to target node") @ManagedName("pathFromRoot")
         String pathFromRoot) {
   try {
     startTransaction();
     NavigationContext ctx = navService.loadNavigation(new SiteKey(navType, navOwner));
     if (ctx == null) {
       LOG.error("Navigation type= " + navType + " , owner= " + navOwner + " does not exist");
     } else {
       String[] path = pathFromRoot.split("/");
       NavNode rootNode =
           navService
               .loadNode(new NavNodeModel(), ctx, GenericScope.branchShape(path), null)
               .getNode();
       NavNode targetNode = rootNode.getDescendant(path);
       if (targetNode != null && targetNode != rootNode) {
         targetNode.remove();
       }
     }
   } finally {
     endTransaction();
   }
 }
 public void addNodes(
     String navType, String navOwner, String nodePrefix, int startIndex, int endIndex) {
   if (startIndex < endIndex) {
     SiteKey key = new SiteKey(navType, navOwner);
     NavigationContext navCtx = navService.loadNavigation(key);
     if (navCtx == null) {
       LOG.error("Navigation type= " + navType + " , owner= " + navOwner + " does not exist");
     } else {
       NavNode rootNode =
           navService.loadNode(new NavNodeModel(), navCtx, Scope.CHILDREN, null).getNode();
       int index = rootNode.getSize();
       for (int i = startIndex; i < endIndex; i++) {
         String nodeName = nodePrefix + "_" + i;
         if (rootNode.getChild(nodeName) != null) {
           LOG.debug(
               "Navigation node named " + nodeName + " already exist, will not add it anymore");
         } else {
           rootNode.addChild(index, nodeName);
           index++;
         }
       }
       navService.saveNode(rootNode.context, null);
     }
   }
 }
  /**
   * The onInit() method is used to prepare the portal to receive requests.
   *
   * <p>1) Get the WebAppController component from the container 2) Create a new PortalApplication,
   * init it with the ServletConfig object (which contains init params) 3) Register that
   * PortalApplication inside WebAppController 4) Create a new PortalRequestHandler object and
   * register it in the WebAppController
   */
  private void onInit(ServletConfig sConfig, PortalContainer portalContainer) {
    // Keep the old ClassLoader
    final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
    boolean hasChanged = false;
    try {
      ServletConfig config = new PortalServletConfig(sConfig, portalContainer);
      WebAppController controller =
          (WebAppController) portalContainer.getComponentInstanceOfType(WebAppController.class);
      // Set the full classloader of the portal container
      Thread.currentThread().setContextClassLoader(portalContainer.getPortalClassLoader());
      hasChanged = true;

      controller.onHandlersInit(config);
      log.info(
          "The WebAppController has been successfully initialized for the portal '"
              + portalContainer.getName()
              + "'");
    } catch (Throwable t) {
      log.error(
          "The WebAppController could not be initialized for the portal '"
              + portalContainer.getName()
              + "'",
          t);
    } finally {
      if (hasChanged) {
        // Re-set the old classloader
        Thread.currentThread().setContextClassLoader(currentClassLoader);
      }
    }
  }
  /**
   * Called on web application add event if the application contains {@value
   * #GATEIN_CONFIG_RESOURCE} file.
   *
   * @param webApp
   * @param url
   */
  protected void add(final WebApp webApp, final URL url) {

    ServletContext scontext = webApp.getServletContext();
    try {
      /* Validate straight away here before creating the PortalContainerPostInitTask */
      final Document document = GateInResourcesSchemaValidator.validate(url);
      /* Also parse both js and skin resources before creating the PortalContainerPostInitTask */
      final ScriptResources scriptResources =
          new JavascriptConfigParser(scontext, document).parse();
      final List<SkinConfigTask> skinTasks = SkinConfigParser.parse(document);

      /* No exceptions at this point */
      final PortalContainerPostInitTask task =
          new PortalContainerPostInitTask() {
            public void execute(ServletContext scontext, PortalContainer portalContainer) {

              try {

                if (!scriptResources.isEmpty()) {
                  javascriptConfigService.add(scriptResources);
                  scontext.setAttribute(SCRIPT_RESOURCES_ATTR, scriptResources.toImmutable());
                }
                javascriptConfigService.registerContext(webApp);

                if (skinTasks != null && !skinTasks.isEmpty()) {
                  skinService.addSkins(skinTasks, scontext);
                }
                skinService.registerContext(webApp);

              } catch (Exception e) {
                log.error(
                    "Could not register script and skin resources from the context '"
                        + (scontext == null ? "unknown" : scontext.getServletContextName())
                        + "'",
                    e);

                /* try to cleanup if anything went wrong */
                try {
                  remove(webApp);
                } catch (Exception e1) {
                  log.error(
                      "Could not cleanup script and skin resources from the context '"
                          + (scontext == null ? "unknown" : scontext.getServletContextName())
                          + "' after a registration failure",
                      e);
                }
              }
            }
          };
      PortalContainer.addInitTask(scontext, task, portalContainerName);
    } catch (Exception ex) {
      log.error(
          "Could not parse or validate gatein-resources.xml in context '"
              + (scontext == null ? "unknown" : scontext.getServletContextName())
              + "'",
          ex);
    }
  }
 /**
  * @param navType
  * @param navOwner
  * @param absolutePath path from root to targeted node
  * @param nodePrefix
  * @param startIndex
  * @param endIndex
  */
 public void addNodes(
     String navType,
     String navOwner,
     String absolutePath,
     String nodePrefix,
     int startIndex,
     int endIndex) {
   SiteKey key = new SiteKey(navType, navOwner);
   NavigationContext navCtx = navService.loadNavigation(key);
   if (navCtx == null) {
     LOG.error("Navigation type= " + navType + " , owner= " + navOwner + " does not exist");
   } else {
     String[] path = absolutePath.split("/");
     NavNode rootNode =
         navService
             .loadNode(new NavNodeModel(), navCtx, GenericScope.branchShape(path), null)
             .getNode();
     NavNode targetNode = rootNode.getDescendant(path);
     if (targetNode == null) {
       LOG.error(
           "Could not find node specified by path "
               + absolutePath
               + " under navigation type= "
               + navType
               + " , owner= "
               + navOwner);
     } else {
       int index = targetNode.getSize();
       for (int i = startIndex; i < endIndex; i++) {
         String nodeName = nodePrefix + "_" + i;
         if (targetNode.getChild(nodeName) == null) {
           targetNode.addChild(index, nodeName);
           index++;
         }
       }
       navService.saveNode(targetNode.context, null);
     }
   }
 }
  /**
   * Called on web application remove event if the application contains {@value
   * #GATEIN_CONFIG_RESOURCE} file.
   *
   * @param webApp
   */
  protected void remove(WebApp webApp) {
    String contextPath = webApp.getServletContext().getContextPath();
    javascriptConfigService.unregisterServletContext(webApp);
    try {
      javascriptConfigService.remove(contextPath);
    } catch (Exception ex) {
      log.error(
          "An error occurred while removing script resources for the context '"
              + webApp.getServletContext().getServletContextName()
              + "'",
          ex);
    }

    try {
      skinService.removeSkins(SkinDependentManager.getPortalSkins(contextPath));
      skinService.removeSkins(SkinDependentManager.getPortletSkins(contextPath));

      /*
       * Remove skinName defined by the webApp, if no other webApps supports the skinName
       */
      Set<String> supportedSkins = SkinDependentManager.getSkinNames(contextPath);
      if (supportedSkins != null) {
        for (String skin : supportedSkins) {
          if (SkinDependentManager.skinNameIsRemovable(skin, contextPath)) {
            skinService.removeSupportedSkin(skin);
          }
        }
      }

      // Update the 'skinDependentManager'
      SkinDependentManager.clearAssociatedSkins(contextPath);
    } catch (Exception e) {
      log.error(e.getMessage(), e);
    }
    skinService.unregisterServletContext(webApp);
  }
 @Managed
 @ManagedDescription("Add nodes into root node of existing navigation")
 @Impact(ImpactType.READ)
 public void insertNodes(
     @ManagedDescription("Type of target navigation") @ManagedName("navType") String navType,
     @ManagedDescription("Owner of target navigation") @ManagedName("navOwner") String navOwner,
     @ManagedDescription("Prefix of new node names") @ManagedName("prefix") String nodePrefix,
     @ManagedDescription("Starting index") @ManagedName("startIndex") String startIndex,
     @ManagedDescription("Ending index") @ManagedName("endIndex") String endIndex) {
   int _startIndex = Integer.parseInt(startIndex);
   int _endIndex = Integer.parseInt(endIndex);
   // Invoke from JMX bean will not go through GateIn's servlet filter. Therefore, we need to
   // open/close transaction
   try {
     startTransaction();
     addNodes(navType, navOwner, nodePrefix, _startIndex, _endIndex);
   } catch (Exception ex) {
     LOG.error("Failed to insert new nodes", ex);
   } finally {
     endTransaction();
   }
 }
 @Managed
 @ManagedDescription("Create new navigation")
 @Impact(ImpactType.READ)
 public void createNavs(
     @ManagedDescription("Type of new navigation") @ManagedName("navType") String type,
     @ManagedDescription("Owner of new navigation") @ManagedName("navOwner") String owner,
     @ManagedDescription("Prefix of new node names") @ManagedName("prefix") String prefix,
     @ManagedDescription("Starting index") @ManagedName("startIndex") String startIndex,
     @ManagedDescription("Ending index") @ManagedName("endIndex") String endIndex) {
   int _startIndex = Integer.parseInt(startIndex);
   int _endIndex = Integer.parseInt(endIndex);
   // Invoke from JMX bean will not go through GateIn's servlet filter. Therefore, we need to
   // open/close transaction
   try {
     startTransaction();
     createNavigation(type, owner, prefix, _startIndex, _endIndex);
   } catch (Exception ex) {
     LOG.error("Failed to create new navigation", ex);
   } finally {
     endTransaction();
   }
 }
  static {
    // Detecting version from maven properties
    // empty value is ok
    String version = "";
    URL url = ResourceRequestHandler.class.getClassLoader().getResource(PATH);
    if (url != null) {
      log.debug("Loading resource serving version from " + url);
      InputStream in = null;
      try {
        in = url.openStream();
        Properties props = new Properties();
        props.load(in);
        version = props.getProperty("version");
      } catch (IOException e) {
        log.error("Could not read properties from " + url, e);
      } finally {
        IOTools.safeClose(in);
      }
    }

    //
    log.info("Use version \"" + version + "\" for resource serving");
    VERSION = version;
  }
Esempio n. 13
0
  public void doImport() {
    if (portalContainer == null) {
      portalContainer = Utils.getUserInput("Container name (ie portal)", level);
    }

    try {
      client =
          PortalObjectsMgmtClient.Factory.create(
              InetAddress.getByName(host), port, username, password, portalContainer);
    } catch (UnknownHostException e) {
      System.err.println("Unknown host name " + host + ". See log for more details.");
      log.error("Exception retrieving host " + host + " by name.", e);
      System.exit(1);
    }

    if (overwrite == null) {
      String ow =
          Utils.getUserInput(
              "Do you wish to fully overwrite all data defined in import file (N) ? Y/N", level);
      if ("Y".equalsIgnoreCase(ow)) {
        overwrite = Boolean.TRUE;
      } else {
        overwrite = Boolean.FALSE;
      }
    }

    if (overwrite && !force) {
      System.out.println(
          "\nOverwrite set to true. This means that all data for a site will be overwritten and any data not defined will be deleted.");
      String proceed = Utils.getUserInput("Do you wish to proceed (N) ? Y/N", level);
      if (!"Y".equalsIgnoreCase(proceed)) {
        System.exit(0);
      }
    }

    log.info("Starting import of file " + importFile + " for portal container " + portalContainer);
    try {
      ImportContext context = client.importFromZip(importFile);
      if (isEmptyContext(context)) {
        System.out.println(
            "Nothing to import. " + importFile + " did not contain anything to import.");
        log.warn("Nothing imported. Zip file did not contain any data for import.");
        System.exit(0);
      }

      if (overwrite) {
        log.info("Overwrite set to true, overwriting all data.");
      }
      context.setOverwrite(overwrite);
      client.importContext(context);
      log.info("Import was successful.");
    } catch (IOException e) {
      System.err.println("Exception reading zip file. See log for more details.");
      log.error("IOException reading zip file " + importFile, e);
    } catch (ClientException e) {
      System.err.println("Client exception during import. See log for more details.");
      log.error("ClientException during import.", e);
    } catch (Throwable t) {
      System.err.println("Unknown exception occurred during import.  See log for more details.");
      log.error("Uknown exception during import.", t);
    }
  }
  @Override
  public boolean execute(ControllerContext context) throws Exception {
    String resourceParam = context.getParameter(RESOURCE_QN);
    String scopeParam = context.getParameter(SCOPE_QN);

    //
    if (scopeParam != null && resourceParam != null) {
      String compressParam = context.getParameter(COMPRESS_QN);
      String lang = context.getParameter(LANG_QN);
      String moduleParam = context.getParameter(MODULE_QN);

      //
      Locale locale = null;
      if (lang != null && lang.length() > 0) {
        locale = I18N.parseTagIdentifier(lang);
      }

      //
      ResourceScope scope;
      try {
        scope = ResourceScope.valueOf(ResourceScope.class, scopeParam);
      } catch (IllegalArgumentException e) {
        HttpServletResponse response = context.getResponse();
        String msg = "Unrecognized scope " + scopeParam;
        log.error(msg);
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
        return true;
      }

      //
      ResourceId resource = new ResourceId(scope, resourceParam);

      ScriptKey key = new ScriptKey(resource, moduleParam, "min".equals(compressParam), locale);

      //
      ScriptResult result = cache.get(context, key);
      HttpServletResponse response = context.getResponse();
      HttpServletRequest request = context.getRequest();

      //
      if (result instanceof ScriptResult.Resolved) {
        ScriptResult.Resolved resolved = (ScriptResult.Resolved) result;

        // Content type + charset
        response.setContentType("text/javascript");
        response.setCharacterEncoding("UTF-8");

        // One hour caching
        // make this configurable later
        response.setHeader("Cache-Control", "max-age:3600");
        response.setDateHeader("Expires", System.currentTimeMillis() + 3600 * 1000);

        // Set content length
        response.setContentLength(resolved.bytes.length);

        long ifModifiedSince = request.getDateHeader(IF_MODIFIED_SINCE);
        if (resolved.isModified(ifModifiedSince)) {
          response.setDateHeader(ResourceRequestFilter.LAST_MODIFIED, resolved.lastModified);
          // Send bytes
          ServletOutputStream out = response.getOutputStream();
          try {
            out.write(resolved.bytes);
          } finally {
            Safe.close(out);
          }
        } else {
          response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        }
      } else if (result instanceof ScriptResult.Error) {
        ScriptResult.Error error = (ScriptResult.Error) result;
        log.error("Could not render script " + key + "\n:" + error.message);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      } else {
        String msg = "Resource " + key + " cannot be found";
        log.error(msg);
        response.sendError(HttpServletResponse.SC_NOT_FOUND, msg);
      }
    } else {
      HttpServletResponse response = context.getResponse();
      String msg = "Missing scope or resource param";
      log.error(msg);
      response.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
    }

    //
    return true;
  }