/*
   * retrieves the data streams for the metadata referenced by domainId. This could be a single .xmi file or an .xmi
   * file and multiple .properties files.
   */
  public Map<String, InputStream> getDomainFilesData(final String domainId) {
    Set<RepositoryFile> metadataFiles;
    lock.readLock().lock();
    try {
      metadataFiles = metadataMapping.getFiles(domainId);
    } finally {
      lock.readLock().unlock();
    }

    Map<String, InputStream> values = new HashMap<String, InputStream>(metadataFiles.size());
    for (RepositoryFile repoFile : metadataFiles) {
      RepositoryFileInputStream is;
      try {
        is = new RepositoryFileInputStream(repoFile);
      } catch (Exception e) {
        return null; // This pretty much ensures an exception will be thrown later and passed to the
        // client
      }
      String fileName =
          repoFile.getName().endsWith(".properties")
              ? repoFile.getName()
              : domainId + (domainId.endsWith(".xmi") ? "" : ".xmi");
      values.put(fileName, is);
    }
    return values;
  }
 public RepositoryFile createFolder(
     final Serializable parentFolderId, final RepositoryFile file, final String versionMessage) {
   return callLogThrow(
       new Callable<RepositoryFile>() {
         public RepositoryFile call() throws Exception {
           return delegatee.createFolder(parentFolderId, file, versionMessage);
         }
       },
       Messages.getInstance()
           .getString("ExceptionLoggingDecorator.createFolder", file.getName())); // $NON-NLS-1$
 }
  /** Performs the process of reloading the domain information from the repository */
  private void internalReloadDomains() {
    lock.writeLock().lock();
    try {
      metadataMapping.reset();

      // Reload the metadata about the metadata (that was fun to say)
      final List<RepositoryFile> children = repository.getChildren(getMetadataDir().getId(), "*");
      if (logger.isTraceEnabled()) {
        logger.trace("\tFound " + children.size() + " files in the repository");
      }
      for (final RepositoryFile child : children) {
        if (getAclHelper().canAccess(child, READ)) {
          // Get the metadata for this file
          final Map<String, Serializable> fileMetadata = repository.getFileMetadata(child.getId());
          if (fileMetadata == null
              || StringUtils.isEmpty((String) fileMetadata.get(PROPERTY_NAME_DOMAIN_ID))) {
            if (logger.isWarnEnabled()) {
              logger.warn(
                  messages.getString(
                      "PentahoMetadataDomainRepository.WARN_0001_FILE_WITHOUT_METADATA",
                      child.getName()));
            }
            continue;
          }
          final String domainId = (String) fileMetadata.get(PROPERTY_NAME_DOMAIN_ID);
          final String type = (String) fileMetadata.get(PROPERTY_NAME_TYPE);
          final String locale = (String) fileMetadata.get(PROPERTY_NAME_LOCALE);
          if (logger.isTraceEnabled()) {
            logger.trace(
                "\tprocessing file [type="
                    + type
                    + " : domainId="
                    + domainId
                    + " : locale="
                    + locale
                    + "]");
          }

          // Save the data in the map
          if (StringUtils.equals(type, TYPE_DOMAIN)) {
            metadataMapping.addDomain(domainId, child);
          } else if (StringUtils.equals(type, TYPE_LOCALE)) {
            metadataMapping.addLocale(domainId, locale, child);
          }
        }
      }

      needToReload = false;
    } finally {
      lock.writeLock().unlock();
    }
  }
 /**
  * there are certain extensions that get imported with locale maps (incorrectly?) like .png only
  * export locale maps for the list from importexport.xml
  *
  * @param repositoryFile
  * @return true if supported
  */
 private boolean supportedLocaleFileExt(RepositoryFile repositoryFile) {
   boolean ans = true;
   String ext = repositoryFile.getName();
   if (!repositoryFile.isFolder()) {
     int idx = ext.lastIndexOf(".");
     if (idx > 0) {
       ext = ext.substring(idx, ext.length());
     }
     List<String> exportList = getLocaleExportList();
     if (exportList != null) {
       ans = exportList.contains(ext);
     }
   }
   return ans;
 }
 protected String toString(final RepositoryFile file) {
   try {
     final Map<String, Serializable> fileMetadata = repository.getFileMetadata(file.getId());
     return "[type="
         + fileMetadata.get(PROPERTY_NAME_TYPE)
         + " : domain="
         + fileMetadata.get(PROPERTY_NAME_DOMAIN_ID)
         + " : locale="
         + fileMetadata.get(PROPERTY_NAME_LOCALE)
         + " : filename="
         + file.getName()
         + "]";
   } catch (Throwable ignore) {
     // ignore
   }
   return "null";
 }
  @Test
  public void testGetFolder() throws Exception {
    final MockUnifiedRepository repository =
        new MockUnifiedRepository(new SpringSecurityCurrentUserProvider());
    final RepositoryUtils repositoryUtils = new RepositoryUtils(repository);

    RepositoryFile test = repositoryUtils.getFolder("/public/one/two/three", true, true, null);
    assertNotNull(test);
    assertEquals("The folder name is invalid", "three", test.getName());
    assertEquals("The path is invalid", "/public/one/two/three", test.getPath());
    assertTrue("The folder should be defined as a folder", test.isFolder());

    // Make sure it created the parents
    RepositoryFile one = repositoryUtils.getFolder("/public/one", false, false, null);
    assertNotNull(one);
    RepositoryFile two = repositoryUtils.getFolder("/public/one/two", false, false, null);
    assertNotNull(two);
  }
  private NamedParams getMeta(RepositoryFile file) throws KettleException {

    NamedParams meta = null;

    if (file != null) {

      String extension = FilenameUtils.getExtension(file.getName());
      Repository repo = PDIImportUtil.connectToRepository(null);

      if ("ktr".equalsIgnoreCase(extension)) {

        meta = new TransMeta(convertTransformation(file.getId()), repo, true, null, null);

      } else if ("kjb".equalsIgnoreCase(extension)) {

        meta = new JobMeta(convertJob(file.getId()), repo, null);
      }
    }

    return meta;
  }
 protected Properties loadProperties(final RepositoryFile bundle) {
   try {
     Properties properties = null;
     final SimpleRepositoryFileData bundleData =
         repository.getDataForRead(bundle.getId(), SimpleRepositoryFileData.class);
     if (bundleData != null) {
       properties = new Properties();
       properties.load(bundleData.getStream());
     } else {
       if (logger.isWarnEnabled()) {
         logger.warn("Could not load properties from repository file: " + bundle.getName());
       }
     }
     return properties;
   } catch (IOException e) {
     throw new UnifiedRepositoryException(
         messages.getErrorString(
             "PentahoMetadataDomainRepository.ERROR_0008_ERROR_IN_REPOSITORY",
             e.getLocalizedMessage()),
         e);
   }
 }
  @Test
  public void testGetFile() throws Exception {
    final MockUnifiedRepository repository =
        new MockUnifiedRepository(new SpringSecurityCurrentUserProvider());
    final RepositoryUtils repositoryUtils = new RepositoryUtils(repository);

    final SimpleRepositoryFileData data =
        new SimpleRepositoryFileData(
            new ByteArrayInputStream("Test".getBytes()), "UTF-8", "text/plain");
    RepositoryFile test =
        repositoryUtils.getFile("/public/one/two/three.prpt", data, true, true, null);
    assertNotNull(test);
    assertEquals("The filename is invalid", "three.prpt", test.getName());
    assertEquals("The path is invalid", "/public/one/two/three.prpt", test.getPath());
    assertFalse("The file should not be defined as a folder", test.isFolder());

    // Make sure it created the parents
    RepositoryFile one = repositoryUtils.getFolder("/public/one", false, false, null);
    assertNotNull(one);
    RepositoryFile two = repositoryUtils.getFolder("/public/one/two", false, false, null);
    assertNotNull(two);
  }
  /**
   * for each locale stored in in Jcr create a .locale file with the stored node properties
   *
   * @param zos
   * @param repositoryFile
   * @param filePath
   * @throws IOException
   */
  private void createLocales(
      RepositoryFile repositoryFile, String filePath, boolean isFolder, OutputStream outputStrean)
      throws IOException {
    ZipEntry entry;
    String zipName;
    String name;
    String localeName;
    Properties properties;
    ZipOutputStream zos = (ZipOutputStream) outputStrean;
    // only process files and folders that we know will have locale settings
    if (supportedLocaleFileExt(repositoryFile)) {
      List<LocaleMapDto> locales = getAvailableLocales(repositoryFile.getId());
      zipName = getZipEntryName(repositoryFile, filePath);
      name = repositoryFile.getName();
      for (LocaleMapDto locale : locales) {
        localeName = locale.getLocale().equalsIgnoreCase("default") ? "" : "_" + locale.getLocale();
        if (isFolder) {
          zipName = getZipEntryName(repositoryFile, filePath) + "index";
          name = "index";
        }

        properties =
            unifiedRepository.getLocalePropertiesForFileById(
                repositoryFile.getId(), locale.getLocale());
        if (properties != null) {
          properties.remove("jcr:primaryType"); // Pentaho Type
          InputStream is = createLocaleFile(name + localeName, properties, locale.getLocale());
          if (is != null) {
            entry = new ZipEntry(zipName + localeName + LOCALE_EXT);
            zos.putNextEntry(entry);
            IOUtils.copy(is, outputStrean);
            zos.closeEntry();
            is.close();
          }
        }
      }
    }
  }
 private RepositoryFile internalCreateFolder(
     final Session session,
     final Serializable parentFolderId,
     final RepositoryFile folder,
     final RepositoryFileAcl acl,
     final String versionMessage)
     throws RepositoryException {
   PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
   JcrRepositoryFileUtils.checkoutNearestVersionableFileIfNecessary(
       session, pentahoJcrConstants, parentFolderId);
   Node folderNode =
       JcrRepositoryFileUtils.createFolderNode(
           session, pentahoJcrConstants, parentFolderId, folder);
   // we must create the acl during checkout
   JcrRepositoryFileAclUtils.createAcl(
       session,
       pentahoJcrConstants,
       folderNode.getIdentifier(),
       acl == null ? defaultAclHandler.createDefaultAcl(folder) : acl);
   session.save();
   if (folder.isVersioned()) {
     JcrRepositoryFileUtils.checkinNearestVersionableNodeIfNecessary(
         session, pentahoJcrConstants, folderNode, versionMessage);
   }
   JcrRepositoryFileUtils.checkinNearestVersionableFileIfNecessary(
       session,
       pentahoJcrConstants,
       parentFolderId,
       Messages.getInstance()
           .getString(
               "JcrRepositoryFileDao.USER_0001_VER_COMMENT_ADD_FOLDER",
               folder.getName(),
               (parentFolderId == null
                   ? "root"
                   : parentFolderId.toString()))); // $NON-NLS-1$ //$NON-NLS-2$
   return JcrRepositoryFileUtils.nodeToFile(
       session, pentahoJcrConstants, pathConversionHelper, lockHelper, folderNode);
 }
Пример #12
0
  /**
   * This method executes an xaction with forcePrompt=true and outputPreference=PARAMETERS, allowing
   * for the xaction to render the secure filter appropriately when being executed in the background
   * or while being scheduled.
   *
   * @param file the location of the xaction
   * @param httpServletRequest the request object
   * @param httpServletResponse the response object
   * @param userSession the user session
   * @return potential response message
   * @throws Exception
   */
  @SuppressWarnings({"unchecked", "rawtypes"})
  public static String executeScheduleUi(
      RepositoryFile file,
      HttpServletRequest httpServletRequest,
      HttpServletResponse httpServletResponse,
      IPentahoSession userSession,
      IMimeTypeListener mimeTypeListener)
      throws Exception {
    IParameterProvider requestParams = new HttpRequestParameterProvider(httpServletRequest);
    IRuntimeContext runtime = null;
    try {
      HttpSessionParameterProvider sessionParameters =
          new HttpSessionParameterProvider(userSession);
      HttpRequestParameterProvider requestParameters =
          new HttpRequestParameterProvider(httpServletRequest);

      boolean doMessages =
          "true"
              .equalsIgnoreCase(
                  requestParams.getStringParameter(
                      "debug", "false")); // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      boolean doWrapper =
          "true"
              .equalsIgnoreCase(
                  requestParams.getStringParameter(
                      "wrapper", "true")); // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

      IOutputHandler outputHandler =
          createOutputHandler(
              httpServletResponse, getOutputStream(httpServletResponse, doMessages));
      if (mimeTypeListener == null) {
        mimeTypeListener = new HttpMimeTypeListener(httpServletRequest, httpServletResponse, null);
      }
      outputHandler.setMimeTypeListener(mimeTypeListener);
      outputHandler.setSession(userSession);

      Map parameterProviders = new HashMap();
      parameterProviders.put("request", requestParameters); // $NON-NLS-1$
      parameterProviders.put("session", sessionParameters); // $NON-NLS-1$
      createOutputFileName(file, outputHandler);
      int outputPreference = IOutputHandler.OUTPUT_TYPE_PARAMETERS;
      outputHandler.setOutputPreference(outputPreference);
      List messages = new ArrayList();

      // forcePrompt=true when displaying the scheduling UI
      runtime =
          executeInternal(
              file,
              requestParams,
              httpServletRequest,
              outputHandler,
              parameterProviders,
              userSession,
              true,
              messages);
      String str =
          postExecute(
              runtime,
              doMessages,
              doWrapper,
              outputHandler,
              parameterProviders,
              httpServletRequest,
              httpServletResponse,
              messages,
              false);
      return str;
    } catch (Exception e) {
      logger.error(
          Messages.getInstance()
              .getString("XactionUtil.ERROR_EXECUTING_ACTION_SEQUENCE", file.getName()),
          e); //$NON-NLS-1$
      throw e;
    } finally {
      if (runtime != null) {
        runtime.dispose();
      }
    }
  }
  public void createReportContent(
      final OutputStream outputStream,
      final Serializable fileId,
      final String path,
      final boolean forceDefaultOutputTarget)
      throws Exception {
    final long start = System.currentTimeMillis();
    final Map<String, Object> inputs = contentGenerator.createInputs();

    AuditHelper.audit(
        userSession.getId(),
        userSession.getName(),
        path,
        contentGenerator.getObjectName(),
        getClass().getName(),
        MessageTypes.INSTANCE_START,
        contentGenerator.getInstanceId(),
        "",
        0,
        contentGenerator); //$NON-NLS-1$

    String result = MessageTypes.INSTANCE_END;
    StagingHandler reportStagingHandler = null;
    try {
      final Object rawSessionId = inputs.get(ParameterXmlContentHandler.SYS_PARAM_SESSION_ID);
      if ((rawSessionId instanceof String) == false || "".equals(rawSessionId)) {
        inputs.put(ParameterXmlContentHandler.SYS_PARAM_SESSION_ID, UUIDUtil.getUUIDAsString());
      }

      // produce rendered report
      final SimpleReportingComponent reportComponent = new SimpleReportingComponent();
      reportComponent.setReportFileId(fileId);
      reportComponent.setPaginateOutput(true);
      reportComponent.setForceDefaultOutputTarget(forceDefaultOutputTarget);
      reportComponent.setDefaultOutputTarget(HtmlTableModule.TABLE_HTML_PAGE_EXPORT_TYPE);
      if (path.endsWith(".prpti")) {
        reportComponent.setForceUnlockPreferredOutput(true);
      }
      reportComponent.setInputs(inputs);

      final MasterReport report = reportComponent.getReport();
      final StagingMode stagingMode = getStagingMode(inputs, report);
      reportStagingHandler = new StagingHandler(outputStream, stagingMode, this.userSession);

      if (reportStagingHandler.isFullyBuffered()) {
        // it is safe to disable the buffered writing for the report now that we have a
        // extra buffering in place.
        report.getReportConfiguration().setConfigProperty(FORCED_BUFFERED_WRITING, "false");
      }

      reportComponent.setOutputStream(reportStagingHandler.getStagingOutputStream());

      // the requested mime type can be null, in that case the report-component will resolve the
      // desired
      // type from the output-target.
      // Hoever, the report-component will inspect the inputs independently from the mimetype here.

      final IUnifiedRepository repository =
          PentahoSystem.get(IUnifiedRepository.class, userSession);
      final RepositoryFile file = repository.getFileById(fileId);

      // add all inputs (request parameters) to report component
      final String mimeType = reportComponent.getMimeType();

      // If we haven't set an accepted page, -1 will be the default, which will give us a report
      // with no pages. This default is used so that when we do our parameter interaction with the
      // engine we can spend as little time as possible rendering unused pages, making it no pages.
      // We are going to intentionally reset the accepted page to the first page, 0, at this point,
      // if the accepted page is -1.
      final String outputTarget = reportComponent.getComputedOutputTarget();
      if (HtmlTableModule.TABLE_HTML_PAGE_EXPORT_TYPE.equals(outputTarget)
          && reportComponent.getAcceptedPage() < 0) {
        reportComponent.setAcceptedPage(0);
      }

      if (logger.isDebugEnabled()) {
        logger.debug(
            Messages.getInstance()
                .getString(
                    "ReportPlugin.logStartGenerateContent",
                    mimeType, //$NON-NLS-1$
                    outputTarget,
                    String.valueOf(reportComponent.getAcceptedPage())));
      }

      HttpServletResponse response = null;
      boolean streamToBrowser = false;
      final IParameterProvider pathProviders = contentGenerator.getParameterProviders().get("path");
      if (pathProviders != null) {
        final Object httpResponse = pathProviders.getParameter("httpresponse");
        if (httpResponse
            instanceof HttpServletResponse) { // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
          response = (HttpServletResponse) httpResponse; // $NON-NLS-1$ //$NON-NLS-2$
          if (reportStagingHandler.getStagingMode() == StagingMode.THRU) {
            // Direct back - check output stream...
            final OutputStream respOutputStream = response.getOutputStream();
            if (respOutputStream == outputStream) {
              //
              // Massive assumption here -
              // Assume the container returns the same object on successive calls to
              // response.getOutputStream()
              streamToBrowser = true;
            }
          }
        }
      }

      final String extension = MimeHelper.getExtension(mimeType);
      String filename = file.getName();
      if (filename.lastIndexOf(".") != -1) { // $NON-NLS-1$
        filename = filename.substring(0, filename.lastIndexOf(".")); // $NON-NLS-1$
      }
      String disposition =
          "inline; filename*=UTF-8''"
              + RepositoryPathEncoder.encode(
                  RepositoryPathEncoder.encodeRepositoryPath(filename + extension));

      final boolean validates = reportComponent.validate();
      if (!validates) {
        sendErrorResponse(response, outputStream, reportStagingHandler);
      } else {
        if (response != null) {
          // Send headers before we begin execution
          response.setHeader("Content-Disposition", disposition);
          response.setHeader("Content-Description", file.getName()); // $NON-NLS-1$
          response.setHeader("Cache-Control", "private, max-age=0, must-revalidate");
        }
        if (reportComponent.execute()) {
          if (response != null) {
            if (reportStagingHandler.canSendHeaders()) {
              response.setHeader("Content-Disposition", disposition);
              response.setHeader("Content-Description", file.getName()); // $NON-NLS-1$
              response.setHeader("Cache-Control", "private, max-age=0, must-revalidate");
              response.setContentLength(reportStagingHandler.getWrittenByteCount());
            }
          }
          if (logger.isDebugEnabled()) {
            logger.debug(
                Messages.getInstance()
                    .getString(
                        "ReportPlugin.logEndGenerateContent",
                        String.valueOf(reportStagingHandler.getWrittenByteCount()))); // $NON-NLS-1$
          }
          reportStagingHandler.complete(); // will copy bytes to final destination...

        } else { // failed execution
          sendErrorResponse(response, outputStream, reportStagingHandler);
        }
      }
    } catch (Exception ex) {
      result = MessageTypes.INSTANCE_FAILED;
      throw ex;
    } finally {
      if (reportStagingHandler != null) {
        reportStagingHandler.close();
      }
      final long end = System.currentTimeMillis();
      AuditHelper.audit(
          userSession.getId(),
          userSession.getName(),
          path,
          contentGenerator.getObjectName(),
          getClass().getName(),
          result,
          contentGenerator.getInstanceId(),
          "",
          ((float) (end - start) / 1000),
          contentGenerator); //$NON-NLS-1$
    }
  }