66import java .io .FileOutputStream ;
77import java .io .IOException ;
88import java .io .InputStream ;
9- import java .net .HttpURLConnection ;
10- import java .net .InetSocketAddress ;
11- import java .net .MalformedURLException ;
12- import java .net .Proxy ;
13- import java .net .URL ;
9+ import java .net .*;
1410import java .nio .file .Files ;
1511import java .util .HashSet ;
1612import java .util .Hashtable ;
3329import edu .kit .scc .dem .wapsrv .exceptions .FormatException ;
3430import edu .kit .scc .dem .wapsrv .exceptions .InternalServerException ;
3531import edu .kit .scc .dem .wapsrv .model .FormattableObject .Type ;
36- import java .net .URI ;
3732
3833/**
3934 * Central registry for JSON-LD profiles. It manages a local in memory profile
@@ -92,19 +87,19 @@ public final class JsonLdProfileRegistry {
9287 * The set of profiles loaded and cached. This set is dynamically populated
9388 * on first usage.
9489 */
95- private final Set <URL > cachedProfiles = new HashSet <URL >();
90+ private final Set <URI > cachedProfiles = new HashSet <>();
9691 /**
9792 * The last times individual profiles have been updated
9893 */
99- private final Map <URL , Long > lastUpdateTimes = new Hashtable <URL , Long >();
94+ private final Map <URI , Long > lastUpdateTimes = new Hashtable <>();
10095 /**
10196 * The number individual profiles have had update failures
10297 */
103- private final Map <URL , Integer > profile2failures = new Hashtable <URL , Integer >();
98+ private final Map <URI , Integer > profile2failures = new Hashtable <>();
10499 /**
105100 * The last times individual profiles have had update failures
106101 */
107- private final Map <URL , Long > profile2failureTimes = new Hashtable <URL , Long >();
102+ private final Map <URI , Long > profile2failureTimes = new Hashtable <>();
108103 /**
109104 * The frames needed for specific types
110105 */
@@ -258,10 +253,10 @@ private void updateProfiles() {
258253 for (Object key : profilesDatabase .keySet ()) {
259254 final String filename = (String ) key ;
260255 final String urlString = profilesDatabase .getProperty (filename );
261- URL url = null ;
256+ URI url = null ;
262257 try {
263- url = new URL (urlString );
264- } catch (MalformedURLException e ) {
258+ url = new URI (urlString );
259+ } catch (URISyntaxException e ) {
265260 logger .warn ("Invalid profile url, skipping it : " + urlString );
266261 continue ;
267262 }
@@ -354,8 +349,9 @@ private void updateProfiles() {
354349 setJsonLdOptions (jsonLdOptions );
355350 }
356351
357- private boolean download (File destinationFile , URL srcUrl ) {
352+ private boolean download (File destinationFile , URI srcUri ) {
358353 try {
354+ URL srcUrl = srcUri .toURL ();
359355 Proxy proxy = getProxy (srcUrl );
360356 HttpURLConnection httpConn
361357 = (HttpURLConnection ) (proxy == null ? srcUrl .openConnection () : srcUrl .openConnection (proxy ));
@@ -446,10 +442,10 @@ private Proxy getProxy(URL srcUrl) {
446442 * @param url The URL to test
447443 * @return True if cached, false otherwise
448444 */
449- public boolean isCachedProfile (URL url ) {
445+ public boolean isCachedProfile (URI url ) {
450446 blockUntilInitialized ();
451447 // always check for http and https. The file is the same, no need to distinguish
452- URL complementaryUrl = toComplementaryUrl (url );
448+ URI complementaryUrl = toComplementaryUrl (url );
453449 return cachedProfiles .contains (url ) || cachedProfiles .contains (complementaryUrl );
454450 }
455451
@@ -634,14 +630,14 @@ private String toComplementaryUrl(String urlString) {
634630 * @param url The url to convert
635631 * @return The converted url
636632 */
637- private URL toComplementaryUrl (URL url ) {
633+ private URI toComplementaryUrl (URI url ) {
638634 if (url == null ) {
639635 return null ;
640636 }
641637 String compString = toComplementaryUrl (url .toString ());
642638 try {
643- return new URL (compString );
644- } catch (MalformedURLException e ) {
639+ return new URI (compString );
640+ } catch (URISyntaxException e ) {
645641 // should not happen
646642 logger .warn ("could not complement url, this should not happen : " + url );
647643 // return the unswitched url as fallback
@@ -658,17 +654,17 @@ private URL toComplementaryUrl(URL url) {
658654 * @param url The URL to cache
659655 * @return True if already cached or caching was successful, false otherwise
660656 */
661- public boolean cacheProfile (URL url ) {
657+ public boolean cacheProfile (URI url ) {
662658 if (url == null ) {
663659 return false ;
664660 }
665661 blockUntilInitialized ();
666662 synchronized (cachedProfiles ) {
667- for (URL urlRegistered : cachedProfiles ) {
663+ for (URI urlRegistered : cachedProfiles ) {
668664 if (url .equals (urlRegistered )) {
669665 return true ;
670666 }
671- URL complementaryUrl = toComplementaryUrl (urlRegistered );
667+ URI complementaryUrl = toComplementaryUrl (urlRegistered );
672668 if (url .equals (complementaryUrl )) {
673669 return true ;
674670 }
@@ -681,8 +677,8 @@ public boolean cacheProfile(URL url) {
681677 synchronized (updateLock ) {
682678 final String urlString = url .toString ();
683679 final File profileFile = new File (profileFolder , getFilename (url ));
684- URL urlHttp = null ;
685- URL urlHttps = null ;
680+ URI urlHttp = null ;
681+ URI urlHttps = null ;
686682 if (url .toString ().toLowerCase ().startsWith ("https:" )) {
687683 urlHttps = url ;
688684 urlHttp = toComplementaryUrl (url );
@@ -719,7 +715,7 @@ public boolean cacheProfile(URL url) {
719715 }
720716 }
721717
722- private String getFilename (URL url ) {
718+ private String getFilename (URI url ) {
723719 // We use the path part and replace / with _
724720 return url .getPath ().replaceAll (Pattern .quote ("/" ), "_" );
725721 }
@@ -731,7 +727,7 @@ private String getFilename(URL url) {
731727 * @param url The url to get the update time from
732728 * @return the last update time, -1 if never updated (=not cached yet)
733729 */
734- protected long getLastUpdateTime (URL url ) {
730+ protected long getLastUpdateTime (URI url ) {
735731 blockUntilInitialized ();
736732 Long lastTime = lastUpdateTimes .get (url );
737733 return lastTime == null ? -1 : lastTime ;
0 commit comments