Skip to content

Commit 43bffc2

Browse files
committed
Merge pull request #4 from ben-gibson/bugfix-url-encoding
bugfix: fixed broken url encoding
2 parents 41f8eb2 + 1bd3d3f commit 43bffc2

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

src/uk/co/ben_gibson/repositorymapper/UrlFactory/GitHubUrlFactory.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import org.jetbrains.annotations.NotNull;
44
import uk.co.ben_gibson.repositorymapper.Context.Context;
55
import java.io.UnsupportedEncodingException;
6-
import java.net.MalformedURLException;
7-
import java.net.URL;
8-
import java.net.URLEncoder;
6+
import java.net.*;
97

108
/**
119
* Creates a URL in the format expected by the remote repository provider GitHub.
@@ -18,8 +16,7 @@ public class GitHubUrlFactory implements UrlFactory
1816
*/
1917
@Override
2018
@NotNull
21-
public URL getUrlFromContext(@NotNull Context context) throws MalformedURLException, UrlFactoryException
22-
{
19+
public URL getUrlFromContext(@NotNull Context context) throws MalformedURLException, UrlFactoryException, URISyntaxException {
2320

2421
String branch;
2522

@@ -29,12 +26,16 @@ public URL getUrlFromContext(@NotNull Context context) throws MalformedURLExcept
2926
throw new UrlFactoryException("Failed to encode path, unsupported encoding");
3027
}
3128

32-
String path = context.getRemoteHost().toString() + "/blob/" + branch + context.getPath();
29+
URL remoteHost = context.getRemoteHost();
30+
31+
String path = context.getRemoteHost().getPath() + "/blob/" + branch + context.getPath();
32+
33+
String fragment = null;
3334

3435
if (context.getCaretLinePosition() != null) {
35-
path += "#L" + context.getCaretLinePosition().toString();
36+
fragment = "L" + context.getCaretLinePosition().toString();
3637
}
3738

38-
return new URL(path);
39+
return new URI(remoteHost.getProtocol(), remoteHost.getHost(), path, fragment).toURL();
3940
}
4041
}

src/uk/co/ben_gibson/repositorymapper/UrlFactory/StashUrlFactory.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
import org.jetbrains.annotations.NotNull;
44
import uk.co.ben_gibson.repositorymapper.Context.Context;
5-
6-
import java.io.UnsupportedEncodingException;
7-
import java.net.MalformedURLException;
8-
import java.net.URL;
9-
import java.net.URLEncoder;
5+
import java.net.*;
106

117
/**
128
* Creates a URL in the format expected by the remote repository provider Stash.
@@ -19,7 +15,7 @@ public class StashUrlFactory implements UrlFactory
1915
*/
2016
@Override
2117
@NotNull
22-
public URL getUrlFromContext(@NotNull Context context) throws MalformedURLException, UrlFactoryException {
18+
public URL getUrlFromContext(@NotNull Context context) throws MalformedURLException, UrlFactoryException, URISyntaxException {
2319

2420
URL remoteHost = context.getRemoteHost();
2521

@@ -36,25 +32,21 @@ public URL getUrlFromContext(@NotNull Context context) throws MalformedURLExcept
3632
String project = parts[1];
3733
String repository = parts[2];
3834

39-
String fullPath = String.format(
35+
String path = String.format(
4036
"/projects/%s/repos/%s/browse%s",
4137
project,
4238
repository,
4339
context.getPath()
4440
);
4541

46-
try {
47-
fullPath += "?at=" + URLEncoder.encode("refs/heads/" + context.getBranch(), "UTF-8");
48-
} catch (UnsupportedEncodingException e) {
49-
throw new UrlFactoryException("Failed to encode path, unsupported encoding");
50-
}
42+
String query = "at=refs/heads/" + context.getBranch();
43+
44+
String fragment = null;
5145

5246
if (context.getCaretLinePosition() != null) {
53-
fullPath += "#" + context.getCaretLinePosition().toString();
47+
fragment = context.getCaretLinePosition().toString();
5448
}
5549

56-
fullPath = remoteHost.getProtocol() + "://" + remoteHost.getHost() + fullPath;
57-
58-
return new URL(fullPath);
50+
return new URI(remoteHost.getProtocol(), remoteHost.getHost(), path, query, fragment).toURL();
5951
}
6052
}

src/uk/co/ben_gibson/repositorymapper/UrlFactory/UrlFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.jetbrains.annotations.NotNull;
44
import uk.co.ben_gibson.repositorymapper.Context.Context;
55
import java.net.MalformedURLException;
6+
import java.net.URISyntaxException;
67
import java.net.URL;
78

89
/**
@@ -19,5 +20,5 @@ public interface UrlFactory
1920
* @return Url
2021
*/
2122
@NotNull
22-
URL getUrlFromContext(@NotNull Context context) throws MalformedURLException, UrlFactoryException;
23+
URL getUrlFromContext(@NotNull Context context) throws MalformedURLException, UrlFactoryException, URISyntaxException;
2324
}

0 commit comments

Comments
 (0)