Project

General

Profile

« Previous | Next » 

Revision 55561

Removed dep to gcube registry and ignore integration tests

View differences:

CatalogueAPIClient.java
4 4
import java.net.URI;
5 5
import java.net.URISyntaxException;
6 6

  
7
import com.google.common.collect.Lists;
7 8
import eu.dnetlib.parthenos.jrr.ParthenosRegistryResource;
8 9
import eu.dnetlib.parthenos.publisher.ParthenosPublisherException;
9 10
import org.apache.commons.logging.Log;
......
27 28

  
28 29
	@Value("${gcube.catalogue.baseurl}")
29 30
	private String baseURL;
31
	private final String itemPath = "items/";
30 32
	@Value("${gcube.registry.application.token}")
31 33
	private String applicationToken;
32 34
	@Value("${gcube.uri.resolver}")
33 35
	private String uriResolver;
34 36
	private String resolverBodyTemplate = "{ \"entity_name\": \"%s\" }";
37
	private String purgeBodyTemplate = "{\"id\":\"%s\"}";
35 38

  
36
	private final String createItemPath = "rest/api/items/create";
37
	private final String updateItemPath = "rest/api/items/update";
38
	private final String showItemPath = "rest/api/items/show"; //param id
39
	private final String purgeItemPath = "rest/api/items/purge";
39
	private HttpHeaders headersForResolver;
40
	private HttpHeaders headersForCatalogue;
40 41

  
41
	//private final String showGroupPath = "rest/api/groups/show"; //param id
42
	//private final String createGroupPath = "rest/api/groups/create";
43
	//private final String purgeGroupPath = "rest/api/groups/purge";
44
	private String purgeBodyTemplate = "{\"id\":\"%s\"}";
45

  
46 42
	@Autowired
47
	private RestTemplate jrrRestTemplate;
43
	private RestTemplate restTemplate;
48 44

  
45
	public CatalogueAPIClient(){
46
		headersForResolver = new HttpHeaders();
47
		headersForResolver.setContentType(MediaType.APPLICATION_JSON);
48

  
49
		headersForCatalogue = new HttpHeaders();
50
		headersForCatalogue.setContentType(MediaType.APPLICATION_JSON);
51
		headersForCatalogue.setAccept(Lists.newArrayList(MediaType.APPLICATION_JSON));
52
		headersForCatalogue.set("gcube-token", getApplicationToken());
53
	}
54

  
55

  
49 56
	/**
50 57
	 * Calls the parthenos resolver to get the name to use for the catalogue
51 58
	 * @param resName Given a Parthenos URL like: http://parthenos.d4science.org/handle/Ariadne/AriadnePortal/rhdsq5xm3e40, the resName is Ariadne/AriadnePortal/rhdsq5xm3e40.
52 59
	 * @return the name as computed by the Parthenos resolver. Null if the request was not successfull.
53 60
	 */
54 61
	public String getNameForCatalogue(final String resName) throws URISyntaxException {
55
		log.debug(String.format("Calling Parthenos resolver for "+resName));
62
		log.debug("Calling Parthenos resolver for "+resName);
56 63
		String body = String.format(resolverBodyTemplate, resName);
57
		HttpEntity<String> entity = new HttpEntity<String>(body, getHeaders());
64
		HttpEntity<String> entity = new HttpEntity<String>(body, headersForResolver);
58 65
		URI uri = new URIBuilder(getUriResolver()).build();
59 66
		log.debug("Resolver at "+getUriResolver()+" post body:\n"+body);
60
		ResponseEntity<String> res = jrrRestTemplate.<String>exchange(uri, HttpMethod.POST, entity, String.class);
67
		ResponseEntity<String> res = restTemplate.exchange(uri, HttpMethod.POST, entity, String.class);
61 68
		if(res.getStatusCode().is2xxSuccessful()){
62 69
			String resolved = res.getBody();
63 70
			log.debug(String.format("Parthenos resolver resolved %s with %s", resName, resolved ));
......
71 78

  
72 79
	public ParthenosRegistryResource getRegistered(final String resCatName) throws ParthenosPublisherException {
73 80
		log.debug(String.format("Catalogue --> Checking if item %s exists", resCatName));
74
		HttpEntity<String> entity = new HttpEntity<String>(getHeaders());
81
		HttpEntity<String> entity = new HttpEntity<String>(headersForCatalogue);
75 82
		try {
76
			URI uri = new URIBuilder(getBaseURL()+showItemPath).addParameter("gcube-token", getApplicationToken()).addParameter("id", resCatName).build();
77
			ResponseEntity<String> res = jrrRestTemplate.<String>exchange(uri, HttpMethod.GET, entity, String.class);
78
			CatalogueAPIResponse response = new CatalogueAPIResponse();
79
			response.setResponseBody(res.getBody());
80
			if(response.isSuccess()){
81
				ParthenosRegistryResource r = response.getParthenosRegistryResource();
83
			URI uri = new URIBuilder(getBaseURL()+itemPath).build();
84
			ResponseEntity<String> response
85
					= restTemplate.exchange(uri + "/"+resCatName, HttpMethod.GET, entity, String.class);
86
			if(response.getStatusCode().is2xxSuccessful()){
87
				CatalogueAPIResponse body = new CatalogueAPIResponse();
88
				body.setResponseBody(response.getBody());
89
				ParthenosRegistryResource r = body.getParthenosRegistryResource();
82 90
				log.debug(String.format("Resource %s is in the catalogue with uuid %s", resCatName, r.getUuid()));
83 91
				return r;
84 92
			}
......
99 107
	 */
100 108
	public String doRegister(final String json, final String resCatName) throws URISyntaxException, IOException {
101 109
		log.debug(String.format("Catalogue --> Registering item %s : %s", resCatName, json));
102
		return doCatalogueCall(json, resCatName, createItemPath);
110
		return doCatalogueCall(json, resCatName, HttpMethod.POST);
103 111
	}
104 112

  
105 113
	public String doUpdate(final String json, final String resCatName) throws IOException, URISyntaxException {
106 114
		log.debug(String.format("Catalogue --> Updating item %s : %s", resCatName, json));
107
		return doCatalogueCall(json, resCatName, updateItemPath);
115
		return doCatalogueCall(json, resCatName, HttpMethod.PUT);
108 116
	}
109 117

  
110
	protected String doCatalogueCall(final String json, final String resCatName, final String actionPath) throws URISyntaxException, IOException {
111
		HttpEntity<String> entity = new HttpEntity<String>(json, getHeaders());
112
		URI uri = new URIBuilder(getBaseURL() + actionPath).addParameter("gcube-token", getApplicationToken()).build();
113
		ResponseEntity<String> res = jrrRestTemplate.exchange(uri, HttpMethod.POST, entity, String.class);
114
		CatalogueAPIResponse response = new CatalogueAPIResponse();
115
		if (res.getStatusCode() == HttpStatus.OK) {
116
			String body = res.getBody();
117
			response.setResponseBody(body);
118
			if (response.isSuccess()) {
119
				return response.getParthenosRegistryResource().getUuid();
120
			} else {
121
				log.warn(resCatName + " could not be registered/updated because of " + response.getErrorMessage());
122
				return null;
123
			}
118
	protected String doCatalogueCall(final String json, final String resCatName, final HttpMethod method) throws URISyntaxException, IOException {
119
		HttpEntity<String> entity = new HttpEntity<String>(json, headersForCatalogue);
120
		URI uri = new URIBuilder(getBaseURL()+itemPath).build();
121
		ResponseEntity<String> res = restTemplate.exchange(uri, method, entity, String.class);
122
		if (res.getStatusCode().is2xxSuccessful()) {
123
			CatalogueAPIResponse response = new CatalogueAPIResponse();
124
			response.setResponseBody(res.getBody());
125
			return response.getParthenosRegistryResource().getUuid();
124 126
		} else {
125
			log.warn(resCatName + " POST FAILED: " + res.getStatusCodeValue());
127
			log.error(String.format("Method %s on resource %s failed with code %s, reason: %s", method.name(), resCatName, res.getStatusCodeValue(), res.getBody()));
126 128
			return null;
127 129
		}
128 130
	}
129 131

  
130 132
	protected boolean purgeItem(final String resCatName) throws URISyntaxException, IOException {
131 133
		log.debug(String.format("Catalogue --> Purge Item %s", resCatName));
132
		return purge(purgeItemPath, resCatName);
133
	}
134

  
135

  
136
	private boolean purge(String purgePath, String id) throws URISyntaxException, IOException {
137
		String body = String.format(purgeBodyTemplate, id);
134
		String body = String.format(purgeBodyTemplate, resCatName);
138 135
		log.warn("DEL BODY: "+body);
139
		HttpEntity<String> entity = new HttpEntity<String>(body, getHeaders());
140
		URI uri = new URIBuilder(getBaseURL()+purgePath).addParameter("gcube-token", getApplicationToken()).build();
141
		ResponseEntity<String> res = jrrRestTemplate.exchange(uri, HttpMethod.DELETE, entity, String.class);
142
		CatalogueAPIResponse response = new CatalogueAPIResponse();
143
		if(res.getStatusCode() == HttpStatus.OK){
144
			String resBody = res.getBody();
145
			response.setResponseBody(resBody);
146
			//TODO: may be useful to log something when the request did not succeeded
147
			return response.isSuccess();
136
		HttpEntity<String> entity = new HttpEntity<String>(body, headersForCatalogue);
137
		URI uri = new URIBuilder(getBaseURL()+itemPath+resCatName).addParameter("purge", "true").build();
138
		ResponseEntity<String> res = restTemplate.exchange(uri, HttpMethod.DELETE, entity, String.class);
139
		if(res.getStatusCode().is2xxSuccessful()){
140
			return true;
148 141
		}
149 142
		else {
150
			log.error(res.getStatusCodeValue());
143
			log.error(String.format("Cannot purge item %s. HTTP error code %s, reason: %s", resCatName, res.getStatusCodeValue(), res.getBody()));
151 144
			return false;
152 145
		}
153 146
	}
154 147

  
155 148

  
156
	private HttpHeaders getHeaders() {
157
		HttpHeaders headers = new HttpHeaders();
158
		headers.setContentType(MediaType.APPLICATION_JSON);
159
		return headers;
160
	}
161

  
162 149
	public String getBaseURL() {
163 150
		return baseURL;
164 151
	}
......
175 162
		this.applicationToken = applicationToken;
176 163
	}
177 164

  
178
	public String getCreateItemPath() {
179
		return createItemPath;
165
	public RestTemplate getRestTemplate() {
166
		return restTemplate;
180 167
	}
181 168

  
182
	public String getShowItemPath() {
183
		return showItemPath;
169
	public void setRestTemplate(final RestTemplate restTemplate) {
170
		this.restTemplate = restTemplate;
184 171
	}
185 172

  
186
	public RestTemplate getJrrRestTemplate() {
187
		return jrrRestTemplate;
188
	}
189

  
190
	public void setJrrRestTemplate(final RestTemplate jrrRestTemplate) {
191
		this.jrrRestTemplate = jrrRestTemplate;
192
	}
193

  
194 173
	public String getUriResolver() {
195 174
		return uriResolver;
196 175
	}
......
199 178
		this.uriResolver = uriResolver;
200 179
	}
201 180

  
202
	public String getPurgeItemPath() {
203
		return purgeItemPath;
204
	}
205

  
206 181
	public String getPurgeBodyTemplate() {
207 182
		return purgeBodyTemplate;
208 183
	}

Also available in: Unified diff