/** * Uploads a single deployment to S-RAMP. * * @param deploymentType * @param fileName * @param client * @param tempFile * @param responseParams * @param version * @throws Exception */ private void uploadSingleDeployment( String deploymentType, String fileName, File tempFile, Map<String, String> responseParams, String version) throws Exception { ArtifactType at = ArtifactType.valueOf(deploymentType); if (at.isExtendedType()) { at = ArtifactType.ExtendedDocument(at.getExtendedType()); } String uuid = null; // First, upload the deployment InputStream contentStream = null; try { contentStream = FileUtils.openInputStream(tempFile); BaseArtifactType artifact = at.newArtifactInstance(); artifact.setName(fileName); artifact.setVersion(version); artifact = clientAccessor.getClient().uploadArtifact(artifact, contentStream); responseParams.put("model", at.getArtifactType().getModel()); // $NON-NLS-1$ responseParams.put("type", at.getArtifactType().getType()); // $NON-NLS-1$ responseParams.put("uuid", artifact.getUuid()); // $NON-NLS-1$ uuid = artifact.getUuid(); } finally { IOUtils.closeQuietly(contentStream); } // Try to expand the artifact (works if an expander is available for the given artifact type). ZipToSrampArchive j2sramp = null; SrampArchive archive = null; try { j2sramp = ZipToSrampArchiveRegistry.createExpander(at, tempFile); if (j2sramp != null) { j2sramp.setContextParam(DefaultMetaDataFactory.PARENT_UUID, uuid); archive = j2sramp.createSrampArchive(); clientAccessor.getClient().uploadBatch(archive); } } finally { SrampArchive.closeQuietly(archive); ZipToSrampArchive.closeQuietly(j2sramp); } }
private String uploadArtifact(MavenMetaData metadata, InputStream content) throws MavenRepositoryException { String uuid = null; if (content == null) { throw new MavenRepositoryException( Messages.i18n.format("maven.resource.upload.no.content")); // $NON-NLS-1$ } String fileName = metadata.getFileName(); PersistenceManager persistenceManager = PersistenceFactory.newInstance(); // We need to query firstly to check if there is an existing item. If // there is an existing item, then it would be updated // Adding the criterias and parameters 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 (StringUtils.isNotBlank(metadata.getParentType())) { parameters.add(metadata.getParentType()); } else { parameters.add(metadata.getType()); } if (StringUtils.isNotBlank(metadata.getVersion())) { criteria.add("@maven.version = ?"); // $NON-NLS-1$ parameters.add(metadata.getVersion()); } else { criteria.add("xp2:not(@maven.version)"); // $NON-NLS-1$ } if (StringUtils.isNotBlank(metadata.getParentFileName())) { criteria.add("@name = ?"); // $NON-NLS-1$ parameters.add(metadata.getParentFileName()); } 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 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("maven.resource.upload.sramp.query.error", metadata.toString()), e); //$NON-NLS-1$ } finally { if (artifactSet != null) { artifactSet.close(); } } if (MavenFileExtensionEnum.value(metadata.getType()) != null) { if (baseArtifact != null) { boolean update = false; ArtifactType artifactType = ArtifactType.valueOf(baseArtifact.getArtifactType()); if (metadata.getType().equals(MavenFileExtensionEnum.HASH_MD5.getExtension()) || metadata.getType().equals(MavenFileExtensionEnum.HASH_SHA1.getExtension())) { String content_value = ""; // $NON-NLS-1$ try { content_value = IOUtils.toString(content); } catch (IOException e1) { throw new MavenRepositoryException( Messages.i18n.format("maven.resource.upload.sramp.error", metadata.toString()), e1); //$NON-NLS-1$ } if (StringUtils.isNotBlank(content_value)) { if (metadata.getType().equals(MavenFileExtensionEnum.HASH_MD5.getExtension())) { SrampModelUtils.setCustomProperty( baseArtifact, JavaModel.PROP_MAVEN_HASH_MD5, content_value); update = true; } else if (metadata.getType().equals(MavenFileExtensionEnum.HASH_SHA1.getExtension())) { SrampModelUtils.setCustomProperty( baseArtifact, JavaModel.PROP_MAVEN_HASH_SHA1, content_value); update = true; } if (update) { try { persistenceManager.updateArtifact(baseArtifact, artifactType); } catch (SrampException e) { throw new MavenRepositoryException( Messages.i18n.format("maven.resource.upload.sramp.error", metadata.toString()), e); //$NON-NLS-1$ } } } } else { try { persistenceManager.updateArtifactContent(baseArtifact.getUuid(), artifactType, content); } catch (SrampException e) { throw new MavenRepositoryException( Messages.i18n.format("maven.resource.upload.sramp.error", metadata.toString()), e); //$NON-NLS-1$ } } } } else { BaseArtifactType persisted = null; // If there is an existing artifact in s-ramp it would be updaded // with the new content if (baseArtifact != null) { if (metadata.isSnapshotVersion() || metadata.getFileName().equals("maven-metadata.xml")) { // $NON-NLS-1$ ArtifactType artifactType = ArtifactType.valueOf(baseArtifact); try { persistenceManager.updateArtifactContent(baseArtifact.getUuid(), artifactType, content); } catch (SrampException e) { throw new MavenRepositoryException( Messages.i18n.format( "maven.resource.upload.sramp.update.content.error", //$NON-NLS-1$ baseArtifact.getUuid()), e); } persisted = baseArtifact; } else { throw new MavenRepositoryException( Messages.i18n.format( "maven.resource.upload.sramp.release.artifact.exist", //$NON-NLS-1$ metadata.getFullName())); } } else { // we need to create a new artifact in s-ramp and persist the // content ArtifactType artifactType = determineArtifactType(fileName); BaseArtifactType baseArtifactType = artifactType.newArtifactInstance(); try { persisted = persistenceManager.persistArtifact(baseArtifactType, content); } catch (SrampException e1) { throw new MavenRepositoryException( Messages.i18n.format("maven.resource.upload.sramp.new.content.error"), e1); //$NON-NLS-1$ } } // Store the metadata to the persisted artifact SrampModelUtils.setCustomProperty( persisted, JavaModel.PROP_MAVEN_GROUP_ID, metadata.getGroupId()); SrampModelUtils.setCustomProperty( persisted, JavaModel.PROP_MAVEN_ARTIFACT_ID, metadata.getArtifactId()); SrampModelUtils.setCustomProperty( persisted, JavaModel.PROP_MAVEN_VERSION, metadata.getVersion()); if (StringUtils.isNotBlank(metadata.getClassifier())) { SrampModelUtils.setCustomProperty( persisted, JavaModel.PROP_MAVEN_CLASSIFIER, metadata.getClassifier()); } if (StringUtils.isNotBlank(metadata.getType())) { SrampModelUtils.setCustomProperty(persisted, JavaModel.PROP_MAVEN_TYPE, metadata.getType()); } if (StringUtils.isNotBlank(metadata.getSnapshotId())) { SrampModelUtils.setCustomProperty( persisted, JavaModel.PROP_MAVEN_SNAPSHOT_ID, metadata.getSnapshotId()); } try { // Persist the content size, because it will be required when // reading persisted .getOtherAttributes() .put(SrampConstants.SRAMP_CONTENT_SIZE_QNAME, content.available() + ""); // $NON-NLS-1$ } catch (IOException e) { logger.error(""); // $NON-NLS-1$ } persisted.setName(metadata.getFileName()); ArtifactType artifactType = ArtifactType.valueOf(persisted); try { persistenceManager.updateArtifact(persisted, artifactType); } catch (SrampException e) { throw new MavenRepositoryException( Messages.i18n.format( "maven.resource.upload.sramp.update.content.metadata.error", //$NON-NLS-1$ persisted.getUuid()), e); } uuid = persisted.getUuid(); } return uuid; }