HttpServletRequest request = FacesContextUtility.getRequest();
UploadNewPackageUIBean uploadUIBean = FacesContextUtility.getManagedBean(UploadNewPackageUIBean.class);
String repoOption = request.getParameter("repoOption");
UploadItem fileItem = uploadUIBean.getFileItem();
boolean usingARepo = true;
// Validate
if (packageName == null || packageName.trim().equals("")) {
FacesContextUtility.addMessage(FacesMessage.SEVERITY_ERROR, "Package name must be specified");
return null;
}
if (repoOption == null) {
FacesContextUtility.addMessage(FacesMessage.SEVERITY_ERROR,
"A repository deployment option must be specified");
return null;
}
if (repoOption.equals(REPO_OPTION_NEW) && (newRepoName == null || newRepoName.trim().equals(""))) {
FacesContextUtility.addMessage(FacesMessage.SEVERITY_ERROR,
"When creating a new repo, the name of the repository to be created must be specified");
return null;
}
if ((fileItem == null) || fileItem.getFile() == null) {
FacesContextUtility.addMessage(FacesMessage.SEVERITY_ERROR, "A package file must be uploaded");
return null;
}
if (repoOption.equalsIgnoreCase(REPO_OPTION_NONE)) {
usingARepo = false;
}
// Determine which repo the package will go into
String repoId = null;
if (usingARepo) {
if (resource != null) {
try {
repoId = determineRepo(repoOption, subject, resource.getId());
} catch (ContentException ce) {
String errorMessages = ThrowableUtil.getAllMessages(ce);
FacesContextUtility.addMessage(FacesMessage.SEVERITY_ERROR,
"Failed to determine repository. Cause: " + errorMessages);
return "failure";
}
} else {
//we're creating a package directly inside a repo. The repo id
//will be in the request params.
repoId = FacesContextUtility.getRequiredRequestParameter("id");
}
}
try {
PackageVersion packageVersion = null;
try {
log.debug("Streaming new package bits from uploaded file: " + fileItem.getFile());
// Grab a stream for the file being uploaded
InputStream packageStream = new FileInputStream(fileItem.getFile());
try {
// Ask the bean to create the package
/* Currently, this is just used in the workflow for deploying a new package. This will probably get
refactored in the future for a general way of adding packages to the repo as its own operation. For
now, don't worry about that. The rest of this will be written assuming it's part of the deploy
workflow and we'll deal with the refactoring later.
jdobies, Feb 27, 2008
*/
try {
ContentManagerLocal contentManager = LookupUtil.getContentManager();
//store information about uploaded file for packageDetails as most of it is already available
Map<String, String> packageUploadDetails = new HashMap<String, String>();
packageUploadDetails.put(ContentManagerLocal.UPLOAD_FILE_SIZE,
String.valueOf(fileItem.getFileSize()));
packageUploadDetails.put(ContentManagerLocal.UPLOAD_FILE_INSTALL_DATE,
String.valueOf(System.currentTimeMillis()));
packageUploadDetails.put(ContentManagerLocal.UPLOAD_OWNER, subject.getName());
packageUploadDetails.put(ContentManagerLocal.UPLOAD_FILE_NAME, fileItem.getFileName());
String sha = null;
String md5 = null;
try {//Easier to implement here than in server side bean. Shouldn't affect performance too much.
md5 = new MessageDigestGenerator(MessageDigestGenerator.MD5).calcDigestString(fileItem
.getFile());
sha = new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(fileItem
.getFile());
} catch (IOException e1) {
log.warn("Error calculating file digest(s)", e1);
}