encode invalid character in URI
authorKatja Luther <k.luther@bgbm.org>
Tue, 30 Jun 2020 12:57:18 +0000 (14:57 +0200)
committerKatja Luther <k.luther@bgbm.org>
Tue, 30 Jun 2020 12:59:11 +0000 (14:59 +0200)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/UriWithLabelElement.java

index 82dd738..4801c22 100644 (file)
@@ -11,6 +11,7 @@ package eu.etaxonomy.taxeditor.ui.element;
 
 import java.io.IOException;
 import java.net.URI;
+import java.net.URL;
 
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
@@ -20,6 +21,7 @@ import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 
+import eu.etaxonomy.cdm.common.UrlUtf8Coder;
 import eu.etaxonomy.taxeditor.l10n.Messages;
 import eu.etaxonomy.taxeditor.model.ImageResources;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
@@ -109,7 +111,24 @@ public class UriWithLabelElement extends AbstractUriWithExceptionLabelElement<UR
     protected URI getParsedText() throws Exception {
         String uriText = super.getText();
         if(uriText!=null){
-            return new URI(super.getText());
+            try{
+                return new URI(super.getText());
+            }catch(Exception e){
+                URL url = new URL(uriText);
+                String[] pathElements =  url.getPath().split("/");
+
+                for (String element: pathElements){
+                    String replacement = UrlUtf8Coder.encode(element);
+                    uriText = uriText.replace(element, replacement);
+                }
+                if (url.getQuery() != null){
+                    uriText = uriText.replace(url.getQuery(), UrlUtf8Coder.encode(url.getQuery()));
+                }
+                url = new URL(uriText);
+                return url.toURI();
+
+            }
+
         }
         return null;
     }