Project

General

Profile

« Previous | Next » 

Revision 50637

View differences:

modules/uoa-repository-manager-service/branches/deprecated-api/src/main/java/eu/dnetlib/repo/manager/service/utils/FrontEndLinkURIAuthenticationSuccessHandler.java
1
/*
2
package eu.dnetlib.repo.manager.service.utils;
3

  
4
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
5
import org.springframework.security.core.Authentication;
6
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
7

  
8
import javax.servlet.ServletException;
9
import javax.servlet.http.Cookie;
10
import javax.servlet.http.HttpServletRequest;
11
import javax.servlet.http.HttpServletResponse;
12
import java.io.IOException;
13

  
14
public class FrontEndLinkURIAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
15

  
16
    private String frontEndURI;
17

  
18
    @Override
19
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
20
        OIDCAuthenticationToken authOIDC = (OIDCAuthenticationToken) authentication;
21
        Cookie sessionCookie = new Cookie("currentUser", authOIDC.getSub());
22
        int expireSec = -1;
23
        sessionCookie.setMaxAge(expireSec);
24
        sessionCookie.setPath("/");
25
        response.addCookie(sessionCookie);
26
        response.sendRedirect(frontEndURI);
27
    }
28

  
29
    public String getFrontEndURI() {
30
        return frontEndURI;
31
    }
32

  
33
    public void setFrontEndURI(String frontEndURI) {
34
        this.frontEndURI = frontEndURI;
35
    }
36
}
37

  
38
*/
modules/uoa-repository-manager-service/branches/deprecated-api/src/main/java/eu/dnetlib/repo/manager/service/utils/Config.java
1
/*
2
package eu.dnetlib.repo.manager.service.utils;
3

  
4
import org.springframework.beans.factory.annotation.Value;
5
import org.springframework.context.annotation.Bean;
6
import org.springframework.context.annotation.ComponentScan;
7
import org.springframework.context.annotation.Configuration;
8
import org.springframework.context.annotation.PropertySource;
9
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
10
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
11
import org.springframework.session.web.http.CookieSerializer;
12
import org.springframework.session.web.http.DefaultCookieSerializer;
13

  
14
import javax.annotation.PostConstruct;
15
import java.util.logging.Logger;
16

  
17
@Configuration
18
@EnableRedisHttpSession
19
@PropertySource(value = { "classpath:eu/dnetlib/repo/manager/service/application.properties", "classpath:application.properties"} )
20
@ComponentScan(basePackages = "eu.dnetlib.repo.manager")
21
public class Config {
22

  
23
    private static Logger LOGGER = Logger.getLogger(String.valueOf(Config.class));
24

  
25
    @Value("${redis.host}")
26
    private String host;
27

  
28
    @Value("${redis.port:6379}")
29
    private String port;
30

  
31
    @Value("${redis.password:#{null}}")
32
    private String password;
33

  
34
    @PostConstruct
35
    private void init(){
36
        LOGGER.info(host);
37
    }
38

  
39
    @Bean
40
    public LettuceConnectionFactory connectionFactory() {
41
        LOGGER.info(String.format("Redis connection listens to %s:%s",host,port));
42
        LettuceConnectionFactory factory = new LettuceConnectionFactory(host,Integer.parseInt(port));
43
        if(password != null) factory.setPassword(password);
44
        return factory;
45
    }
46

  
47
    @Bean
48
    public CookieSerializer cookieSerializer() {
49
        DefaultCookieSerializer serializer = new DefaultCookieSerializer();
50
        serializer.setCookieName("SESSION"); // <1>
51
        serializer.setCookiePath("/"); // <2>
52
        return serializer;
53
    }
54

  
55
}
56
*/
modules/uoa-repository-manager-service/branches/deprecated-api/src/main/java/eu/dnetlib/repo/manager/service/utils/Converter.java
1
package eu.dnetlib.repo.manager.service.utils;
2

  
3
import com.fasterxml.jackson.databind.ObjectMapper;
4
import eu.dnetlib.domain.data.Repository;
5
import eu.dnetlib.domain.data.RepositoryInterface;
6
import eu.dnetlib.repo.manager.shared.*;
7
import org.apache.log4j.Logger;
8
import org.json.JSONArray;
9
import org.json.JSONException;
10
import org.json.JSONObject;
11

  
12
import java.io.*;
13
import java.text.ParseException;
14
import java.text.SimpleDateFormat;
15
import java.util.*;
16

  
17
public class Converter {
18

  
19
    private static final Logger LOGGER = Logger.getLogger(Converter.class);
20
    
21
    public static Repository jsonToRepositoryObject(JSONObject repositoryObject) throws JSONException {
22

  
23
        Repository repository = new Repository();
24

  
25

  
26
        LOGGER.debug("datasource response -> " + repositoryObject);
27
        JSONObject datasource = repositoryObject.getJSONObject("datasource");
28

  
29
        if( datasource.equals(null))
30
            return null;
31
        
32
        repository.setActivationId(datasource.get("activationId").toString());
33
        repository.setAggregator(datasource.get("aggregator").toString());
34
        repository.setCertificates(datasource.get("certificates").toString());
35
        repository.setCitationGuidelineUrl(datasource.get("citationguidelineurl").toString());
36
        repository.setCollectedFrom( datasource.get("collectedfrom").toString());
37

  
38
        repository.setContactEmail(datasource.get("contactemail").toString());
39
        if(repository.getContactEmail().equals("null"))
40
            repository.setContactEmail("");
41

  
42
        repository.setDatabaseAccessRestriction(datasource.get("databaseaccessrestriction").toString());
43
        repository.setDatabaseAccessType(datasource.get("databaseaccesstype").toString());
44
        repository.setDataUploadRestriction(datasource.get("datauploadrestriction").toString());
45
        repository.setDataUploadType(datasource.get("datauploadtype").toString());
46
        repository.setDateOfCollection(convertStringToDate( datasource.get("dateofcollection").toString()));
47
        repository.setDateOfValidation(convertStringToDate( datasource.get("dateofvalidation").toString()));
48

  
49
        repository.setDescription(datasource.get("description").toString());
50
        if(repository.getDescription().equals("null"))
51
            repository.setDescription("");
52

  
53
        repository.setEissn(datasource.get("eissn").toString());
54

  
55
        repository.setEnglishName( datasource.get("englishname").toString());
56
        if(repository.getEnglishName().equals("null"))
57
            repository.setEnglishName("");
58

  
59

  
60
        repository.setId(datasource.get("id").toString());
61
        repository.setIssn(datasource.get("issn").toString());
62
        repository.setOdLanguages(datasource.get("languages").toString());
63
        repository.setLatitude( toDouble(datasource.get("latitude").toString()));
64
        repository.setLissn(datasource.get("lissn").toString());
65

  
66
        repository.setLogoUrl(datasource.get("logourl").toString());
67
        if(repository.getLogoUrl().equals("null"))
68
            repository.setLogoUrl("");
69

  
70
        repository.setLongitude(toDouble(datasource.get("longitude").toString()));
71
        //datasource.get("managed");
72
        repository.setMissionStatementUrl(datasource.get("missionstatementurl").toString());
73
        repository.setNamespacePrefix(datasource.get("namespaceprefix").toString());
74
        repository.setOdContentTypes(datasource.get("od_contenttypes").toString());
75
        repository.setOfficialName(datasource.get("officialname").toString());
76
        if(repository.getOfficialName().equals("null"))
77
            repository.setOfficialName("");
78

  
79
        repository.setPidSystems(datasource.get("pidsystems").toString());
80
        //datasource.get("platform");
81
        repository.setProvenanceActionClass( datasource.get("provenanceaction").toString());
82
        repository.setQualityManagementKind(datasource.get("qualitymanagementkind").toString());
83
        repository.setRegisteredBy(datasource.get("registeredby").toString());
84

  
85
        if(Objects.equals(repository.getRegisteredBy(),"null"))
86
            repository.setRegistered(true);
87

  
88
        repository.setReleaseEndDate(convertStringToDate(datasource.get("releaseenddate").toString()));
89
        repository.setReleaseStartDate(convertStringToDate(datasource.get("releasestartdate").toString()));
90
        repository.setServiceProvider(Boolean.valueOf(datasource.get("serviceprovider").toString()));
91
        //datasource.get("subjects");
92
        Double timezone = toDouble(datasource.get("timezone").toString());
93
        repository.setTimezone(timezone!=null?timezone:0.0);
94
        repository.setTypology(datasource.get("platform").toString());
95
        repository.setVersioning(Boolean.valueOf(datasource.get("versioning").toString()));
96
        repository.setWebsiteUrl(datasource.get("websiteurl").toString());
97
        repository.setDatasourceClass(datasource.get("typology").toString());
98

  
99
        //TODO change organization to list
100
        repository.setOrganization( ((JSONArray)datasource.get("organizations")).getJSONObject(0).get("legalname").toString());
101
        String countryCode = ((JSONArray)datasource.get("organizations")).getJSONObject(0).get("country").toString();
102
        repository.setCountryCode(countryCode);
103

  
104

  
105
        String collectedFrom = datasource.get("collectedfrom").toString();
106
        //TODO check data consistency
107
        String type = "UNKNOWN";
108
        if (collectedFrom.equalsIgnoreCase("openaire____::opendoar")) {
109
            type = "opendoar";
110
        } else if (collectedFrom.equalsIgnoreCase("openaire____::re3data")) {
111
            type = "re3data";
112
        } else if (collectedFrom.equalsIgnoreCase("infrastruct_::openaire")) {
113
            type = "journal";
114
        }
115

  
116
        repository.setDatasourceType(type);
117

  
118

  
119
        return repository;
120
    }
121

  
122

  
123

  
124
    public static Date convertStringToDate(String date){
125

  
126
        if(Objects.equals(date, "null"))
127
            return null;
128

  
129
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
130
        try {
131
            return formatter.parse(date);
132
        } catch (ParseException e) {
133
            e.printStackTrace();
134
        }
135
        return null;
136
    }
137

  
138
    public static String convertDateToString(Date date){
139

  
140
        if(Objects.equals(date, null))
141
            return null;
142

  
143
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
144
        return formatter.format(date);
145
    }
146

  
147
    public static Double toDouble(String number){
148
        if(Objects.equals(number, "null"))
149
            return 0.0;
150
        else
151
            return Double.valueOf(number);
152
    }
153

  
154
    public static List<Repository> jsonToRepositoryList(JSONObject json) throws JSONException {
155

  
156
        List<Repository> resultSet = new ArrayList<>();
157
        JSONArray rs = json.getJSONArray("datasourceInfo");
158
        for(int i=0;i<rs.length();i++)
159
            resultSet.add(jsonToRepositoryObject( rs.getJSONObject(i)) );
160
        return resultSet;
161
    }
162

  
163
    public static List<RepositoryInterface> jsonToRepositoryInterfaceList(JSONArray rs) throws JSONException {
164

  
165
        List<RepositoryInterface> resultSet = new ArrayList<>();
166
        for(int i=0;i<rs.length();i++)
167
            resultSet.add(jsonToRepositoryInterfaceObject( rs.getJSONObject(i)) );
168
        return resultSet;
169
    }
170

  
171
    public static RepositoryInterface jsonToRepositoryInterfaceObject(JSONObject repositoryInterfaceObject) throws JSONException {
172

  
173
        RepositoryInterface repositoryInterface = new RepositoryInterface();
174

  
175
        repositoryInterface.setBaseUrl(repositoryInterfaceObject.get("baseurl").toString());
176
        repositoryInterface.setContentDescription(repositoryInterfaceObject.get("contentdescription").toString());
177
        repositoryInterface.setId(repositoryInterfaceObject.get("id").toString());
178
        repositoryInterface.setMetadataIdentifierPath(repositoryInterfaceObject.get("metadataIdentifierPath").toString());
179
        repositoryInterface.setAccessProtocol(repositoryInterfaceObject.get("protocol").toString());
180
        repositoryInterface.setTypology(repositoryInterfaceObject.get("typology").toString());
181
        repositoryInterface.setDesiredCompatibilityLevel(repositoryInterfaceObject.get("compatibility").toString());
182
        repositoryInterface.setActive(Boolean.parseBoolean(repositoryInterfaceObject.get("active").toString()));
183
        repositoryInterface.setRemovable(Boolean.parseBoolean(repositoryInterfaceObject.get("removable").toString()));
184
        repositoryInterface.setCompliance(repositoryInterfaceObject.get("compatibility").toString());
185

  
186
        Map<String, String> accessParams = new HashMap<>();
187
        Map<String, String> extraFields = new HashMap<>();
188

  
189
        ObjectMapper mapper = new ObjectMapper();
190
        JSONArray apiparams = repositoryInterfaceObject.getJSONArray("apiparam");
191

  
192
        for(int i=0;i<apiparams.length();i++)
193
            accessParams.put(apiparams.getJSONObject(i).getString("param"),apiparams.getJSONObject(i).getString("value"));
194

  
195
        return repositoryInterface;
196
    }
197

  
198
    public static String repositoryObjectToJson(Repository repository) throws JSONException {
199

  
200
        JSONObject jsonObject = new JSONObject();
201
        jsonObject.put("activationId",repository.getActivationId());
202
        jsonObject.put("aggregator",repository.getAggregator());
203
        jsonObject.put("certificates",repository.getCertificates());
204
        jsonObject.put("citationguidelineurl",repository.getCitationGuidelineUrl());
205
        jsonObject.put("collectedfrom",repository.getCollectedFrom());
206
        jsonObject.put("contactemail",repository.getContactEmail());
207
        jsonObject.put("databaseaccessrestriction",repository.getDatabaseAccessRestriction());
208
        jsonObject.put("databaseaccesstype",repository.getDatabaseAccessType());
209
        jsonObject.put("datauploadrestriction",repository.getDataUploadRestriction());
210
        jsonObject.put("datauploadtype",repository.getDataUploadType());
211
        jsonObject.put("dateofcollection",convertDateToString(repository.getDateOfCollection()));
212
        jsonObject.put("dateofvalidation",convertDateToString(repository.getDateOfValidation()));
213
        jsonObject.put("description",repository.getDescription());
214
        jsonObject.put("eissn",repository.getEissn());
215
        jsonObject.put("englishname",repository.getEnglishName());
216
        jsonObject.put("id",repository.getId());
217
        jsonObject.put("issn",repository.getIssn());
218
        jsonObject.put("languages",repository.getOdLanguages());
219
        jsonObject.put("latitude",repository.getLatitude().toString());
220
        jsonObject.put("lissn",repository.getLissn());
221
        jsonObject.put("logourl",repository.getLogoUrl());
222
        jsonObject.put("longitude",repository.getLongitude().toString());
223
        jsonObject.put("missionstatementurl",repository.getMissionStatementUrl());
224
        jsonObject.put("namespaceprefix",repository.getNamespacePrefix());
225
        jsonObject.put("od_contenttypes",repository.getOdContentTypes());
226
        jsonObject.put("officialname",repository.getOfficialName());
227
        jsonObject.put("pidsystems",repository.getPidSystems());
228
        jsonObject.put("provenanceaction",repository.getProvenanceActionClass());
229
        jsonObject.put("qualitymanagementkind",repository.getQualityManagementKind());
230
        jsonObject.put("registeredby",repository.getRegisteredBy());
231
        jsonObject.put("releaseenddate",convertDateToString(repository.getReleaseEndDate()));
232
        jsonObject.put("releasestartdate",convertDateToString(repository.getReleaseStartDate()));
233
        jsonObject.put("serviceprovider",repository.getServiceProvider());
234
        jsonObject.put("timezone",repository.getTimezone());
235
        jsonObject.put("typology",repository.getTypology());
236
        jsonObject.put("versioning",repository.getVersioning());
237
        jsonObject.put("websiteurl",repository.getWebsiteUrl());
238

  
239
        //datasource.get("managed");
240
        //datasource.get("platform");
241
        //datasource.get("subjects");
242
        return jsonObject.toString();
243
    }
244

  
245
    public static String repositoryInterfaceObjectToJson(Repository repository,RepositoryInterface repositoryInterface) throws JSONException {
246

  
247
        JSONObject jsonObject = new JSONObject();
248

  
249
        jsonObject.put("baseurl",repositoryInterface.getBaseUrl());
250
        jsonObject.put("contentdescription",repositoryInterface.getContentDescription());
251
        jsonObject.put("id",repositoryInterface.getId());
252
        jsonObject.put("metadataIdentifierPath",repositoryInterface.getMetadataIdentifierPath());
253
        jsonObject.put("protocol",repositoryInterface.getAccessProtocol());
254
        jsonObject.put("typology",repositoryInterface.getTypology());
255
        jsonObject.put("compatibility",repositoryInterface.getDesiredCompatibilityLevel());
256
        jsonObject.put("datasource",repository.getId());
257
        jsonObject.put("metadataIdentifierPath",repositoryInterface.getMetadataIdentifierPath());
258
        jsonObject.put("protocol",repositoryInterface.getAccessProtocol());
259
        jsonObject.put("removable",repositoryInterface.isRemovable());
260
        jsonObject.put("active",repositoryInterface.isActive());
261

  
262

  
263
        JSONArray apiparams = new JSONArray();
264
        for(String param: repositoryInterface.getAccessParams().keySet()){
265
            JSONObject jo = new JSONObject();
266
            jo.put("param",param);
267
            jo.put("value",repositoryInterface.getAccessParams().get(param));
268
            apiparams.put(jo);
269
        }
270
        jsonObject.put("apiparam",apiparams);
271

  
272

  
273
        jsonObject.put("lastCollectionDate",repositoryInterface.getLastCollectionDate());
274
        //jsonObject.put("lastCollectionMdid",repositoryInterface);
275
        //jsonObject.put("lastCollectionTotal");
276
        //jsonObject.put("lastDownloadDate");
277
//        jsonObject.put("lastDownloadMdid");
278
//        jsonObject.put("lastDownloadTotal");
279
//        jsonObject.put("lastValidationJob");
280
        //jsonObject.put("lastAggregationDate");
281
        //jsonObject.put("lastAggregationMdid");
282
        //jsonObject.put("lastAggregationTotal");
283

  
284
        return jsonObject.toString();
285
    }
286

  
287
    public static ArrayList<String> readFile(String filename) {
288
        String line;
289
        ArrayList<String> list = new ArrayList<String>();
290
        try {
291
            //InputStream in = Converter.class.getResourceAsStream("resources/eu/dnetlib/repo/manager/service/utils/"+filename);
292
            InputStream in = Converter.class.getClass().getResourceAsStream("/eu/**/" + filename);
293
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
294
            while((line = br.readLine()) != null) {
295
                list.add(line.trim());
296
            }
297
            br.close();
298
        } catch (IOException e) {
299
            LOGGER.debug("Error opening file!");
300
            e.printStackTrace();
301
        }
302
        return list;
303
    }
304

  
305
    public static List<AggregationDetails> getAggregationHistoryFromJson(JSONObject repositoryObject) throws JSONException {
306

  
307
       /* if( repositoryObject.get("aggregationHistory").toString().equals("[]") ||
308
                repositoryObject.get("aggregationHistory")!= null)
309
            return null;*/
310
       if(repositoryObject.get("aggregationHistory").toString().equals("[]"))
311
           return null;
312

  
313

  
314
        JSONArray rs = new JSONArray(repositoryObject.get("aggregationHistory").toString());
315

  
316
        LOGGER.debug(rs.length());
317

  
318
        List<AggregationDetails> aggregationDetailsList = new ArrayList<>();
319
        for(int i=0;i<rs.length();i++)
320
            aggregationDetailsList.add(jsonToAggregationDetails(rs.getJSONObject(i)));
321
        return aggregationDetailsList;
322
    }
323

  
324
    private static AggregationDetails jsonToAggregationDetails(JSONObject aggregationObject) throws JSONException {
325

  
326
        AggregationDetails aggregationDetails = new AggregationDetails();
327

  
328
        aggregationDetails.setAggregationStage(aggregationObject.get("aggregationStage").toString());
329
        if(aggregationObject.has("collectionMode"))
330
            aggregationDetails.setCollectionMode(aggregationObject.get("collectionMode").toString());
331
        aggregationDetails.setDate(convertStringToDate(aggregationObject.get("date").toString()));
332
        aggregationDetails.setNumberOfRecords(Integer.parseInt(aggregationObject.get("numberOfRecords").toString()));
333
        return aggregationDetails;
334
    }
335

  
336
    public static AggregationDetails getLastCollectionFromJson(JSONObject repositoryObject) throws JSONException {
337

  
338
        if( repositoryObject.get("lastCollection").equals(null))
339
            return null;
340

  
341
        return jsonToAggregationDetails(repositoryObject.getJSONObject("lastCollection"));
342
    }
343

  
344
    public static AggregationDetails getLastTransformationFromJson(JSONObject repositoryObject) throws JSONException {
345

  
346
        if( repositoryObject.get("lastTransformation").equals(null))
347
            return null;
348

  
349
        return jsonToAggregationDetails(repositoryObject.getJSONObject("lastTransformation"));
350
    }
351

  
352
    public static List<Timezone> toTimezones(List<String> timezones) {
353

  
354
        List<Timezone> tmz = new ArrayList<>();
355
        for(String t : timezones){
356
            String[] s = t.split("\t");
357
            tmz.add(new Timezone(s[1],Double.parseDouble(s[0])));
358
        }
359
        return tmz;
360
    }
361
}
modules/uoa-repository-manager-service/branches/deprecated-api/src/main/java/eu/dnetlib/repo/manager/service/utils/OaiTools.java
1
package eu.dnetlib.repo.manager.service.utils;
2

  
3
import org.apache.log4j.Logger;
4
import org.dom4j.io.DOMWriter;
5
import org.w3c.dom.Document;
6
import se.kb.oai.pmh.*;
7
import se.kb.oai.pmh.Set;
8

  
9
import javax.net.ssl.*;
10
import javax.xml.namespace.NamespaceContext;
11
import javax.xml.xpath.XPath;
12
import javax.xml.xpath.XPathExpressionException;
13
import javax.xml.xpath.XPathFactory;
14
import java.security.KeyManagementException;
15
import java.security.NoSuchAlgorithmException;
16
import java.security.cert.X509Certificate;
17
import java.util.*;
18

  
19
public class OaiTools {
20

  
21
	{
22
		disableSslVerification();
23
	}
24

  
25
	private static Logger LOGGER = Logger.getLogger(OaiTools.class);
26

  
27
	public static List<String> getSetsOfRepo(String baseUrl) throws Exception {
28
		try {
29
			LOGGER.debug("Getting sets of repository " + baseUrl);
30
			OaiPmhServer harvester = new OaiPmhServer(baseUrl);
31
			SetsList setList = harvester.listSets();
32
			ResumptionToken token = setList.getResumptionToken();
33
			List<Set> sets = new ArrayList<Set>();
34
			sets.addAll(setList.asList());
35
			while (token != null) {
36
				setList = harvester.listSets(token);
37
				token = setList.getResumptionToken();
38
				sets.addAll(setList.asList());
39
			}
40

  
41
			List<String> ret = new ArrayList<String>();
42
			for (Set set : sets) {
43
				ret.add(set.getSpec().trim());
44
			}
45
			if (ret.size() > 0 )
46
				Collections.sort(ret);
47
			return ret;
48

  
49
		} catch (Exception e) {
50
			LOGGER.error("Error getting sets of repository " + baseUrl, e);
51
			return new ArrayList<String>();
52
			//throw e;
53
		}
54
	}
55

  
56
	public static boolean identifyRepository(String baseUrl) throws Exception {
57
		LOGGER.debug("sending identify request to repo " + baseUrl);
58

  
59
		OaiPmhServer harvester = new OaiPmhServer(baseUrl);
60

  
61
		if (baseUrl.trim().isEmpty()) {
62
			return false;
63
		}
64

  
65
		try {
66
			Identification identification = harvester.identify();
67
			DOMWriter d4Writer = new DOMWriter();
68
			Document d = d4Writer.write(identification.getResponse());
69

  
70
			return verifyIdentify(d);
71
		} catch (Exception e) {
72
			LOGGER.debug("Error verifying identify response", e);
73
			throw e;
74
		}
75
	}
76

  
77
	private static boolean verifyIdentify(Document doc) throws XPathExpressionException {
78
		NamespaceContext ctx = new NamespaceContext() {
79
			public String getNamespaceURI(String prefix) {
80
				String uri;
81
				if (prefix.equals("oai"))
82
					uri = "http://www.openarchives.org/OAI/2.0/";
83
				else
84
					uri = null;
85
				return uri;
86
			}
87

  
88
			// Dummy implementation - not used!
89
			public Iterator<String> getPrefixes(String val) {
90
				return null;
91
			}
92

  
93
			// Dummy implemenation - not used!
94
			public String getPrefix(String uri) {
95
				return null;
96
			}
97
		};
98

  
99
		// Now the XPath expression
100

  
101
		String xpathStr = "//oai:OAI-PMH/oai:Identify";
102
		XPathFactory xpathFact = XPathFactory.newInstance();
103
		XPath xpath = xpathFact.newXPath();
104
		xpath.setNamespaceContext(ctx);
105
		String result = xpath.evaluate(xpathStr, doc);
106

  
107
		return (result != null && !result.equals(""));
108
	}
109

  
110
	private static void disableSslVerification() {
111
		try
112
		{
113
			LOGGER.debug("disabling ssl verification");
114
			// Create a trust manager that does not validate certificate chains
115
			TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
116
				public X509Certificate[] getAcceptedIssuers() {
117
					return null;
118
				}
119
				public void checkClientTrusted(X509Certificate[] certs, String authType) {
120
				}
121
				public void checkServerTrusted(X509Certificate[] certs, String authType) {
122
				}
123
			}
124
			};
125

  
126
			// Install the all-trusting trust manager
127
			SSLContext sc = SSLContext.getInstance("SSL");
128
			sc.init(null, trustAllCerts, new java.security.SecureRandom());
129
			HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
130

  
131
			// Create all-trusting host name verifier
132
			HostnameVerifier allHostsValid = new HostnameVerifier() {
133
				public boolean verify(String hostname, SSLSession session) {
134
					return true;
135
				}
136
			};
137

  
138
			// Install the all-trusting host verifier
139
			HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
140
		} catch (NoSuchAlgorithmException e) {
141
			LOGGER.error("disabling ssl verification", e);
142
		} catch (KeyManagementException e) {
143
			LOGGER.error("error while disabling ssl verification", e);
144
		}
145
	}
146
}
modules/uoa-repository-manager-service/branches/deprecated-api/src/main/java/eu/dnetlib/repo/manager/service/config/RepoManagerContextLoaderListener.java
1
package eu.dnetlib.repo.manager.service.config;
2

  
3
import org.apache.commons.lang.ArrayUtils;
4
import org.apache.log4j.Logger;
5
import org.springframework.beans.BeansException;
6
import org.springframework.context.support.ClassPathXmlApplicationContext;
7
import org.springframework.web.context.ContextLoaderListener;
8
import org.springframework.web.context.WebApplicationContext;
9
import org.springframework.web.context.support.XmlWebApplicationContext;
10

  
11
import javax.servlet.ServletContext;
12
import java.util.Properties;
13

  
14
public class RepoManagerContextLoaderListener extends ContextLoaderListener {
15
	private static Logger logger = Logger.getLogger(RepoManagerContextLoaderListener.class);
16

  
17

  
18
	public RepoManagerContextLoaderListener() {
19
		super();
20
	}
21

  
22
	public RepoManagerContextLoaderListener(WebApplicationContext context) {
23
		super(context);
24
	}
25

  
26
	@Override
27
	protected WebApplicationContext createWebApplicationContext(
28
			ServletContext servletContext)
29
			throws BeansException {
30
		logger.debug("Creating web application context");
31
		Properties props = this.loadProperties();
32
		String repoMode = props.getProperty("services.validator.mode.repo");
33
		String userMode = props.getProperty("services.validator.mode.user");
34
		Boolean standaloneMode = Boolean.parseBoolean(props.getProperty("services.validator.mode.standalone"));
35
		
36
		logger.info("User mode: " + userMode);
37
		logger.info("Repo mode: " + repoMode);
38
		logger.info("Standalone mode: " + standaloneMode);
39
//		logger.info("Dnet workflow enabled: " + repoMode);
40
		XmlWebApplicationContext ctx = new XmlWebApplicationContext();
41

  
42
		ctx.setServletContext(servletContext);
43

  
44
		String userApiContext = null;
45
		if (userMode.equalsIgnoreCase("local"))
46
			userApiContext = "eu/dnetlib/validator/web/api/impls/users/springContext-validator-user-local.xml";
47
		else if (userMode.equalsIgnoreCase("ldap"))
48
			userApiContext = "eu/dnetlib/users/springContext-users-ldap.xml";
49

  
50
		String[] springContextCore = new  String[] {
51
				"classpath:META-INF/cxf/cxf.xml",
52
				"classpath:META-INF/cxf/cxf-extension-soap.xml",
53
				"classpath:META-INF/cxf/cxf-extension-jaxws.xml",
54
				"classpath:META-INF/cxf/cxf-servlet.xml",
55
				"classpath*:/cxf.xml",
56
				"classpath*:/eu/dnetlib/repos/ehcacher/springContext-repos-ehcacher.xml",
57
				"classpath*:/eu/dnetlib/clients/ws/springContext-locatorFactory.xml",
58
				"classpath*:/eu/dnetlib/soap/cxf/applicationContext-eprbuilders.xml",
59
//				"classpath*:/eu/dnetlib/validator/web/actions/springContext-validator-struts.xml",
60
//				"classpath*:/eu/dnetlib/validator/web/actions/springContext-validator-emailer.xml",
61
//				"classpath*:/eu/dnetlib/validator/web/config/springContext-validator.xml",
62
				"classpath*:/eu/dnetlib/repo/manager/server/config/springContext-repo-manager-config.xml",
63
//				"classpath*:/eu/dnetlib/validator/commons/dao/springContext-*.xml",
64
				"classpath*:/eu/dnetlib/repos/springContext-repos-" + repoMode + ".xml",
65
				"classpath*:/" + userApiContext
66
		};
67

  
68

  
69
		String[] springContextForStandalone = new String[] {
70
		};
71

  
72
		String[] springContextForIS = new String[] {
73
				"classpath*:/gr/uoa/di/driver/util/springContext-locators.xml",
74
				"classpath*:/gr/uoa/di/driver/app/springContext-lookupFactory.xml",
75
				"classpath*:/gr/uoa/di/driver/app/springContext-lookupClients.xml",
76
				"classpath*:/eu/dnetlib/enabling/hcm/springContext-hcmService.xml",
77
				"classpath*:/gr/uoa/di/driver/app/springContext-commons.xml",
78
				"classpath*:/gr/uoa/di/driver/app/springContext-registrator.xml"
79
		};
80
		
81
		if (standaloneMode) {
82
			logger.debug("Loading contexts for standalone mode");
83
			ctx.setConfigLocations((String[])ArrayUtils.addAll(springContextCore,springContextForStandalone));
84
		} else {
85
			logger.debug("Loading contexts for dnet");
86
			ctx.setConfigLocations((String[])ArrayUtils.addAll(springContextCore,springContextForIS));
87
		}
88
		
89
		ctx.refresh();
90
		
91
		logger.debug("done");
92
		
93
		return ctx;
94
	}	
95
	
96
	private Properties loadProperties() {
97
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {
98
				"classpath*:/eu/dnetlib/repo/manager/server/config/springContext-repo-manager-config.xml"
99
		});
100
		
101
		CascadingPropertyLoader pLoader = (CascadingPropertyLoader) ctx.getBean("propertyLoader");
102
		Properties props = pLoader.getProperties();
103
		
104
		ctx.destroy();
105
		ctx.close();
106
		return props;
107
	}
108

  
109
}
modules/uoa-repository-manager-service/branches/deprecated-api/src/main/java/eu/dnetlib/repo/manager/service/config/SwaggerConfig.java
1
package eu.dnetlib.repo.manager.service.config;
2

  
3
import eu.dnetlib.repo.manager.service.controllers.MonitorApi;
4
import eu.dnetlib.repo.manager.service.controllers.PiWikApi;
5
import eu.dnetlib.repo.manager.service.controllers.RepositoryApi;
6
import eu.dnetlib.repo.manager.service.controllers.ValidatorApi;
7
import org.springframework.context.annotation.Bean;
8
import org.springframework.context.annotation.ComponentScan;
9
import org.springframework.context.annotation.Configuration;
10
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
11
import springfox.documentation.builders.PathSelectors;
12
import springfox.documentation.builders.RequestHandlerSelectors;
13
import springfox.documentation.service.ApiInfo;
14
import springfox.documentation.service.Contact;
15
import springfox.documentation.service.VendorExtension;
16
import springfox.documentation.spi.DocumentationType;
17
import springfox.documentation.spring.web.plugins.Docket;
18
import springfox.documentation.swagger2.annotations.EnableSwagger2;
19
import java.util.ArrayList;
20

  
21

  
22
/**
23
 * Created by panagiotis on 16/11/2017.
24
 */
25
@Configuration
26
@EnableSwagger2
27
@EnableWebMvc
28
@ComponentScan(basePackageClasses = {
29
        RepositoryApi.class,
30
        MonitorApi.class,
31
        ValidatorApi.class,
32
        PiWikApi.class
33
})
34
public class SwaggerConfig  {
35

  
36
    @Bean
37
    public Docket productApi() {
38
        return new Docket(DocumentationType.SWAGGER_2)
39
                .select()
40
                .apis(RequestHandlerSelectors.any())
41
                .paths(PathSelectors.any())
42
                .build()
43
                .pathMapping("/")
44
                .apiInfo(getApiInfo());
45
    }
46

  
47
    private ApiInfo getApiInfo() {
48
        return new ApiInfo("Repository Manager Dashboard API Documentation",
49
                "Repository Manager Dashboard API Documentation",
50
                "1.0",
51
                "urn:tos",
52
                new Contact("", "", ""),
53
                "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0",
54
                new ArrayList<VendorExtension>());
55
    }
56

  
57
}
modules/uoa-repository-manager-service/branches/deprecated-api/src/main/java/eu/dnetlib/repo/manager/service/config/CascadingPropertyLoader.java
1
package eu.dnetlib.repo.manager.service.config;
2

  
3
import org.springframework.beans.BeansException;
4
import org.springframework.beans.factory.InitializingBean;
5
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
6
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
7

  
8
import java.util.Properties;
9

  
10
/**
11
 * CascadingPropertyLoader loads a number of property files and mergers them together, so that the last properties
12
 * override the previous. It also supports property expansion like:
13
 * 
14
 * <code>
15
 *   something = 1
16
 *   somethingelse = 2
17
 *   test = ${something}/${somethingelse}
18
 *  </code>
19
 * 
20
 * <p>
21
 * And if you override something to XX, then test will become XX/2
22
 * </p>
23
 * 
24
 * 
25
 * @author marko
26
 * 
27
 */
28
public class CascadingPropertyLoader extends PropertyPlaceholderConfigurer implements InitializingBean {
29

  
30
	private Properties properties;
31

  
32
	public void afterPropertiesSet() throws Exception {
33
		this.properties = mergeProperties();
34

  
35
		// Convert the merged properties, if necessary.
36
		convertProperties(this.properties);
37
		
38
		logger.debug("Properties: " + properties);
39
	}
40

  
41
	@Override
42
	protected void processProperties(final ConfigurableListableBeanFactory beanFactoryToProcess, final Properties props) throws BeansException {
43
		super.processProperties(beanFactoryToProcess, props);
44
	}
45

  
46
	public Properties getProperties() {
47
		return properties;
48
	}
49

  
50
	public void setProperties(final Properties properties) {
51
		super.setProperties(properties);
52
		
53
		this.properties = properties;
54
	}
55
}
modules/uoa-repository-manager-service/branches/deprecated-api/src/main/java/eu/dnetlib/repo/manager/service/controllers/PiWikApiImpl.java
1
package eu.dnetlib.repo.manager.service.controllers;
2

  
3
import eu.dnetlib.domain.data.PiwikInfo;
4
import eu.dnetlib.utils.md5.MD5;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.beans.factory.annotation.Qualifier;
7
import org.springframework.dao.EmptyResultDataAccessException;
8
import org.springframework.jdbc.core.JdbcTemplate;
9
import org.springframework.jdbc.core.RowMapper;
10
import org.springframework.stereotype.Component;
11
import org.springframework.web.bind.annotation.PathVariable;
12

  
13
import javax.annotation.PostConstruct;
14
import javax.sql.DataSource;
15
import java.security.NoSuchAlgorithmException;
16
import java.sql.ResultSet;
17
import java.sql.SQLException;
18
import java.sql.Types;
19
import java.util.List;
20

  
21
@Component
22
public class PiWikApiImpl implements PiWikApi{
23

  
24
    @Autowired
25
    @Qualifier("repomanager.dataSource")
26
    private DataSource dataSource;
27

  
28
    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
29
            .getLogger(PiWikApiImpl.class);
30

  
31
    private final static String GET_PIWIK_SITE = "select repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, validated, validationdate, comment, repositoryname, country from piwik_site where repositoryid = ?;";
32

  
33
    private final static String INSERT_PIWIK_INFO = "insert into piwik_site (repositoryid, siteid, creationdate, requestorname, requestoremail, validated, repositoryname, country, authenticationtoken) values (?, ?, now(), ?, ?, ?, ?, ?, ?)";
34

  
35
    private final static String GET_PIWIK_SITES = "select repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, validated, validationdate, comment, repositoryname, country from piwik_site order by repositoryname";
36

  
37
    private final static String APPROVE_PIWIK_SITE = "update piwik_site set validated=true, validationdate=now() where repositoryid = ?;";
38

  
39

  
40
    private RowMapper<PiwikInfo> piwikRowMapper = (rs, i) -> new PiwikInfo(rs.getString("repositoryid"), getOpenaireId(rs.getString("repositoryid")), rs.getString("repositoryname"), rs.getString("country"),
41
            rs.getString("siteid"), rs.getString("authenticationtoken"), rs.getTimestamp("creationdate"), rs.getString("requestorname"), rs.getString("requestoremail"),
42
            rs.getBoolean("validated"), rs.getTimestamp("validationdate"), rs.getString("comment"));
43

  
44

  
45
    @Override
46
    public PiwikInfo getPiwikSiteForRepo(@PathVariable("repositoryId") String repositoryId) {
47
        try{
48
            return new JdbcTemplate(dataSource).queryForObject(GET_PIWIK_SITE, new String[]{repositoryId}, new int[]{Types.VARCHAR}, piwikRowMapper);
49
        }catch (EmptyResultDataAccessException e){
50
            return null;
51
        }
52
    }
53

  
54
    @Override
55
    public PiwikInfo savePiwikInfo(PiwikInfo piwikInfo) {
56
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
57
        jdbcTemplate.update(INSERT_PIWIK_INFO, new Object[]{piwikInfo.getRepositoryId(), piwikInfo.getSiteId(), piwikInfo.getRequestorName(),
58
                        piwikInfo.getRequestorEmail(), piwikInfo.isValidated(), piwikInfo.getRepositoryName(), piwikInfo.getCountry(), piwikInfo.getAuthenticationToken()},
59
                new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.BOOLEAN, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR});
60
        return piwikInfo;
61
    }
62

  
63
    @Override
64
    public List<PiwikInfo> getPiwikSitesForRepos() {
65
        LOGGER.debug("Getting piwik sites for repos! ");
66
        try{
67
            return new JdbcTemplate(dataSource).query(GET_PIWIK_SITES, piwikRowMapper);
68
        }catch (EmptyResultDataAccessException e){
69
            return null;
70
        }
71

  
72
    }
73

  
74
    @Override
75
    public void approvePiwikSite(@PathVariable("repositoryId") String repositoryId) {
76
        new JdbcTemplate(dataSource).update(APPROVE_PIWIK_SITE, new Object[] {repositoryId}, new int[] {Types.VARCHAR});
77
    }
78

  
79
    @Override
80
    public String getOpenaireId(@PathVariable("repositoryId") String repositoryid) {
81
        try {
82
            if (repositoryid != null && repositoryid.contains("::"))
83
                return repositoryid.split("::")[0] + "::" + MD5.encrypt2Hex(repositoryid.split("::")[1]);
84
            else
85
                return null;
86
        } catch (NoSuchAlgorithmException e) {
87
            e.printStackTrace();
88
        }
89
        return null;
90
    }
91

  
92

  
93
}
modules/uoa-repository-manager-service/branches/deprecated-api/src/main/java/eu/dnetlib/repo/manager/service/controllers/ValidatorApi.java
1
package eu.dnetlib.repo.manager.service.controllers;
2

  
3
import eu.dnetlib.api.functionality.ValidatorServiceException;
4
import eu.dnetlib.domain.functionality.validator.JobForValidation;
5
import eu.dnetlib.domain.functionality.validator.RuleSet;
6
import eu.dnetlib.domain.functionality.validator.StoredJob;
7
import eu.dnetlib.repo.manager.shared.InterfaceInformation;
8
import eu.dnetlib.repo.manager.shared.ValidationServiceException;
9
import io.swagger.annotations.Api;
10
import org.json.JSONException;
11
import org.springframework.http.MediaType;
12
import org.springframework.web.bind.annotation.*;
13

  
14
import java.util.List;
15

  
16

  
17
@RestController
18
@RequestMapping(value = "/validator")
19
@Api(description = "Validator API",  tags = {"validator"})
20
public interface ValidatorApi {
21

  
22
    @RequestMapping(value = "/submitJobForValidation",method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE )
23
    @ResponseBody
24
    void submitJobForValidation(@RequestBody JobForValidation jobForValidation);
25

  
26
    @RequestMapping(value = "/reSubmitJobForValidation/",method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE )
27
    @ResponseBody
28
    void reSubmitJobForValidation(@RequestBody String jobId) throws JSONException;
29

  
30
    @RequestMapping(value = "/getRuleSets/{mode}" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
31
    @ResponseBody
32
    List<RuleSet> getRuleSets(String mode);
33

  
34
    @RequestMapping(value = "/getSetsOfRepository" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
35
    @ResponseBody
36
    List<String> getSetsOfRepository(@RequestBody String url);
37

  
38
    @RequestMapping(value = "/identifyRepository/{url}" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
39
    @ResponseBody
40
    boolean identifyRepo(String url);
41

  
42
    @RequestMapping(value = "/getRuleSet/{acronym}" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
43
    @ResponseBody
44
    RuleSet getRuleSet(String acronym);
45

  
46
    @RequestMapping(value = "/getStoredJobsNew" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
47
    @ResponseBody
48
    List<StoredJob> getStoredJobsNew(String user, String jobType, Integer offset, Integer limit, String dateFrom,
49
                                     String dateTo, String validationStatus) throws ValidatorServiceException;
50

  
51

  
52
    @RequestMapping(value = "/getStoredJobsTotalNumberNew" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
53
    @ResponseBody
54
    int getStoredJobsTotalNumberNew(String user, String jobType, String validationStatus) throws ValidatorServiceException;
55

  
56
    @RequestMapping(value = "/getInterfaceInformation/{baseUrl}" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
57
    @ResponseBody
58
    InterfaceInformation getInterfaceInformation(String baseUrl) throws ValidationServiceException;
59
}
modules/uoa-repository-manager-service/branches/deprecated-api/src/main/java/eu/dnetlib/repo/manager/service/controllers/BrokerApiImpl.java
1
package eu.dnetlib.repo.manager.service.controllers;
2

  
3
import com.fasterxml.jackson.databind.JsonNode;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
import eu.dnetlib.domain.data.Repository;
6
import eu.dnetlib.repo.manager.shared.BrokerException;
7
import eu.dnetlib.repo.manager.shared.Term;
8
import eu.dnetlib.repo.manager.shared.Tuple;
9
import eu.dnetlib.repo.manager.shared.broker.*;
10
import org.json.JSONException;
11
import org.json.JSONObject;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.beans.factory.annotation.Value;
14
import org.springframework.core.ParameterizedTypeReference;
15
import org.springframework.http.HttpEntity;
16
import org.springframework.http.HttpHeaders;
17
import org.springframework.http.HttpMethod;
18
import org.springframework.http.ResponseEntity;
19
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
20
import org.springframework.stereotype.Component;
21
import org.springframework.util.LinkedMultiValueMap;
22
import org.springframework.util.MultiValueMap;
23
import org.springframework.web.bind.annotation.PathVariable;
24
import org.springframework.web.client.RestClientException;
25
import org.springframework.web.client.RestTemplate;
26
import org.springframework.web.util.UriComponents;
27
import org.springframework.web.util.UriComponentsBuilder;
28

  
29
import javax.annotation.PostConstruct;
30
import java.io.IOException;
31
import java.io.InputStream;
32
import java.net.URL;
33
import java.util.*;
34

  
35
@Component
36
public class BrokerApiImpl implements BrokerApi {
37

  
38
    @Autowired
39
    private RepositoryApiImpl repoAPI;
40
    @Value("${services.broker.url}:${services.broker.port}/${services.broker.api}${services.broker.openaire}")
41
    private String openairePath;
42
    @Value("${services.broker.url}:${services.broker.port}/${services.broker.api}")
43
    private String apiPath;
44
    @Value("${topic_types.url}")
45
    private String topicsURL;
46

  
47
    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
48
            .getLogger(BrokerApiImpl.class);
49

  
50
    private RestTemplate restTemplate = null;
51

  
52
    private HttpHeaders httpHeaders;
53

  
54
    private HashMap<String,Term> topics = new HashMap<String, Term>();
55

  
56
    @PostConstruct
57
    private void initDnetTopicsMap() {
58

  
59
        restTemplate = new RestTemplate();
60
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
61

  
62
        httpHeaders = new HttpHeaders();
63
        httpHeaders.set("Content-Type", "application/json");
64

  
65
        LOGGER.debug("Init dnet topics!");
66
        InputStream is = null;
67
        try {
68
            is = new URL(topicsURL).openStream();
69
            ObjectMapper mapper = new ObjectMapper();
70
            JsonNode root = mapper.readTree(is);
71
            for (JsonNode term : root.path("terms") )
72
                topics.put(term.path("code").textValue(), parseTerm(term));
73
        } catch (IOException e) {
74
            LOGGER.debug(e);
75
            e.printStackTrace();
76
        }
77
    }
78

  
79
    private Term parseTerm(JsonNode term) {
80
        return new Term(term.path("englishName").textValue(),term.path("nativeName").textValue(),
81
                term.path("encoding").textValue(),term.path("code").textValue());
82
    }
83

  
84

  
85
    @Override
86
    public DatasourcesBroker getDatasourcesOfUser(String params) throws JSONException {
87
        JSONObject json_params = new JSONObject(params);
88
        DatasourcesBroker ret = new DatasourcesBroker();
89

  
90
        String userEmail = json_params.getString("userEmail");
91
        boolean includeShared = Boolean.parseBoolean( json_params.getString("includeShared") );
92
        boolean includeByOthers = Boolean.parseBoolean( json_params.getString("includeByOthers") );
93

  
94
        try {
95
            ret.setDatasourcesOfUser(getDatasourcesOfUserType(getRepositoriesOfUser(userEmail)));
96
            if (includeShared) {
97
                //TODO whatever nikonas was saying
98
                List<String> sharedDatasourceIds = new ArrayList<String>();
99
                ret.setSharedDatasources(getDatasourcesOfUserType(getRepositoriesByIds(sharedDatasourceIds)));
100
            }
101

  
102
            if (includeByOthers) {
103
                ret.setDatasourcesOfOthers(getDatasourcesOfUserType(getRepositoriesOfUser(userEmail)));
104
            }
105
        } catch (BrokerException e) {
106
            e.printStackTrace();
107
        }
108

  
109
        return ret;
110
    }
111

  
112
    @Override
113
    public List<BrowseEntry> getTopicsForDatasource(@PathVariable("datasourceName")  String datasourceName) throws BrokerException {
114
        final String service = "/topicsForDatasource";
115

  
116
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
117
                .queryParam("ds", datasourceName);
118

  
119
        ResponseEntity<List<BrowseEntry>> resp;
120
        try {
121
            resp = restTemplate.exchange(
122
                    builder.build().encode().toUri(),
123
                    HttpMethod.GET,
124
                    null,
125
                    new ParameterizedTypeReference<List<BrowseEntry>>() {
126
                    });
127
        } catch (RestClientException e) {
128
            throw new BrokerException(e);
129
        }
130

  
131
        return resp.getBody();
132
    }
133

  
134
    @Override
135
    public EventsPage advancedShowEvents(String params) throws BrokerException, JSONException ,IOException {
136
        JSONObject json_params = new JSONObject(params);
137

  
138
        String page = json_params.getString("page");
139
        String pagesize = json_params.getString("pagesize");
140
        String json_advQueryObject = json_params.getString("advQueryObject");
141

  
142
        ObjectMapper mapper = new ObjectMapper();
143
        AdvQueryObject advQueryObject = mapper.readValue(json_advQueryObject, AdvQueryObject.class);
144

  
145
        final String service = "/events/{page}/{pageSize}";
146

  
147
        Map<String, Long> uriParams = new HashMap<>();
148
        uriParams.put("page", Long.parseLong(page));
149
        uriParams.put("pageSize", Long.parseLong(pagesize));
150

  
151
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service);
152

  
153
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
154
        headers.add("Content-Type", "application/json");
155

  
156
        advQueryObject.setPage(Long.parseLong(page));
157

  
158
        HttpEntity<AdvQueryObject> entity = new HttpEntity<>(advQueryObject, headers);
159

  
160
        ResponseEntity<EventsPage> resp;
161
        try {
162
            resp = restTemplate.exchange(
163
                    builder.buildAndExpand(uriParams).encode().toUri(),
164
                    HttpMethod.POST,
165
                    entity,
166
                    new ParameterizedTypeReference<EventsPage>() {
167
                    }
168
            );
169
        } catch (RestClientException e) {
170
            throw new BrokerException(e);
171
        }
172
        return resp.getBody();
173

  
174

  
175
    }
176

  
177

  
178
    private List<Tuple<BrowseEntry, String>> getDatasourcesOfUserType(List<Repository> repositories) throws BrokerException {
179

  
180
        List<Tuple<BrowseEntry, String>> entries = new ArrayList<>();
181
        for (Repository repo : repositories) {
182
            BrowseEntry temp = new BrowseEntry();
183
            temp.setValue(repo.getOfficialName());
184
            temp.setSize(new Long(0));
185
            for (BrowseEntry e : getTopicsForDatasource(repo.getOfficialName())) {
186
                temp.setSize(temp.getSize() + e.getSize());
187
            }
188
            Tuple<BrowseEntry, String> tup = new Tuple<>(temp, repo.getLogoUrl());
189
            entries.add(tup);
190
        }
191

  
192
        // sort the collection by the second field of the tuple which is size
193
        Collections.sort(entries, new Comparator<Tuple<BrowseEntry, String>>() {
194
            @Override
195
            public int compare(Tuple<BrowseEntry, String> e1, Tuple<BrowseEntry, String> e2) {
196
                return (int) (e2.getFirst().getSize().longValue() - e1.getFirst().getSize().longValue());
197
            }
198
        });
199

  
200
        return entries;
201
    }
202

  
203

  
204

  
205
    private List<Repository> getRepositoriesOfUser(String userEmail) throws JSONException {
206

  
207
        int page = 0;
208
        int size = 50;
209
        List<Repository> rs ;
210
        List<Repository> resultSet = new ArrayList<>();
211

  
212
        while (true){
213
            rs = repoAPI.getRepositoriesOfUser(userEmail, String.valueOf(page), String.valueOf(size));
214
            resultSet.addAll(rs);
215
            page+=1;
216
            if(rs.size() == 0) break;
217
        }
218
        return resultSet;
219
    }
220

  
221
    private List<Repository> getRepositoriesByIds(List<String> sharedDatasourceIds) {
222
        return null;
223
    }
224

  
225
    @Override
226
    public EventsPage showEvents(String params) throws BrokerException, JSONException {
227

  
228
        JSONObject json_params = new JSONObject(params);
229

  
230
        String datasourceName = json_params.getString("datasourceName");
231
        String topic = json_params.getString("topic");
232
        String page = json_params.getString("page");
233

  
234
        final String service = "/showEvents";
235

  
236
        //build the uri params
237
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
238
                .queryParam("ds", datasourceName)
239
                .queryParam("topic", topic)
240
                .queryParam("page", page);
241

  
242
        ResponseEntity<EventsPage> resp;
243
        try {
244
            //communicate with endpoint
245
            resp = restTemplate.exchange(
246
                    builder.build().encode().toUri(),
247
                    HttpMethod.GET,
248
                    null,
249
                    new ParameterizedTypeReference<EventsPage>() {
250
                    });
251
        } catch (RestClientException e) {
252
            throw new BrokerException(e);
253
        }
254
        return resp.getBody();
255
    }
256

  
257
    @Override
258
    public Map<String, List<SimpleSubscriptionDesc>> getSimpleSubscriptionsOfUser(@PathVariable("userEmail") String userEmail) throws BrokerException {
259

  
260
        final String service = "/subscriptions";
261

  
262
        //build the uri params
263
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service)
264
                .queryParam("email", userEmail);
265

  
266
        //create new template engine
267
        ResponseEntity<Map<String, List<SimpleSubscriptionDesc>>> resp;
268
        try {
269
            //communicate with endpoint
270
            resp = restTemplate.exchange(
271
                    builder.build().encode().toUri(),
272
                    HttpMethod.GET,
273
                    null,
274
                    new ParameterizedTypeReference<Map<String, List<SimpleSubscriptionDesc>>>() {
275
                    });
276
        } catch (RestClientException e) {
277
            LOGGER.debug("Error " , e);
278
            throw new BrokerException(e);
279
        }
280
        return resp.getBody();
281
    }
282

  
283
    @Override
284
    public Subscription subscribe(OpenaireSubscription obj) throws BrokerException {
285
        final String service = "/subscribe";
286

  
287
        //build the uri params
288
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service);
289

  
290
        HttpEntity<OpenaireSubscription> entity = new HttpEntity<>(obj, httpHeaders);
291

  
292
        //create new template engine
293
        RestTemplate template = new RestTemplate();
294
        template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
295
        ResponseEntity<Subscription> resp;
296
        try {
297
            //communicate with endpoint
298
            resp = restTemplate.exchange(
299
                    builder.build().encode().toUri(),
300
                    HttpMethod.POST,
301
                    entity,
302
                    new ParameterizedTypeReference<Subscription>() {
303
                    });
304
        } catch (RestClientException e) {
305
            throw new BrokerException(e);
306
        }
307

  
308
        return resp.getBody();
309
    }
310

  
311
    @Override
312
    public void unsubscribe(@PathVariable("subscriptionId") String subscriptionId) throws BrokerException {
313
        final String service = "/subscriptions/" + subscriptionId;
314

  
315
        //build the uri params
316
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
317

  
318
        try {
319
            //communicate with endpoint
320
            restTemplate.exchange(
321
                    builder.build().encode().toUri(),
322
                    HttpMethod.DELETE,
323
                    null,
324
                    new ParameterizedTypeReference<Void>() {
325
                    });
326
        } catch (RestClientException e) {
327
            throw new BrokerException(e);
328
        }
329
    }
330

  
331
    @Override
332
    public Subscription getSubscription(@PathVariable("subscriptionId") String subscriptionId) throws BrokerException {
333
        final String service = "/subscriptions/" + subscriptionId;
334

  
335
        //build the uri params
336
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiPath + service);
337

  
338
        ResponseEntity<Subscription> resp;
339
        try {
340
            //communicate with endpoint
341
            resp = restTemplate.exchange(
342
                    builder.build().encode().toUri(),
343
                    HttpMethod.GET,
344
                    null,
345
                    new ParameterizedTypeReference<Subscription>() {
346
                    });
347
        } catch (RestClientException e) {
348
            throw new BrokerException(e);
349
        }
350
        return resp.getBody();
351
    }
352

  
353
    @Override
354
    public Map<String, Term> getDnetTopics() throws BrokerException {
355
        return topics;
356
    }
357

  
358
    @Override
359
    public EventsPage getNotificationsBySubscriptionId(@PathVariable("subscriptionId") String subscriptionId,
360
                                                       @PathVariable("page") String page,
361
                                                       @PathVariable("size") String size
362
                                                       ) throws BrokerException {
363

  
364
        UriComponents uriComponents = UriComponentsBuilder
365
                .fromHttpUrl(openairePath + "/notifications/")
366
                .path("/{id}/{page}/{size}/")
367
                .build().expand(subscriptionId,page, size).encode();
368

  
369
        ResponseEntity<EventsPage> resp;
370
        try {
371
            resp = restTemplate.exchange(
372
                    uriComponents.toUri(),
373
                    HttpMethod.GET,
374
                    null,
375
                    new ParameterizedTypeReference<EventsPage>() {
376
                    });
377
        } catch (RestClientException e) {
378
            throw new BrokerException(e);
379
        }
380
        return resp.getBody();
381
    }
382

  
383
    @Override
384
    public Map<String, List<Subscription>> getSubscriptionsOfUser(@PathVariable("userEmail") String userEmail)
385
            throws BrokerException {
386

  
387
        Map<String, List<SimpleSubscriptionDesc>> simpleSubs = getSimpleSubscriptionsOfUser(userEmail);
388
        Map<String,List<Subscription>> subs = new HashMap<>();
389
        List<Subscription> subscriptions = null;
390

  
391
        for(String s:simpleSubs.keySet()){
392
            List<SimpleSubscriptionDesc> simpleSubscriptionDescs = simpleSubs.get(s);
393
            for(SimpleSubscriptionDesc simpleSubscriptionDesc : simpleSubscriptionDescs) {
394
                subscriptions = new ArrayList<>();
395
                subscriptions.add(getSubscription(simpleSubscriptionDesc.getId()));
396
            }
397
            subs.put(s,subscriptions);
398
        }
399
        return subs;
400
    }
401

  
402

  
403
}
modules/uoa-repository-manager-service/branches/deprecated-api/src/main/java/eu/dnetlib/repo/manager/service/controllers/MonitorApi.java
1
package eu.dnetlib.repo.manager.service.controllers;
2

  
3
import eu.dnetlib.api.functionality.ValidatorServiceException;
4
import eu.dnetlib.domain.functionality.validator.StoredJob;
5
import eu.dnetlib.repo.manager.shared.JobsOfUser;
6
import io.swagger.annotations.Api;
7
import org.json.JSONException;
8
import org.springframework.http.MediaType;
9
import org.springframework.web.bind.annotation.*;
10

  
11
@RestController
12
@RequestMapping(value = "/monitor")
13
@Api(description = "Monitor API",  tags = {"monitor"})
14
public interface MonitorApi {
15

  
16
    @RequestMapping(value = "/getJobsOfUser" , method = RequestMethod.GET,consumes = MediaType.APPLICATION_JSON_VALUE,
17
            produces = MediaType.APPLICATION_JSON_VALUE)
18
    @ResponseBody
19
    JobsOfUser getJobsOfUser(@RequestBody String user,
20
                             @RequestBody String jobType,
21
                             @RequestBody String offset,
22
                             @RequestBody String limit,
23
                             @RequestBody String dateFrom,
24
                             @RequestBody String dateTo,
25
                             @RequestBody String validationStatus,
26
                             @RequestBody String includeJobsTotal) throws JSONException, ValidatorServiceException;
27

  
28
    @RequestMapping(value = "/getJobsOfUserPerValidationStatus" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
29
    @ResponseBody
30
    int getJobsOfUserPerValidationStatus(@RequestBody String user,
31
                                         @RequestBody String jobType,
32
                                         @RequestBody String validationStatus) throws JSONException;
33

  
34
    @RequestMapping(value = "/getJobSummary" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
35
    @ResponseBody
36
    StoredJob getJobSummary(@RequestBody String jobId,
37
                            @RequestBody String groupBy) throws JSONException;
38

  
39
}
modules/uoa-repository-manager-service/branches/deprecated-api/src/main/java/eu/dnetlib/repo/manager/service/controllers/ValidatorApiImpl.java
1
package eu.dnetlib.repo.manager.service.controllers;
2

  
3
import eu.dnetlib.api.functionality.ValidatorServiceException;
4
import eu.dnetlib.domain.functionality.validator.StoredJob;
5
import eu.dnetlib.repo.manager.service.utils.OaiTools;
6
import eu.dnetlib.repo.manager.shared.InterfaceInformation;
7
import eu.dnetlib.repo.manager.shared.ValidationServiceException;
8
import gr.uoa.di.driver.util.ServiceLocator;
9
import eu.dnetlib.domain.functionality.validator.JobForValidation;
10
import eu.dnetlib.domain.functionality.validator.RuleSet;
11
import eu.dnetlib.repo.manager.shared.Constants;
12

  
13
import java.util.*;
14
import java.util.concurrent.ConcurrentHashMap;
15
import eu.dnetlib.api.functionality.ValidatorService;
16
import org.apache.log4j.Logger;
17
import org.json.JSONException;
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.stereotype.Component;
20
import org.springframework.web.bind.annotation.PathVariable;
21

  
22
import javax.annotation.PostConstruct;
23
import javax.annotation.Resource;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff