// Make sure to remove the xml:base on the feed element for storage;
feedDoc.getDocumentElement().getAttributes().remove(Attribute.XML_BASE);
if (!targetClient.exists()) {
log.info("Creating target feed "+targetClient.getLocation());
Status status = targetClient.create(feedDoc);
if (!status.isSuccess()) {
log.severe("Cannot create target feed, status="+status.getCode());
errorCount++;
ok = false;
}
}
}
public void onEntry(Document entryDoc) {
if (!ok) {
return;
}
Entry entry = new Entry(entryDoc);
entry.index();
UUID entryId = null;
try {
entryId = UUID.fromString(entry.getId().substring(9));
} catch (IllegalArgumentException ex) {
log.severe("Ignoring entry with bad UUID: "+entry.getId());
errorCount++;
return;
}
log.info("Entry: "+entryId);
EntryClient entryClient = new EntryClient(new Reference(targetClient.getEntryLocation(entryId).toString()),target.getIdentity());
String src = null;
Element content = entryDoc.getDocumentElement().getFirstElementNamed(CONTENT_NAME);
URI baseURI = null;
MediaType contentType = null;
if (content!=null) {
src = content.getAttributeValue("src");
String type = content.getAttributeValue("type");
if (type!=null) {
contentType = MediaType.valueOf(type);
}
baseURI = content.getBaseURI();
}
if (entries!=null) {
entries.add(entryId);
}
Status exists = null;
try {
exists = entryClient.get();
} catch (Exception ex) {
log.log(Level.SEVERE,"Cannot get entry "+entryClient.getLocation()+", error: "+ex.getMessage(),ex);
errorCount++;
return;
}
if (exists.isSuccess()) {
entryClient.getEntry().index();
try {
Status status = entryClient.update(entryDoc);
if (!status.isSuccess()) {
log.severe("Cannot update entry "+entryClient.getLocation()+", status="+status.getCode());
errorCount++;
}
} catch (Exception ex) {
log.log(Level.SEVERE,"Cannot update entry "+entryClient.getLocation()+", error: "+ex.getMessage(),ex);
errorCount++;
}
if (src!=null) {
// update media
Reference mediaLocation = new Reference(baseURI.resolve(src).toString());
Client client = new Client(mediaLocation.getSchemeProtocol());
Request getRequest = new Request(Method.GET,mediaLocation);
if (source.getIdentity()!=null) {
getRequest.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,source.getUsername(),source.getPassword()));
}
Response getResponse = client.handle(getRequest);
if (getResponse.getStatus().isSuccess()) {
Reference targetLocation = new Reference(targetClient.getLocation().resolve(src).toString());
Client targetClient = new Client(targetLocation.getSchemeProtocol());
Request putRequest = new Request(Method.PUT,targetLocation);
if (target.getIdentity()!=null) {
putRequest.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,target.getUsername(),target.getPassword()));
}
Representation rep = getResponse.getEntity();
if (contentType!=null) {
rep.setMediaType(contentType);
}
putRequest.setEntity(rep);
Response putResponse = targetClient.handle(putRequest);
if (!putResponse.getStatus().isSuccess()) {
log.severe("Cannot put media update to "+targetLocation+", status="+putResponse.getStatus().getCode());
}
} else {
if (!getResponse.getStatus().isSuccess()) {
log.severe("Cannot get media from "+mediaLocation+", status="+getResponse.getStatus().getCode());
}
}
}
} else if (exists.getCode()==Status.CLIENT_ERROR_NOT_FOUND.getCode()) {
if (src==null) {
// regular entry
try {
targetClient.createEntry(entryDoc);
} catch (StatusException ex) {
log.severe("Cannot create entry "+entryClient.getLocation()+", status="+ex.getStatus().getCode());
errorCount++;
} catch (Exception ex) {
log.log(Level.SEVERE,"Cannot create entry "+entryClient.getLocation()+", error: "+ex.getMessage(),ex);
errorCount++;
}
} else {
// media entry
Reference mediaLocation = new Reference(baseURI.resolve(src).toString());
Client client = new Client(mediaLocation.getSchemeProtocol());
Request request = new Request(Method.GET,mediaLocation);
if (source.getIdentity()!=null) {
request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,source.getUsername(),source.getPassword()));
}
Response response = client.handle(request);
if (response.getStatus().isSuccess()) {
try {
Representation rep = response.getEntity();
if (contentType!=null) {
rep.setMediaType(contentType);
}
Entry mediaEntry = targetClient.createMedia(entryId, src, rep);
entryClient.setEntry(mediaEntry);
try {
Status status = entryClient.update(entryDoc);
if (!status.isSuccess()) {
log.severe("Cannot update entry "+entryClient.getLocation()+", status="+status.getCode());
errorCount++;
}
} catch (Exception ex) {
log.log(Level.SEVERE,"Cannot update entry "+entryClient.getLocation()+", error: "+ex.getMessage(),ex);
errorCount++;