git-svn-id: http://svn.research-infrastructures.eu/d4science/gcube/trunk/spatial-data/geonetwork@128530 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
3fb3a00f35
commit
b14e714d99
|
@ -13,6 +13,7 @@ import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.xml.bind.JAXBException;
|
import javax.xml.bind.JAXBException;
|
||||||
|
|
||||||
|
@ -20,20 +21,23 @@ import org.gcube.spatial.data.geonetwork.configuration.AuthorizationException;
|
||||||
import org.gcube.spatial.data.geonetwork.configuration.Configuration;
|
import org.gcube.spatial.data.geonetwork.configuration.Configuration;
|
||||||
import org.gcube.spatial.data.geonetwork.configuration.ConfigurationManager;
|
import org.gcube.spatial.data.geonetwork.configuration.ConfigurationManager;
|
||||||
import org.gcube.spatial.data.geonetwork.configuration.XMLAdapter;
|
import org.gcube.spatial.data.geonetwork.configuration.XMLAdapter;
|
||||||
|
import org.gcube.spatial.data.geonetwork.extension.GNClientExtension;
|
||||||
|
import org.gcube.spatial.data.geonetwork.model.Group;
|
||||||
|
import org.gcube.spatial.data.geonetwork.utils.GroupsUtils;
|
||||||
import org.geotoolkit.xml.XML;
|
import org.geotoolkit.xml.XML;
|
||||||
import org.jdom.output.Format;
|
import org.jdom.output.Format;
|
||||||
import org.jdom.output.XMLOutputter;
|
import org.jdom.output.XMLOutputter;
|
||||||
import org.opengis.metadata.Metadata;
|
import org.opengis.metadata.Metadata;
|
||||||
|
|
||||||
public class GeoNetwork implements GeoNetworkPublisher {
|
public class GeoNetwork implements GeoNetworkAdministration {
|
||||||
|
|
||||||
private static XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
|
private static XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
|
||||||
|
|
||||||
public static GeoNetworkPublisher get() throws Exception{
|
public static GeoNetworkAdministration get() throws Exception{
|
||||||
return new GeoNetwork(ConfigurationManager.get());
|
return new GeoNetwork(ConfigurationManager.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GeoNetworkPublisher get(Configuration config){
|
public static GeoNetworkAdministration get(Configuration config){
|
||||||
return new GeoNetwork(config);
|
return new GeoNetwork(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -170,8 +174,27 @@ public class GeoNetwork implements GeoNetworkPublisher {
|
||||||
GNClient client=getClient();
|
GNClient client=getClient();
|
||||||
client.deleteMetadata(id);
|
client.deleteMetadata(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//******************************* ADMIN ********************************* //
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Group> createGroup(String name, String description, String mail)
|
||||||
|
throws GNLibException, GNServerException {
|
||||||
|
GNClientExtension client=getClient();
|
||||||
|
return client.createGroup(name, description, mail);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Group> getGroups() throws GNLibException, GNServerException {
|
||||||
|
return getClient().getGroups();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* (non-Javadoc)
|
||||||
|
* @see org.gcube.spatial.data.geonetwork.GeoNetworkPublisher#registerXMLAdapter(org.gcube.spatial.data.geonetwork.configuration.XMLAdapter)
|
||||||
|
*/
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void registerXMLAdapter(XMLAdapter adapter) {
|
public void registerXMLAdapter(XMLAdapter adapter) {
|
||||||
|
@ -183,11 +206,11 @@ public class GeoNetwork implements GeoNetworkPublisher {
|
||||||
|
|
||||||
//************* PRIVATE
|
//************* PRIVATE
|
||||||
|
|
||||||
private GNClient theClient=null;
|
private GNClientExtension theClient=null;
|
||||||
|
|
||||||
private synchronized GNClient getClient(){
|
private synchronized GNClientExtension getClient(){
|
||||||
if(theClient==null)
|
if(theClient==null)
|
||||||
theClient = new GNClient(config.getGeoNetworkEndpoint());
|
theClient = new GNClientExtension(config.getGeoNetworkEndpoint());
|
||||||
return theClient;
|
return theClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
package org.gcube.spatial.data.geonetwork;
|
||||||
|
|
||||||
|
import it.geosolutions.geonetwork.exception.GNLibException;
|
||||||
|
import it.geosolutions.geonetwork.exception.GNServerException;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.gcube.spatial.data.geonetwork.model.Group;
|
||||||
|
|
||||||
|
public interface GeoNetworkAdministration extends GeoNetworkPublisher {
|
||||||
|
|
||||||
|
public Set<Group> createGroup(String name, String description, String mail) throws GNLibException, GNServerException;
|
||||||
|
public Set<Group> getGroups() throws GNLibException, GNServerException;
|
||||||
|
}
|
|
@ -10,4 +10,6 @@ public interface Configuration {
|
||||||
public Map<LoginLevel,String> getGeoNetworkUsers();
|
public Map<LoginLevel,String> getGeoNetworkUsers();
|
||||||
public Map<LoginLevel,String> getGeoNetworkPasswords();
|
public Map<LoginLevel,String> getGeoNetworkPasswords();
|
||||||
public int getScopeGroup();
|
public int getScopeGroup();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
package org.gcube.spatial.data.geonetwork.extension;
|
||||||
|
|
||||||
|
import it.geosolutions.geonetwork.GNClient;
|
||||||
|
import it.geosolutions.geonetwork.exception.GNLibException;
|
||||||
|
import it.geosolutions.geonetwork.exception.GNServerException;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.gcube.spatial.data.geonetwork.model.Group;
|
||||||
|
import org.gcube.spatial.data.geonetwork.utils.GroupsUtils;
|
||||||
|
|
||||||
|
public class GNClientExtension extends GNClient {
|
||||||
|
|
||||||
|
private String gnServiceURL;
|
||||||
|
|
||||||
|
public GNClientExtension(String serviceURL) {
|
||||||
|
super(serviceURL);
|
||||||
|
this.gnServiceURL=serviceURL;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Group> createGroup(String name, String description, String mail)throws GNLibException, GNServerException {
|
||||||
|
return GroupsUtils.parseGroupPage(GNMetadataAdminExtension.createGroup(getConnection(), gnServiceURL, name, description, mail));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Set<Group> getGroups() throws GNLibException, GNServerException{
|
||||||
|
return GroupsUtils.parseGroupXMLResponse(GNMetadataAdminExtension.getGroups(getConnection(), gnServiceURL));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,80 @@
|
||||||
|
package org.gcube.spatial.data.geonetwork.extension;
|
||||||
|
|
||||||
|
import it.geosolutions.geonetwork.exception.GNLibException;
|
||||||
|
import it.geosolutions.geonetwork.exception.GNServerException;
|
||||||
|
import it.geosolutions.geonetwork.op.GNMetadataAdmin;
|
||||||
|
import it.geosolutions.geonetwork.util.GNPrivConfiguration;
|
||||||
|
import it.geosolutions.geonetwork.util.HTTPUtils;
|
||||||
|
|
||||||
|
import org.apache.commons.httpclient.HttpStatus;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.jdom.Element;
|
||||||
|
import org.jdom.output.Format;
|
||||||
|
import org.jdom.output.XMLOutputter;
|
||||||
|
|
||||||
|
public class GNMetadataAdminExtension {
|
||||||
|
|
||||||
|
private final static Logger LOGGER = Logger.getLogger(GNMetadataAdminExtension.class);
|
||||||
|
|
||||||
|
private final static XMLOutputter outputter = new XMLOutputter(Format.getCompactFormat());
|
||||||
|
|
||||||
|
private final static String CREATE_GROUP_METHOD="/srv/en/group.update";
|
||||||
|
private final static String GROUP_LIST_METHOD="/srv/en/xml.group.list";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static String createGroup(HTTPUtils connection, String gnServiceURL, String groupName, String groupDescription, String groupMail) throws GNLibException, GNServerException {
|
||||||
|
if(LOGGER.isDebugEnabled())
|
||||||
|
LOGGER.debug(String.format("Creating group [Name : %s, Description : %s, Mail : %s ",groupName,groupDescription,groupMail));
|
||||||
|
Element adminRequest = buildCreateGroupRequest(groupName, groupDescription, groupMail);
|
||||||
|
return gnCall(connection, gnServiceURL, adminRequest,CREATE_GROUP_METHOD);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String getGroups(HTTPUtils connection,String gnServiceURL) throws GNServerException{
|
||||||
|
if(LOGGER.isDebugEnabled())
|
||||||
|
LOGGER.debug("Requesting groups..");
|
||||||
|
return gnCall(connection, gnServiceURL, new Element("request"),GROUP_LIST_METHOD);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static String gnCall(HTTPUtils connection,String baseURL, final Element gnRequest,String toInvokeMethod)throws GNServerException {
|
||||||
|
|
||||||
|
String serviceURL = baseURL + toInvokeMethod;
|
||||||
|
String result=gnPut(connection, serviceURL, gnRequest);
|
||||||
|
if(connection.getLastHttpStatus() != HttpStatus.SC_OK)
|
||||||
|
throw new GNServerException("Error setting metadata privileges in GeoNetwork");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @see {@link http://geonetwork-opensource.org/latest/developers/xml_services/metadata_xml_services.html#update-operations-allowed-for-a-metadata-metadata-admin }
|
||||||
|
*/
|
||||||
|
private static Element buildCreateGroupRequest(String groupName,String groupDescription,String groupMail) throws GNLibException {
|
||||||
|
if(LOGGER.isDebugEnabled())
|
||||||
|
LOGGER.debug("Compiling admin request document");
|
||||||
|
|
||||||
|
Element request = new Element("request");
|
||||||
|
request.addContent(new Element("name").setText(groupName));
|
||||||
|
request.addContent(new Element("description").setText(groupDescription));
|
||||||
|
request.addContent(new Element("email").setText(groupMail));
|
||||||
|
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static String gnPut(HTTPUtils connection, String serviceURL, final Element gnRequest) {
|
||||||
|
|
||||||
|
|
||||||
|
String s = outputter.outputString(gnRequest);
|
||||||
|
|
||||||
|
connection.setIgnoreResponseContentOnSuccess(false);
|
||||||
|
String res = connection.postXml(serviceURL, s);
|
||||||
|
// if(LOGGER.isInfoEnabled())
|
||||||
|
// LOGGER.info(serviceURL + " returned --> " + res);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package org.gcube.spatial.data.geonetwork.model;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@EqualsAndHashCode
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Account {
|
||||||
|
|
||||||
|
private String user;
|
||||||
|
private String password;
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
package org.gcube.spatial.data.geonetwork.model;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@ToString
|
||||||
|
public class Group {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private String description;
|
||||||
|
private String mail;
|
||||||
|
private Integer id;
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see java.lang.Object#hashCode()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + ((id == null) ? 0 : id.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see java.lang.Object#equals(java.lang.Object)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null)
|
||||||
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
Group other = (Group) obj;
|
||||||
|
if (id == null) {
|
||||||
|
if (other.id != null)
|
||||||
|
return false;
|
||||||
|
} else if (!id.equals(other.id))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package org.gcube.spatial.data.geonetwork.model;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import org.gcube.spatial.data.geonetwork.LoginLevel;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class ScopeConfiguration {
|
||||||
|
|
||||||
|
public static String NOT_ASSIGNED;
|
||||||
|
|
||||||
|
private String assignedScope;
|
||||||
|
private Integer publicGroup;
|
||||||
|
private Integer privateGroup;
|
||||||
|
private Map<LoginLevel,Account> accounts;
|
||||||
|
private Integer defaultGroup;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,117 @@
|
||||||
|
package org.gcube.spatial.data.geonetwork.utils;
|
||||||
|
|
||||||
|
import it.geosolutions.geonetwork.exception.GNLibException;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.StringReader;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.xml.xpath.XPath;
|
||||||
|
import javax.xml.xpath.XPathConstants;
|
||||||
|
import javax.xml.xpath.XPathExpressionException;
|
||||||
|
import javax.xml.xpath.XPathFactory;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import org.gcube.spatial.data.geonetwork.model.Group;
|
||||||
|
import org.jdom.JDOMException;
|
||||||
|
import org.jdom.input.SAXBuilder;
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
|
import org.w3c.dom.NodeList;
|
||||||
|
import org.xml.sax.InputSource;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class GroupsUtils {
|
||||||
|
|
||||||
|
private static XPath xpath = XPathFactory.newInstance().newXPath();
|
||||||
|
|
||||||
|
|
||||||
|
public static Set<Group> parseGroupXMLResponse(String xml) throws GNLibException {
|
||||||
|
try{
|
||||||
|
HashSet<Group> toReturn=new HashSet<>();
|
||||||
|
SAXBuilder builder = new SAXBuilder();
|
||||||
|
org.jdom.Element responseEl= builder.build(new StringReader(xml)).detachRootElement();
|
||||||
|
for(Object recordObj:responseEl.getChildren("record")){
|
||||||
|
org.jdom.Element record=(org.jdom.Element) recordObj;
|
||||||
|
Integer id=Integer.parseInt(record.getChild("id").getText());
|
||||||
|
String name=record.getChild("name").getText();
|
||||||
|
String description=record.getChild("description").getText();
|
||||||
|
String email=record.getChild("email").getText();
|
||||||
|
toReturn.add(new Group(name,description,email,id));
|
||||||
|
}
|
||||||
|
return toReturn;
|
||||||
|
}catch(Exception e){
|
||||||
|
throw new GNLibException("Unable to parse response", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static Set<Group> parseGroupPage(String page) throws GNLibException{
|
||||||
|
try{
|
||||||
|
page=page.substring(page.indexOf("<div id=\"content_container\""),page.lastIndexOf("</div>")+("</div>").length());
|
||||||
|
page=page.replaceAll(" ", "");
|
||||||
|
HashSet<Group> toReturn=new HashSet<>();
|
||||||
|
String expression = "//table/tr/td/table";
|
||||||
|
InputSource inputSource = new InputSource(new StringReader(page));
|
||||||
|
Node tableNode = (Node) xpath.evaluate(expression, inputSource, XPathConstants.NODE);
|
||||||
|
|
||||||
|
NodeList tableChildren=((Element)tableNode.getParentNode()).getElementsByTagName("tr");
|
||||||
|
for(int i=1;i<tableChildren.getLength();i++){ // starts from 1 to skip first row
|
||||||
|
Element tr=(Element)tableChildren.item(i);
|
||||||
|
NodeList tds=tr.getElementsByTagName("td");
|
||||||
|
String name=getElementContent((Element)tds.item(0));
|
||||||
|
String description=getElementContent((Element)tds.item(1));
|
||||||
|
String mail=getElementContent((Element)tds.item(2));
|
||||||
|
|
||||||
|
String idLink=((Element)tds.item(3)).getElementsByTagName("button").item(0).getAttributes().getNamedItem("onclick").getNodeValue();
|
||||||
|
|
||||||
|
Integer id=parseGroupId(idLink);
|
||||||
|
toReturn.add(new Group(name,description,mail,id));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return toReturn;
|
||||||
|
}catch(XPathExpressionException e){
|
||||||
|
throw new GNLibException("Unable to parse response", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// private static Element parse(String s) throws GNLibException {
|
||||||
|
// try {
|
||||||
|
// SAXBuilder builder = new SAXBuilder();
|
||||||
|
// return builder.build(new StringReader(s)).detachRootElement();
|
||||||
|
// } catch (Exception ex) {
|
||||||
|
// log.error("Error parsing GN response: " + s);
|
||||||
|
// throw new GNLibException("Error parsing GN response: " + ex.getMessage(), ex);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
private static Integer parseGroupId(String tdContent){
|
||||||
|
Integer indexOfIdParameter=tdContent.indexOf("id=");
|
||||||
|
return Integer.parseInt(tdContent.substring(indexOfIdParameter+3, tdContent.indexOf("'", indexOfIdParameter)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getElementContent(Element element){
|
||||||
|
|
||||||
|
|
||||||
|
NodeList children = element.getChildNodes();
|
||||||
|
String result = "";
|
||||||
|
for (int i = 0; i < children.getLength(); i++)
|
||||||
|
{
|
||||||
|
if (children.item(i).getNodeType() == Node.TEXT_NODE ||
|
||||||
|
children.item(i).getNodeType() == Node.CDATA_SECTION_NODE)
|
||||||
|
{
|
||||||
|
result += children.item(i).getNodeValue();
|
||||||
|
}
|
||||||
|
else if( children.item(i).getNodeType() == Node.COMMENT_NODE )
|
||||||
|
{
|
||||||
|
// Ignore comment nodes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.trim();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue