Esempio n. 1
0
  private MavenArtifactWrapper getArtifactContent(MavenMetaData metadata)
      throws MavenRepositoryException {
    // List of criterias and the parameters associated
    List<String> criteria = new ArrayList<String>();
    List<Object> parameters = new ArrayList<Object>();
    criteria.add("@maven.artifactId = ?"); // $NON-NLS-1$
    criteria.add("@maven.groupId = ?"); // $NON-NLS-1$
    criteria.add("@maven.type = ?"); // $NON-NLS-1$

    parameters.add(metadata.getArtifactId());
    parameters.add(metadata.getGroupId());
    // If there is a parent type (in case of sha1 or md5, it is passed as
    // parameter
    if (StringUtils.isNotBlank(metadata.getParentType())) {
      parameters.add(metadata.getParentType());
    } else {
      parameters.add(metadata.getType());
    }
    // Not always it is passed the maven version. This is the case when it
    // is requested a file (normally maven-metadata.xml) that is stored in
    // the artifact subfolder
    if (StringUtils.isNotBlank(metadata.getVersion())) {
      criteria.add("@maven.version = ?"); // $NON-NLS-1$
      parameters.add(metadata.getVersion());
    }
    // If it is included a classfier it is added as parameter.
    if (StringUtils.isNotBlank(metadata.getClassifier())) {
      criteria.add("@maven.classifier = ?"); // $NON-NLS-1$
      parameters.add(metadata.getClassifier());
    } else {
      criteria.add("xp2:not(@maven.classifier)"); // $NON-NLS-1$
    }

    if (StringUtils.isNotBlank(metadata.getSnapshotId())) {
      criteria.add("@maven.snapshot.id = ?"); // $NON-NLS-1$
      parameters.add(metadata.getSnapshotId());
    } else {
      criteria.add("xp2:not(@maven.snapshot.id)"); // $NON-NLS-1$
    }

    ArtifactSet artifactSet = null;
    BaseArtifactType baseArtifact = null;
    try {
      // query based on the previous criterias
      artifactSet = query(criteria, parameters);
      if (artifactSet.size() >= 1) {
        // Found some content!
        baseArtifact = artifactSet.iterator().next();
      }
    } catch (SrampAtomException e) {
      throw new MavenRepositoryException(Messages.i18n.format(""), e); // $NON-NLS-1$
    } finally {
      if (artifactSet != null) {
        artifactSet.close();
      }
    }
    // If the artifact returned is not null, then the content will be
    // retrieved
    if (baseArtifact != null) {
      PersistenceManager persistenceManager = PersistenceFactory.newInstance();
      final InputStream artifactContent;
      ArtifactType artifactType = ArtifactType.valueOf(baseArtifact.getArtifactType());
      Date lastModifiedDate = null;
      if (baseArtifact.getLastModifiedTimestamp() != null) {
        lastModifiedDate = baseArtifact.getLastModifiedTimestamp().toGregorianCalendar().getTime();
      }
      int contentLength = -1;
      MavenFileExtensionEnum ext = MavenFileExtensionEnum.value(metadata.getType());
      if (ext != null && StringUtils.isNotBlank(ext.getCustomProperty())) {
        // we need to set the input stream with the value of the custom
        // property
        String content = SrampModelUtils.getCustomProperty(baseArtifact, ext.getCustomProperty());
        if (StringUtils.isNotBlank(content)) {
          artifactContent = new ByteArrayInputStream(content.getBytes());
          contentLength = content.length();
        } else {
          logger.info(
              Messages.i18n.format(
                  "maven.resource.get.subcontent.not.found",
                  baseArtifact.getUuid(),
                  ext.getCustomProperty())); // $NON-NLS-1$
          return null;
        }

      } else {
        // we need to set the input stream with the artifact content
        try {
          artifactContent =
              persistenceManager.getArtifactContent(baseArtifact.getUuid(), artifactType);
        } catch (SrampException e) {
          logger.error(
              Messages.i18n.format("maven.resource.get.content.error", baseArtifact.getUuid()),
              e); //$NON-NLS-1$

          throw new MavenRepositoryException(
              Messages.i18n.format(
                  "maven.resource.get.content.error", //$NON-NLS-1$
                  baseArtifact.getUuid()),
              e);
        }
        String contentSize =
            baseArtifact.getOtherAttributes().get(SrampConstants.SRAMP_CONTENT_SIZE_QNAME);
        if (StringUtils.isNotBlank(contentSize)) {
          contentLength = Integer.parseInt(contentSize);
        }
      }

      MavenArtifactWrapper wrapper =
          new MavenArtifactWrapper(
              artifactContent,
              contentLength,
              lastModifiedDate,
              metadata.getFileName(),
              artifactType.getMimeType());
      return wrapper;
    } else {
      logger.error(
          Messages.i18n.format("maven.resource.item.null", metadata.toString())); // $NON-NLS-1$
      // Return null so that the servlet can return a 404
      return null;
    }
  }