package org.gcube.spatial.data.sdi.engine.impl.gn.utils; import java.io.StringReader; import java.util.HashSet; import java.util.Set; import org.gcube.spatial.data.sdi.model.gn.User; import org.gcube.spatial.data.sdi.model.gn.User.Profile; import org.gcube.spatial.data.sdi.utils.StringUtils; import org.jdom.input.SAXBuilder; import org.json.JSONArray; import org.json.JSONObject; import it.geosolutions.geonetwork.exception.GNLibException; public class UserUtils { public static Set parseUserXMLResponse(String toParse) throws GNLibException{ try{ HashSet toReturn=new HashSet<>(); SAXBuilder builder = new SAXBuilder(); org.jdom.Element responseEl= builder.build(new StringReader(toParse)).detachRootElement(); for(Object recordObj:responseEl.getChildren("record")){ org.jdom.Element record=(org.jdom.Element) recordObj; Integer id=Integer.parseInt(record.getChildText("id")); String username=record.getChildText("username"); String password=record.getChildText("password"); Profile profile=Profile.valueOf(record.getChildText("profile")); toReturn.add(new User(id,username, password, profile)); } return toReturn; }catch(Exception e){ throw new GNLibException("Unable to parse users XML response", e); } } public static Set parseUserJSONResponse(String toParse)throws GNLibException{ try{ HashSet toReturn=new HashSet<>(); JSONArray array=new JSONArray(toParse); for(int i=0;i existing, Integer nameLenght, Integer passwordLength){ Set existingNames=new HashSet<>(); for(User g:existing)existingNames.add(g.getUsername()); return new User(0,StringUtils.generateNewRandom(existingNames, nameLenght),StringUtils.generateRandomString(passwordLength),Profile.RegisteredUser); } public static Set parseGroupsByUserResponse(String toParse) throws GNLibException{ try{ HashSet toReturn=new HashSet<>(); SAXBuilder builder = new SAXBuilder(); org.jdom.Element responseEl= builder.build(new StringReader(toParse)).detachRootElement(); for(Object recordObj:responseEl.getChildren("group")){ org.jdom.Element record=(org.jdom.Element) recordObj; Integer id=Integer.parseInt(record.getChildText("id")); toReturn.add(id); } return toReturn; }catch(Exception e){ throw new GNLibException("Unable to Groups By User XML response", e); } } public static User getByName(Set toLookInto,String toLookFor){ for(User g:toLookInto) if(g.getUsername().equals(toLookFor)) return g; return null; } }