2424import javax .net .ssl .X509ExtendedTrustManager ;
2525import javax .net .ssl .X509TrustManager ;
2626
27- import joptsimple .OptionException ;
28- import joptsimple .OptionParser ;
29- import joptsimple .OptionSet ;
30- import joptsimple .OptionSpec ;
31- import joptsimple .ValueConversionException ;
32- import joptsimple .ValueConverter ;
33-
3427import org .apache .http .auth .AuthScope ;
3528import org .apache .http .auth .UsernamePasswordCredentials ;
3629import org .apache .http .client .CredentialsProvider ;
3730import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
3831import org .apache .http .impl .client .BasicCredentialsProvider ;
39- import org .apache .http .impl .client .CloseableHttpClient ;
32+ import org .apache .http .impl .client .HttpClientBuilder ;
4033import org .apache .http .impl .client .cache .BasicHttpCacheStorage ;
4134import org .apache .http .impl .client .cache .CacheConfig ;
4235import org .apache .http .impl .client .cache .CachingHttpClientBuilder ;
4639import org .eclipse .rdf4j .rio .Rio ;
4740
4841import com .github .jsonldjava .core .DocumentLoader ;
49- import com .github .jsonldjava .core .JsonLdConsts ;
5042import com .github .jsonldjava .core .JsonLdApi ;
43+ import com .github .jsonldjava .core .JsonLdConsts ;
5144import com .github .jsonldjava .core .JsonLdOptions ;
5245import com .github .jsonldjava .core .JsonLdProcessor ;
5346import com .github .jsonldjava .core .RDFDataset ;
5447import com .github .jsonldjava .utils .JarCacheStorage ;
5548import com .github .jsonldjava .utils .JsonUtils ;
5649
50+ import joptsimple .OptionException ;
51+ import joptsimple .OptionParser ;
52+ import joptsimple .OptionSet ;
53+ import joptsimple .OptionSpec ;
54+ import joptsimple .ValueConversionException ;
55+ import joptsimple .ValueConverter ;
56+
5757/**
5858 * A command-line-interface used to load and process JSON-LD and RDF files.
5959 */
@@ -81,14 +81,14 @@ public static void main(String[] args) throws Exception {
8181 final OptionSpec <RDFFormat > outputFormat = parser .accepts ("format" ).withOptionalArg ()
8282 .ofType (String .class ).withValuesConvertedBy (new ValueConverter <RDFFormat >() {
8383 @ Override
84- public RDFFormat convert (String arg0 ) {
84+ public RDFFormat convert (String inputFormat ) {
8585 // Normalise the name to provide alternatives
86- final String formatName = arg0 .replaceAll ("-" , "" ). replaceAll ( "/ " , "" )
87- .toLowerCase ();
86+ final String formatName = inputFormat .replaceAll ("-" , "" )
87+ .replaceAll ( "/" , "" ). toLowerCase ();
8888 if (formats .containsKey (formatName )) {
8989 return formats .get (formatName );
9090 }
91- throw new ValueConversionException ("Format was not known: " + arg0
91+ throw new ValueConversionException ("Format was not known: " + inputFormat
9292 + " (Valid values are: " + formats .keySet () + ")" );
9393 }
9494
@@ -158,7 +158,8 @@ public Class<String> valueType() {
158158 .ofType (String .class ).describedAs ("username for basic authentication credentials" );
159159
160160 final OptionSpec <String > passwordOption = parser .accepts ("password" ).withOptionalArg ()
161- .ofType (String .class ).describedAs ("password for basic authentication credentials (defaults to value of 'PASSWORD' environment property, if set, or empty string otherwise)" );
161+ .ofType (String .class ).describedAs (
162+ "password for basic authentication credentials (defaults to value of 'PASSWORD' environment property, if set, or empty string otherwise)" );
162163
163164 final OptionSpec <String > authHostOption = parser .accepts ("authHost" ).withOptionalArg ()
164165 .ofType (String .class ).defaultsTo ("localhost" )
@@ -168,7 +169,8 @@ public Class<String> valueType() {
168169 .ofType (Integer .class ).defaultsTo (443 )
169170 .describedAs ("host port authentication scope" );
170171
171- final OptionSpec <Void > authInsecureOption = parser .accepts ("insecure" ,"Similar to `curl -k` or `curl --insecure`: if unspecified, all SSL connections are secure by default; if specified, trust everything (do not use for production!)" );
172+ final OptionSpec <Void > authInsecureOption = parser .accepts ("insecure" ,
173+ "Similar to `curl -k` or `curl --insecure`: if unspecified, all SSL connections are secure by default; if specified, trust everything (do not use for production!)" );
172174
173175 OptionSet options = null ;
174176
@@ -223,71 +225,38 @@ public Class<String> valueType() {
223225 final DocumentLoader documentLoader = new DocumentLoader ();
224226
225227 final CredentialsProvider credsProvider = new BasicCredentialsProvider ();
226- credsProvider .setCredentials (
227- new AuthScope (authHost , authPort ),
228+ credsProvider .setCredentials (new AuthScope (authHost , authPort ),
228229 new UsernamePasswordCredentials (username , password ));
229230
230- final CacheConfig cacheConfig = CacheConfig .custom ()
231- .setMaxCacheEntries (1000 )
231+ final CacheConfig cacheConfig = CacheConfig .custom ().setMaxCacheEntries (1000 )
232232 .setMaxObjectSize (1024 * 128 ).build ();
233233
234- if (options .has (authInsecureOption )) {
234+ HttpClientBuilder builder = CachingHttpClientBuilder .create ()
235+ // allow caching
236+ .setCacheConfig (cacheConfig )
237+ // Wrap the local JarCacheStorage around a
238+ // BasicHttpCacheStorage
239+ .setHttpCacheStorage (new JarCacheStorage (null , cacheConfig ,
240+ new BasicHttpCacheStorage (cacheConfig )))
241+ .setDefaultCredentialsProvider (credsProvider );
235242
243+ if (options .has (authInsecureOption )) {
236244 final SSLContext ctx = SSLContext .getInstance ("TLS" );
237- final X509TrustManager tm = new InsecureX509TrustManager ();
238- ctx .init (null , new TrustManager [] { tm }, null );
239-
245+ ctx .init (null , new TrustManager [] { new InsecureX509TrustManager () }, null );
240246 final HostnameVerifier v = new HostnameVerifier () {
241-
242247 @ Override
243248 public boolean verify (String s , SSLSession sslSession ) {
244249 return true ;
245250 }
246251 };
247252
248253 final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory (ctx , v );
249-
250- final CloseableHttpClient httpClient = CachingHttpClientBuilder
251- .create ()
252- // allow caching
253- .setCacheConfig (cacheConfig )
254- // Wrap the local JarCacheStorage around a BasicHttpCacheStorage
255- .setHttpCacheStorage (
256- new JarCacheStorage (null , cacheConfig , new BasicHttpCacheStorage (
257- cacheConfig )))
258-
259- // Add in the credentials provider
260- .setDefaultCredentialsProvider (credsProvider )
261-
262- // insecure ssl connections
263- .setSSLSocketFactory (sslsf )
264-
265- // When you are finished setting the properties, call build
266- .build ();
267- documentLoader .setHttpClient (httpClient );
268- opts .setDocumentLoader (documentLoader );
269-
270- } else {
271-
272- final CloseableHttpClient httpClient = CachingHttpClientBuilder
273- .create ()
274- // allow caching
275- .setCacheConfig (cacheConfig )
276- // Wrap the local JarCacheStorage around a BasicHttpCacheStorage
277- .setHttpCacheStorage (
278- new JarCacheStorage (null , cacheConfig , new BasicHttpCacheStorage (
279- cacheConfig )))
280-
281- // Add in the credentials provider
282- .setDefaultCredentialsProvider (credsProvider )
283-
284- // When you are finished setting the properties, call build
285- .build ();
286-
287- documentLoader .setHttpClient (httpClient );
288- opts .setDocumentLoader (documentLoader );
289-
254+ // insecure ssl connections
255+ builder = builder .setSSLSocketFactory (sslsf );
290256 }
257+
258+ documentLoader .setHttpClient (builder .build ());
259+ opts .setDocumentLoader (documentLoader );
291260 }
292261
293262 if ("fromrdf" .equals (processingOptionValue )) {
@@ -331,18 +300,20 @@ public boolean verify(String s, SSLSession sslSession) {
331300 // outobj = JsonLdProcessor.normalize(inobj, opts);
332301
333302 // see https://github.com/jsonld-java/jsonld-java/issues/194
334- // until this is fixed, it is necessary to clear the format so that JsonLdProcessor won't try to interpret it.
303+ // until this is fixed, it is necessary to clear the format so that
304+ // JsonLdProcessor won't try to interpret it.
335305 opts .format = null ;
336306
337- // If an output format is specified, add a callback to show the result.
338- Object result = JsonLdProcessor . toRDF (
339- inobj ,
307+ // If an output format is specified, add a callback to show the
308+ // result.
309+ final Object result = JsonLdProcessor . toRDF ( inobj ,
340310 options .has (outputFormat )
341- ? new RDF4JJSONLDTripleCallback (Rio .createWriter (sesameOutputFormat , System .out ))
311+ ? new RDF4JJSONLDTripleCallback (
312+ Rio .createWriter (sesameOutputFormat , System .out ))
342313 : null ,
343314 opts );
344315 if (RDFDataset .class .isInstance (result )) {
345- RDFDataset rdfds = RDFDataset .class .cast (result );
316+ final RDFDataset rdfds = RDFDataset .class .cast (result );
346317 outobj = new JsonLdApi (opts ).normalize (rdfds );
347318 } else {
348319 outobj = result ;
@@ -397,45 +368,52 @@ private static Map<String, RDFFormat> getOutputFormats() {
397368 }
398369
399370 private static String readFile (File in ) throws IOException {
400- String inobj = "" ;
401- try (BufferedReader buf = Files .newBufferedReader (in .toPath (), StandardCharsets .UTF_8 )){
371+ final StringBuilder inobj = new StringBuilder ( 1024 ) ;
372+ try (BufferedReader buf = Files .newBufferedReader (in .toPath (), StandardCharsets .UTF_8 )) {
402373 String line ;
403374 while ((line = buf .readLine ()) != null ) {
404375 line = line .trim ();
405- inobj = ( inobj ) + line + " \n " ;
376+ inobj . append ( line ). append ( '\n' ) ;
406377 }
407378 }
408- return inobj ;
379+ return inobj . toString () ;
409380 }
410381
411- private static class InsecureX509TrustManager extends X509ExtendedTrustManager implements X509TrustManager {
382+ private static class InsecureX509TrustManager extends X509ExtendedTrustManager
383+ implements X509TrustManager {
412384
385+ @ Override
413386 public void checkClientTrusted (X509Certificate [] xcs , String string ) {
414387 }
415388
416389 @ Override
417- public void checkServerTrusted (X509Certificate [] xcs , String string ) throws CertificateException {
390+ public void checkServerTrusted (X509Certificate [] xcs , String string )
391+ throws CertificateException {
418392 }
419393
394+ @ Override
420395 public java .security .cert .X509Certificate [] getAcceptedIssuers () {
421396 return null ;
422397 }
423398
424-
425399 @ Override
426- public void checkClientTrusted (X509Certificate [] x509Certificates , String s , Socket socket ) throws CertificateException {
400+ public void checkClientTrusted (X509Certificate [] x509Certificates , String s , Socket socket )
401+ throws CertificateException {
427402 }
428403
429404 @ Override
430- public void checkServerTrusted (X509Certificate [] x509Certificates , String s , Socket socket ) throws CertificateException {
405+ public void checkServerTrusted (X509Certificate [] x509Certificates , String s , Socket socket )
406+ throws CertificateException {
431407 }
432408
433409 @ Override
434- public void checkClientTrusted (X509Certificate [] x509Certificates , String s , SSLEngine sslEngine ) throws CertificateException {
410+ public void checkClientTrusted (X509Certificate [] x509Certificates , String s ,
411+ SSLEngine sslEngine ) throws CertificateException {
435412 }
436413
437414 @ Override
438- public void checkServerTrusted (X509Certificate [] x509Certificates , String s , SSLEngine sslEngine ) throws CertificateException {
415+ public void checkServerTrusted (X509Certificate [] x509Certificates , String s ,
416+ SSLEngine sslEngine ) throws CertificateException {
439417 }
440418 }
441419
0 commit comments