br.ufrj.ppgi.greco.kettle.sparqlupdate.SparqlUpdate   A
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 340
Duplicated Lines 18.82 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 39
eloc 227
c 1
b 0
f 1
dl 64
loc 340
rs 9.28

13 Methods

Rating   Name   Duplication   Size   Complexity  
A SparqlUpdate(String,String,String,int,String,String) 0 7 1
A createConnection() 20 20 2
A SparqlUpdate(URI,String,String) 0 7 1
A deleteGraph(String) 22 22 2
A insertNTriples(String,String) 0 21 2
A insertTriples(String,String[]) 0 35 4
A deleteTriple(String,String) 0 25 2
D convert(Format,String,String,Format) 0 54 12
A printLog(String,String) 0 2 1
A insertTriples(String,String) 0 30 2
B insertTriples(String,String,int) 0 33 6
A triplify(String,String) 0 22 2
A createGraph(String) 22 22 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
package br.ufrj.ppgi.greco.kettle.sparqlupdate;
2
3
import java.io.ByteArrayOutputStream;
4
import java.net.URI;
5
import java.nio.charset.StandardCharsets;
6
import java.util.ArrayList;
7
import java.util.List;
8
9
import org.apache.any23.Any23;
10
import org.apache.any23.source.DocumentSource;
11
import org.apache.any23.source.StringDocumentSource;
12
import org.apache.any23.writer.NTriplesWriter;
13
import org.apache.any23.writer.RDFXMLWriter;
14
import org.apache.any23.writer.TripleHandler;
15
import org.apache.any23.writer.TurtleWriter;
16
import org.apache.http.HttpHost;
17
import org.apache.http.HttpResponse;
18
import org.apache.http.NameValuePair;
19
import org.apache.http.auth.AuthScope;
20
import org.apache.http.auth.UsernamePasswordCredentials;
21
import org.apache.http.client.AuthCache;
22
import org.apache.http.client.CredentialsProvider;
23
import org.apache.http.client.entity.UrlEncodedFormEntity;
24
import org.apache.http.client.methods.HttpGet;
25
import org.apache.http.client.methods.HttpPost;
26
import org.apache.http.client.protocol.HttpClientContext;
27
import org.apache.http.client.utils.URIBuilder;
28
import org.apache.http.impl.auth.BasicScheme;
29
import org.apache.http.impl.client.BasicAuthCache;
30
import org.apache.http.impl.client.BasicCredentialsProvider;
31
import org.apache.http.impl.client.CloseableHttpClient;
32
import org.apache.http.impl.client.HttpClientBuilder;
33
import org.apache.http.message.BasicNameValuePair;
34
35
public class SparqlUpdate {
36
37
	private String protocol;
38
	private String host;
39
	private String path;
40
	private int port;
41
	private String user;
42
	private String password;
43
44
	public SparqlUpdate(URI uri, String user, String password) {
45
		this.protocol = uri.getScheme();
46
		this.host = uri.getHost();
47
		this.port = uri.getPort();
48
		this.path = uri.getPath();
49
		this.user = user;
50
		this.password = password;
51
	}
52
53
	public SparqlUpdate(String protocol, String host, String path, int port, String user, String password) {
54
		this.protocol = protocol;
55
		this.host = host;
56
		this.path = path;
57
		this.port = port;
58
		this.user = user;
59
		this.password = password;
60
	}
61
62 View Code Duplication
	public int createGraph(String graphURL) throws Exception {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
63
		int resultOperation = 0;
64
65
		CloseableHttpClient httpClient = this.createConnection();
66
67
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
68
		qparams.add(new BasicNameValuePair("query", "CREATE GRAPH <" + graphURL + ">"));
69
		qparams.add(new BasicNameValuePair("format", "application/sparql-results+xml"));
70
71
		try {
72
			URI uri = new URIBuilder().setScheme(this.protocol).setHost(this.host).setPort(this.port).setPath(this.path)
73
					.setParameters(qparams).build();
74
			HttpGet httpget = new HttpGet(uri);
75
			HttpResponse response = httpClient.execute(httpget);
76
			resultOperation = response.getStatusLine().getStatusCode();
77
		} catch (Exception e) {
78
			printLog("SparqlUpdate", "createGraph");
79
			e.printStackTrace();
80
			throw new Exception(e);
81
		}
82
83
		return resultOperation;
84
85
	}
86
87 View Code Duplication
	public int deleteGraph(String graphURL) throws Exception {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
88
		int resultOperation = 0;
89
90
		CloseableHttpClient httpClient = this.createConnection();
91
92
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
93
		qparams.add(new BasicNameValuePair("query", "DROP GRAPH <" + graphURL + ">"));
94
		qparams.add(new BasicNameValuePair("format", "application/sparql-results+xml"));
95
96
		try {
97
			URI uri = new URIBuilder().setScheme(this.protocol).setHost(this.host).setPort(this.port).setPath(this.path)
98
					.setParameters(qparams).build();
99
			HttpGet httpget = new HttpGet(uri);
100
			HttpResponse resposta = httpClient.execute(httpget);
101
			resultOperation = resposta.getStatusLine().getStatusCode();
102
103
		} catch (Exception e) {
104
			printLog("SparqlUpdate", "deleteGraph");
105
			throw new Exception(e);
106
		}
107
108
		return resultOperation;
109
	}
110
111
	public HttpResponse insertNTriples(String graph, String triples) throws Exception {
112
		CloseableHttpClient httpClient = this.createConnection();
113
114
		try {
115
			List<NameValuePair> qParams = new ArrayList<NameValuePair>();
116
			String query = "INSERT INTO GRAPH <" + graph + "> {\n" + triples + "\n" + "}";
117
			qParams.add(new BasicNameValuePair("query", query));
118
119
			URI uri = new URIBuilder().setScheme(this.protocol).setHost(this.host).setPort(this.port).setPath(this.path)
120
					.build();
121
			HttpPost httpPost = new HttpPost(uri);
122
			httpPost.setEntity(new UrlEncodedFormEntity(qParams, StandardCharsets.UTF_8));
123
124
			HttpResponse resposta = httpClient.execute(httpPost);
125
126
			return resposta;
127
128
		} catch (Exception e) {
129
			printLog("SparqlUpdate", "insertTriples");
130
			e.printStackTrace();
131
			throw new Exception(e);
132
		}
133
	}
134
135
	public Response insertTriples(String graph, String RDFXML) throws Exception {
136
		Response operationResult = null;
137
138
		String n3 = this.triplify(graph, RDFXML);
139
		CloseableHttpClient httpClient = this.createConnection();
140
141
		try {
142
143
			List<NameValuePair> qParams = new ArrayList<NameValuePair>();
144
			String query = "INSERT DATA INTO <" + graph + "> {" + n3 + "}";
145
			qParams.add(new BasicNameValuePair("query", query));
146
			System.out.println(graph);
147
148
			URI uri = new URIBuilder().setScheme(this.protocol).setHost(this.host).setPort(this.port).setPath(this.path)
149
					.build();
150
			HttpPost httpPost = new HttpPost(uri);
151
			httpPost.setEntity(new UrlEncodedFormEntity(qParams, StandardCharsets.UTF_8));
152
153
			HttpResponse resposta = httpClient.execute(httpPost);
154
155
			operationResult = new Response(resposta.getStatusLine().getStatusCode(),
156
					resposta.getStatusLine().getReasonPhrase());
157
158
		} catch (Exception e) {
159
			printLog("SparqlUpdate", "insertTriples");
160
			e.printStackTrace();
161
			throw new Exception(e);
162
		}
163
164
		return operationResult;
165
	}
166
167
	public Response insertTriples(String graph, String[] n3) throws Exception {
168
		Response operationResult = null;
169
		try {
170
			StringBuffer sb = new StringBuffer();
171
			for (String triple : n3) {
172
173
				if (triple != null) {
174
					sb.append(triple);
175
					sb.append('\n');
176
				}
177
			}
178
			String triples = sb.toString();
179
180
			CloseableHttpClient httpClient = this.createConnection();
181
			List<NameValuePair> qParams = new ArrayList<NameValuePair>();
182
			String query = "INSERT DATA INTO <" + graph + "> {" + triples + "}";
183
			qParams.add(new BasicNameValuePair("query", query));
184
185
			URI uri = new URIBuilder().setScheme(this.protocol).setHost(this.host).setPort(this.port).setPath(this.path)
186
					.build();
187
			HttpPost httpPost = new HttpPost(uri);
188
			httpPost.setEntity(new UrlEncodedFormEntity(qParams, StandardCharsets.UTF_8));
189
190
			HttpResponse resposta = httpClient.execute(httpPost);
191
192
			operationResult = new Response(resposta.getStatusLine().getStatusCode(),
193
					resposta.getStatusLine().getReasonPhrase());
194
195
		} catch (Exception e) {
196
			printLog("SparqlUpdate", "insertTriples");
197
			e.printStackTrace();
198
			throw new Exception(e);
199
		}
200
201
		return operationResult;
202
	}
203
204
	public Response insertTriples(String graph, String rdfxml, int blockSize) throws Exception {
205
		Response operationResult = null;
206
207
		String[] n3 = this.triplify(graph, rdfxml).split("\n");
208
209
		int i = 0;
210
		String[] n2short = new String[blockSize];
211
212
		for (String triple : n3) {
213
			n2short[i] = triple;
214
			i++;
215
216
			if (i == blockSize) {
217
				operationResult = insertTriples(graph, n2short);
218
219
				i = 0;
220
				n2short = new String[blockSize];
221
222
				if (operationResult.getStatusCode() != 200)
223
					return operationResult;
224
			}
225
		}
226
227
		if (i > 0) {
228
			operationResult = insertTriples(graph, n2short);
229
230
			if (operationResult.getStatusCode() != 200)
231
				return operationResult;
232
		}
233
234
		n2short = null;
235
236
		return operationResult;
237
	}
238
239
	public int deleteTriple(String graph, String RDFXML) {
240
		int operationResult = 0;
241
242
		CloseableHttpClient httpClient = this.createConnection();
243
244
		String n3 = this.triplify(graph, RDFXML);
245
246
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
247
		qparams.add(new BasicNameValuePair("query", "DELETE FROM <" + graph + ">{" + n3 + "}"));
248
		qparams.add(new BasicNameValuePair("format", "application/sparql-results+xml"));
249
250
		try {
251
			URI uri = new URIBuilder().setScheme(this.protocol).setHost(this.host).setPort(this.port).setPath(this.path)
252
					.setParameters(qparams).build();
253
			HttpGet httpget = new HttpGet(uri);
254
			HttpResponse resposta = httpClient.execute(httpget);
255
256
			operationResult = resposta.getStatusLine().getStatusCode();
257
258
		} catch (Exception e) {
259
			printLog("SparqlUpdate", "insertTriples");
260
261
		}
262
263
		return operationResult;
264
	}
265
266
	public enum Format {
267
		NTRIPLES, RDFXML, TURTLE
268
	}
269
270
	// wrong: rdf->n3, n3->turtle, rdf->n3
271
	public static String convert(Format inputFormat, String inputData, String defaultURI, Format outputFormat)
272
			throws Exception {
273
		String n3 = "";
274
		try {
275
			if (inputData == null || inputData.length() == 0)
276
				throw new NullPointerException("local");
277
278
			Any23 runner = new Any23();
279
280
			String inputMime = "";
281
			switch (inputFormat) {// TODO N3
282
			case NTRIPLES:
283
				inputMime = "text/plain";
284
				break;
285
			case RDFXML:
286
				inputMime = "application/rdf+xml";
287
				break;
288
			case TURTLE:
289
				inputMime = "text/turtle";
290
				break;
291
			default:
292
				throw new Exception("Formato de entrada invalido");
293
			}
294
295
			DocumentSource source = new StringDocumentSource(inputData, defaultURI, inputMime, "UTF-8");
296
297
			ByteArrayOutputStream out = new ByteArrayOutputStream();
298
			TripleHandler handler = null;
299
300
			switch (outputFormat) {
301
			case NTRIPLES:
302
				handler = new NTriplesWriter(out);
303
				break;
304
			case RDFXML:
305
				handler = new RDFXMLWriter(out);
306
				break;
307
			case TURTLE:
308
				handler = new TurtleWriter(out);
309
				break;
310
			default:
311
				throw new Exception("Formato de saida invalido");
312
			}
313
314
			runner.extract(source, handler);
315
			n3 = out.toString("UTF-8");
316
		} catch (Exception e) {
317
			e.printStackTrace();
318
			printLog("SparqlUpdate", "convert");
319
320
			String msg = String.format("%s -> %s. INPUT=[[%s]]", inputFormat.name(), outputFormat.name(), inputData);
321
322
			throw new Exception(msg, e);
323
		}
324
		return n3;
325
	}
326
327
	private String triplify(String defaultURI, String rdfxml) {
328
		String n3 = "";
329
		try {
330
			Any23 runner = new Any23();
331
332
			DocumentSource source = new StringDocumentSource(rdfxml, defaultURI, "application/rdf+xml", "UTF-8");
333
334
			// DocumentSource source2 = new StringDocumentSource(rdfxml,
335
			// defaultURI,
336
			// "text/rdf+n3", "UTF-8");
337
			//
338
			// DocumentSource source = source1 == null ? source2 : source1;
339
340
			ByteArrayOutputStream out = new ByteArrayOutputStream();
341
			TripleHandler handler = new NTriplesWriter(out);
342
			runner.extract(source, handler);
343
			n3 = out.toString("UTF-8");
344
		} catch (Exception e) {
345
			e.printStackTrace();
346
			printLog("SparqlUpdate", "triplify");
347
		}
348
		return n3;
349
	}
350
351 View Code Duplication
	private CloseableHttpClient createConnection() {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
352
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
353
354
		if (this.password != null) {
355
			HttpHost targetHost = new HttpHost(this.host, this.port, this.protocol);
356
			CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
357
			credentialsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
358
					new UsernamePasswordCredentials(this.user, this.password));
359
360
			httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
361
362
			AuthCache authCache = new BasicAuthCache();
363
			BasicScheme basicAuth = new BasicScheme();
364
			authCache.put(targetHost, basicAuth);
365
			HttpClientContext context = HttpClientContext.create();
366
			context.setCredentialsProvider(credentialsProvider);
367
			context.setAuthCache(authCache);
368
		}
369
370
		return httpClient;
371
	}
372
373
	private static void printLog(String classe, String method) {
374
		System.out.println("Problems:\n class:" + classe + "\n method:" + method + "\n\n");
375
	}
376
377
}
378