String parentPath = idx > 1 ? suggestedPath.substring(0, idx) : "/";
// Does the resource already exist? If so this is an update (PUT) not a create (POST)
boolean alreadyExists = resourceExists(suggestedPath);
AbderaClient abderaClient = new AbderaClient(abdera);
final Factory factory = abdera.getFactory();
boolean isCollection = resource instanceof Collection;
ExtensibleElement element;
if (isCollection) {
Feed feed = factory.newFeed();
feed.setId(baseURI + APPConstants.ATOM + encodeURL(suggestedPath));
// feed.setId(encodeURL(suggestedPath));
feed.setTitle(suggestedPath);
feed.setSubtitle(resource.getDescription());
feed.addAuthor(username);
feed.setUpdated(new Date());
element = feed;
} else {
Entry entry = factory.newEntry();
entry.setId(baseURI + APPConstants.ATOM + encodeURL(suggestedPath));
// entry.setId(encodeURL(suggestedPath));
entry.setTitle(suggestedPath);
entry.setSummary(resource.getDescription());
entry.addAuthor(username);
entry.setUpdated(new Date());
Object content = resource.getContent();
if (content != null && content instanceof byte[]) {
ByteArrayInputStream in = new ByteArrayInputStream((byte[]) content);
entry.setContent(in);
} else if (content instanceof InputStream) {
entry.setContent((InputStream) content);
} else {
entry.setContent((String) content);
}
element = entry;
}
java.util.Properties properties = resource.getProperties();
addPropertyExtensionElement(properties, factory, element,
PropertyExtensionFactory.PROPERTIES,
PropertyExtensionFactory.PROPERTY);
final String mediaType = resource.getMediaType();
if (mediaType != null && mediaType.length() > 0) {
element.addSimpleExtension(new QName(APPConstants.NAMESPACE, "mediaType"), mediaType);
}
element.addSimpleExtension(new QName(APPConstants.NAMESPACE, "parentPath"),
resource.getParentPath());
if (((ResourceImpl) resource).isContentModified()) {
element.addSimpleExtension(new QName(APPConstants.NAMESPACE, "contentModified"),
"true");
}
RequestOptions requestOptions = getAuthorization();
requestOptions.setSlug(relativePath);
ClientResponse resp;
if (!alreadyExists) {
resp = abderaClient.post(baseURI + APPConstants.ATOM + encodeURL(parentPath),
element, requestOptions);
} else {
resp = abderaClient.put(baseURI + APPConstants.ATOM + encodeURL(suggestedPath),
element, requestOptions);
}
if (resp.getType() == Response.ResponseType.SUCCESS) {
if (log.isDebugEnabled()) {
// log.debug(Messages.getMessage("resource.add", suggestedPath));
}
} else if (resp.getStatus() == 401) {
abderaClient.teardown();
String msg = "User is not authorized to add the resource to " + suggestedPath;
log.error(msg);
throw new RegistryException(msg);
} else {
String msg = "Add resource fail. Suggested Path: " + suggestedPath +
", Response Status: " + resp.getStatus() +
", Response Type: " + resp.getType();
abderaClient.teardown();
log.error(msg);
throw new RegistryException(msg);
}
// ResourceImpl impl = (ResourceImpl)resource;
// impl.setPath(resultPath);
// // todo - fix this to use util routine?
// int i = resultPath.lastIndexOf('/');
// impl.setParentPath(i == 0 ? "/" : resultPath.substring(0, i));
abderaClient.teardown();
return suggestedPath;
}