Generalized request xml.metadata.get

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@152698 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2017-09-06 08:43:37 +00:00
parent d869aa8b50
commit fbeef4d7db
2 changed files with 93 additions and 36 deletions

View File

@ -445,6 +445,7 @@ public class GeonetworkResolver extends HttpServlet{
List<String> fileIdentifiers = GetResponseRecordFilter.getTextContentStringsForTagName(doc, "gmd:fileIdentifier");
List<String> noMatchingOwner = new ArrayList<String>();
for (String fileId : fileIdentifiers) {
//String own = GetResponseRecordFilter.getMetaCategoryByFileIdentifier(fileId, config.getGeoNetworkEndpoint(),config.getAdminAccount().getUser(), config.getAdminAccount().getPassword());
String own = GetResponseRecordFilter.getMetaOwnerNameByFileIdentifier(fileId, config.getGeoNetworkEndpoint(),config.getAdminAccount().getUser(), config.getAdminAccount().getPassword());
if(own.compareTo(theOwner)!=0){
logger.debug("Owner of file Identifier "+fileId+" not matching the owner passed: "+theOwner+", removing it..");

View File

@ -307,6 +307,66 @@ public class GetResponseRecordFilter {
}
/**
* Perform the request xml metadata get.
*
* @param fileIdentifier the file identifier is the UUID
* @param geonetworkURL the geonetwork url
* @param user the user
* @param pwd the pwd
* @return the response as string
*/
public static String requestXmlMetadataGet(String fileIdentifier, String geonetworkURL, String user, String pwd){
if(fileIdentifier==null || fileIdentifier.isEmpty())
return null;
HTTPCallsUtils httpCall = new HTTPCallsUtils();
String queryURL = geonetworkURL.endsWith("/")?geonetworkURL:geonetworkURL+"/";
try {
queryURL+=XML_METADATA_GET_SERVICE+"?uuid="+URLEncoder.encode(fileIdentifier, "UTF-8");
}
catch (UnsupportedEncodingException e1) {
queryURL+=XML_METADATA_GET_SERVICE+"?uuid="+fileIdentifier;
}
String theResponse = null;
try {
logger.trace("Performing query: "+queryURL);
//IT IS NEED ADMIN LOGIN IN ON GN TO PERFORM SUCH QUERY
boolean authorized = GNAuthentication.login(httpCall, geonetworkURL, user, pwd);
HttpResponse response = httpCall.get(queryURL);
if(response.getStatus()==HttpStatus.SC_OK){
theResponse = response.getResponse();
}
}
catch (MalformedURLException e) {
logger.debug("MalformedURLException error on getting the tag 'ownername' for the file identifier: "+fileIdentifier, e);
}
catch (Exception e) {
logger.debug("Error on getting the tag 'ownername' for the file identifier: "+fileIdentifier, e);
}
return theResponse;
}
/**
* Gets the meta category by file identifier.
*
* @param fileIdentifier the file identifier
* @param geonetworkURL the geonetwork url
* @param user the user
* @param pwd the pwd
* @return the meta category by file identifier
*/
public static String getMetaCategoryByFileIdentifier(String fileIdentifier, String geonetworkURL, String user, String pwd){
return getMetadataValueByFileIdentifier(fileIdentifier, geonetworkURL, user, pwd, "category");
}
/**
* Gets the meta owner name by file identifier.
*
@ -318,51 +378,47 @@ public class GetResponseRecordFilter {
*/
public static String getMetaOwnerNameByFileIdentifier(String fileIdentifier, String geonetworkURL, String user, String pwd){
if(fileIdentifier==null || fileIdentifier.isEmpty())
return getMetadataValueByFileIdentifier(fileIdentifier, geonetworkURL, user, pwd, "ownername");
}
/**
* Gets the metadata value by file identifier.
*
* @param fileIdentifier the file identifier
* @param geonetworkURL the geonetwork url
* @param user the user
* @param pwd the pwd
* @param metadataName the metadata name
* @return the metadata value by file identifier
*/
private static String getMetadataValueByFileIdentifier(String fileIdentifier, String geonetworkURL, String user, String pwd, String metadataName){
String response = requestXmlMetadataGet(fileIdentifier, geonetworkURL, user, pwd);
if(response==null || response.isEmpty())
return null;
String ownerName = null;
HTTPCallsUtils httpCall = new HTTPCallsUtils();
String queryURL = geonetworkURL.endsWith("/")?geonetworkURL:geonetworkURL+"/";
InputStream stream = new ByteArrayInputStream(response.getBytes());
Document doc;
try {
queryURL+=XML_METADATA_GET_SERVICE+"?uuid="+URLEncoder.encode(fileIdentifier, "UTF-8");
}
catch (UnsupportedEncodingException e1) {
queryURL+=XML_METADATA_GET_SERVICE+"?uuid="+fileIdentifier;
}
HttpResponse response;
try {
logger.trace("Performing query: "+queryURL);
//IT IS NEED ADMIN LOGIN IN ON GN TO PERFORM SUCH QUERY
boolean authorized = GNAuthentication.login(httpCall, geonetworkURL, user, pwd);
response = httpCall.get(queryURL);
if(response.getStatus()==HttpStatus.SC_OK){
String theR = response.getResponse();
//logger.trace("resp: "+theR);
InputStream stream = new ByteArrayInputStream(response.getResponse().getBytes());
Document doc = GetResponseRecordFilter.inputStreamToW3CDocument(stream);
List<String> ownerNames = GetResponseRecordFilter.getTextContentStringsForTagName(doc, "ownername");
if(ownerNames==null || ownerNames.isEmpty())
return null;
ownerName = ownerNames.get(0);
logger.debug("found and returning the ownername: '"+ownerName + "' for file identifier: "+fileIdentifier);
doc = GetResponseRecordFilter.inputStreamToW3CDocument(stream);
List<String> metadataValues = GetResponseRecordFilter.getTextContentStringsForTagName(doc, metadataName);
if(metadataValues==null || metadataValues.isEmpty()){
logger.debug("No "+metadataName+" found, returning null");
return null;
}
}
catch (MalformedURLException e) {
logger.debug("MalformedURLException error on getting the tag 'ownername' for the file identifier: "+fileIdentifier, e);
logger.debug("found and returning value of "+metadataName+": '"+metadataValues.get(0) + "' for file identifier: "+fileIdentifier);
return metadataValues.get(0);
}
catch (Exception e) {
logger.debug("Error on getting the tag 'ownername' for the file identifier: "+fileIdentifier, e);
logger.debug("Error: ",e);
return null;
}
return ownerName;
}
// /**