public Entry createMediaEntry(User user,Feed feed, Representation entity, String slug, UUID id)
throws AppException
{
// Create entry index
Entry entry = null;
try {
entry = feed.createEntry(id);
} catch (SQLException ex) {
throw new AppException(Status.SERVER_ERROR_INTERNAL,"Cannot create entry in database.",ex);
}
String file = slug;
try {
InputStream is = entity.getStream();
MediaType mediaType = entity.getMediaType();
if (mediaType==null) {
throw new AppException(Status.CLIENT_ERROR_BAD_REQUEST,"The media type is missing.");
}
MediaType baseMediaType = mediaType.valueOf(mediaType.getName());
// Make sure we have a filename for the media resource
if (file==null) {
String ext = metaService.getExtension(baseMediaType);
if (ext!=null) {
file = entry.getUUID()+"."+ext;
} else {
file = entry.getUUID().toString();
}
slug = entry.getUUID().toString();
} else if (file.indexOf('.')<0) {
String ext = metaService.getExtension(baseMediaType);
if (ext!=null) {
file += "."+ext;
}
}
EntryMedia media;
try {
media = entry.createResource(file,mediaType);
} catch (SQLException ex) {
try {
entry.delete(null);
} catch (SQLException ox) {
log.log(Level.SEVERE,"Cannot delete entry for exception cleanup.",ox);
}
throw new AppException(Status.SERVER_ERROR_INTERNAL,"Cannot create entry media resource in database.",ex);
}
if (media==null) {
throw new AppException(Status.CLIENT_ERROR_BAD_REQUEST,"Media entry name "+file+" refused.");
}
// Get author name for identity
String authorName = user.getName();
// Create entry document
Document doc = null;
try {
String title = URLDecoder.decode(slug,"UTF-8");
doc = AtomResource.createMediaEntryDocument(title,entry.getUUID(),entry.getCreated(),authorName,file,mediaType);
} catch (XMLException ex) {
try {
entry.delete(null);
} catch (SQLException ox) {
log.log(Level.SEVERE,"Cannot delete entry for exception cleanup.",ox);
}
throw new AppException(Status.SERVER_ERROR_INTERNAL,"Cannot create media entry document.",ex);
} catch (UnsupportedEncodingException ex) {
try {
entry.delete(null);
} catch (SQLException ox) {
log.log(Level.SEVERE,"Cannot delete entry for exception cleanup.",ox);
}
throw new AppException(Status.SERVER_ERROR_INTERNAL,"Cannot decode slug for media entry title.",ex);
}
try {
String path = feed.getPath();
Status entryStatus = storage.storeEntry(path,feed.getUUID(),entry.getUUID(),doc);
if (entryStatus.isSuccess()) {
Status mediaStatus = storage.storeMedia(path,feed.getUUID(),media.getName(),mediaType,is);
// TODO: storage may change media type parameters and the entry needs to be updated
if (!mediaStatus.isSuccess()) {
try {
entry.delete(null);
} catch (SQLException ox) {
log.log(Level.SEVERE,"Cannot delete entry for exception cleanup.",ox);
}
try {
storage.deleteEntry(path,feed.getUUID(),entry.getUUID());
} catch (IOException ex) {
log.log(Level.SEVERE,"Cannot delete entry storage for exception cleanup.",ex);
}
throw new AppException(mediaStatus,"Cannot store entry's media (refused)");
} else {
storage.feedUpdated(feed.getPath(),feed.getUUID(),feed.getEdited());
}
} else {
try {
entry.delete(null);
} catch (SQLException ox) {
log.log(Level.SEVERE,"Cannot delete entry for exception cleanup.",ox);
}
throw new AppException(entryStatus,"Cannot store entry (refused)");
}
} catch (SQLException ex) {
try {
entry.delete(null);
} catch (SQLException ox) {
log.log(Level.SEVERE,"Cannot delete entry for exception cleanup.",ox);
}
throw new AppException(Status.SERVER_ERROR_INTERNAL,"Cannot store entry.",ex);
}
} catch (IOException ex) {
try {
entry.delete(null);
} catch (SQLException ox) {
log.log(Level.SEVERE,"Cannot delete entry for exception cleanup.",ox);
}
throw new AppException(Status.SERVER_ERROR_INTERNAL,"Cannot store entry due to request I/O exception.",ex);
}