Skip to content

Commit

Permalink
Merge pull request #8 from SBI-/master
Browse files Browse the repository at this point in the history
Minor url routing improvements
  • Loading branch information
SBI- committed Jul 3, 2018
2 parents cb7ab7a + 7c157a6 commit fac0d74
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 86 deletions.
2 changes: 1 addition & 1 deletion feature/feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<feature
id="org.jazzcommunity.GitConnectorService.feature"
label="REST Service"
version="1.5.1.qualifier"
version="1.5.3.qualifier"
provider-name="PROVIDER">

<description url="http://www.example.com/description">
Expand Down
2 changes: 1 addition & 1 deletion feature/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.jazzcommunity.GitConnectorService</groupId>
<artifactId>org.jazzcommunity.GitConnectorService.parent</artifactId>
<version>1.5.1-SNAPSHOT</version>
<version>1.5.3-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion plugin/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: org.jazzcommunity.GitConnectorService
Bundle-SymbolicName: org.jazzcommunity.GitConnectorService;singleton:=true
Bundle-Version: 1.5.1.qualifier
Bundle-Version: 1.5.3.qualifier
Bundle-Vendor: VENDOR
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Require-Bundle:
Expand Down
2 changes: 1 addition & 1 deletion plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.jazzcommunity.GitConnectorService</groupId>
<artifactId>org.jazzcommunity.GitConnectorService.parent</artifactId>
<version>1.5.1-SNAPSHOT</version>
<version>1.5.3-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import com.siemens.bt.jazz.services.base.rest.RestAction;
import com.siemens.bt.jazz.services.base.rest.RestRequest;
import org.jazzcommunity.GitConnectorService.base.rest.RestActionBuilder;
import org.jazzcommunity.GitConnectorService.base.router.factory.RestFactory;
import org.jazzcommunity.GitConnectorService.base.router.CustomRouter;
import org.jazzcommunity.GitConnectorService.builder.VersionService;
import org.jazzcommunity.GitConnectorService.builder.gitlab.IssueLinkService;
import org.jazzcommunity.GitConnectorService.builder.gitlab.IssuePreviewService;
import org.jazzcommunity.GitConnectorService.builder.gitlab.RequestLinkService;
import org.jazzcommunity.GitConnectorService.builder.gitlab.RequestPreviewService;
import org.jazzcommunity.GitConnectorService.base.router.CustomRouter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -33,23 +32,23 @@ public class GitConnectorService extends TeamRawService implements IGitConnector
*/
public GitConnectorService() {
super();
router.get(new RestFactory(
"gitlab/{host}/project/{projectId}/issue/{issueId}/link.*",
IssueLinkService.class));
router.get(new RestFactory(
"gitlab/{host}/project/{projectId}/issue/{issueId}/preview.*",
IssuePreviewService.class));
router.get(
"gitlab/{host}/project/{projectId}/issue/{issueId}/link",
IssueLinkService.class);
router.get(
"gitlab/{host}/project/{projectId}/issue/{issueId}/preview",
IssuePreviewService.class);

router.get(new RestFactory(
"gitlab/{host}/project/{projectId}/merge-request/{mergeRequestId}/link.*",
RequestLinkService.class));
router.get(new RestFactory(
"gitlab/{host}/project/{projectId}/merge-request/{mergeRequestId}/preview.*",
RequestPreviewService.class));
router.get(
"gitlab/{host}/project/{projectId}/merge-request/{mergeRequestId}/link",
RequestLinkService.class);
router.get(
"gitlab/{host}/project/{projectId}/merge-request/{mergeRequestId}/preview",
RequestPreviewService.class);

router.get(new RestFactory(
router.get(
"info/version",
VersionService.class));
VersionService.class);

/**
* This code is purposely commented out and not deleted!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.jazzcommunity.GitConnectorService.base.rest;

import com.google.common.base.Joiner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
Expand All @@ -12,18 +9,7 @@ public class PathParameters {
private final Map<String, String> parameters;

public PathParameters(String path, String url) {
// first, I want the names...
// This I can probably do just once..., maybe when
// creating the service. But I'm not going to optimize
// this until later.
ArrayList<String> names = getNames(path);
ArrayList<String> values = getValues(path, url);

if (names.size() != values.size()) {
throw new RuntimeException("Unable to match url parameters to values");
}

this.parameters = makeMap(names, values);
this.parameters = makeMap(path, url);
}

// getters should do checking if stuff exists etc.
Expand All @@ -35,54 +21,36 @@ public Integer getAsInteger(String key) {
return Integer.parseInt(get(key));
}

private Map<String, String> makeMap(ArrayList<String> names, ArrayList<String> values) {
// zip operation
private static Map<String, String> makeMap(String path, String url) {
HashMap<String, String> parameters = new HashMap<>();

for (int i = 0; i < names.size(); i += 1) {
parameters.put(names.get(i), values.get(i));
}

return parameters;
}

// It's probably possible to consolidate these two functions, maybe even into a single
// regex call.
// Idea: Match the all-match regex to both path and url, which should give the name
// in one match and the value in the other. Then the zip could be done right away as well.
// this is what the matching regex should look like: \{{0,1}([^\/}]+)\}{0,1} for everything
// in {} and not. The rest of the path should then match accordingly.
// Regex 101: example
// gitlab\/\{{0,1}([^\/}]+)\}{0,1}\/project\/\{{0,1}([^\/}]+)\}{0,1}\/issue\/\{{0,1}([^\/}]+)\}{0,1}\/link.*
// will match the path and the actual url as well.
// test data:
// gitlab/{host}/project/{projectId}/issue/{issueId}/link.*
// gitlab/code.siemens.com/project/1234/issue/12/link.*
private static ArrayList<String> getValues(String path, String url) {
String regex = path.replaceAll("\\{[^\\/]+\\}", "([^\\\\/]+)");
String regex = path.replaceAll(
"\\{([^\\/}]+)\\}",
"([^\\\\/]+)");
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(url);

ArrayList<String> values = new ArrayList<>();
matcher.find();

for (int i = 1; i <= matcher.groupCount(); i += 1) {
values.add(matcher.group(i));
Matcher names = pattern.matcher(path);
Matcher values = pattern.matcher(url);

if (names.groupCount() != values.groupCount()) {
/*
* This should never happen if routing is actually successful. If we reach this
* exception, something is fundamentally wrong and we have overlooked a core
* url mapping concept.
*/
throw new RuntimeException("Unable to match url parameters to values");
}

return values;
}

private static ArrayList<String> getNames(String path) {
String regex = "\\{([^\\/]+)\\}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(path);

ArrayList<String> names = new ArrayList<>();
while (matcher.find()) {
names.add(matcher.group(1));
while (names.find() && values.find()) {
/*
* Skip the leading group because it is always the entire match.
* For a visualization how this regex works for creating the parameter map,
* refer to the provided example: https://regex101.com/r/17Ul3V/1
*/
for (int i = 1; i <= names.groupCount(); i += 1) {
String name = names.group(i).substring(1, names.group(i).length() - 1);
parameters.put(name, values.group(i));
}
}

return names;
return parameters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class RestActionBuilder {
protected final String path;
protected HttpServletRequest request;
protected HttpServletResponse response;
// not sure about which log to use here...
protected Log log;
protected RestRequest restRequest;
protected TeamRawService parentService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.ibm.team.repository.service.TeamRawService;
import com.siemens.bt.jazz.services.base.rest.RestRequest;
import org.apache.commons.logging.Log;
import org.jazzcommunity.GitConnectorService.base.rest.AbstractRestService;
import org.jazzcommunity.GitConnectorService.base.rest.RestActionBuilder;
import org.jazzcommunity.GitConnectorService.base.router.factory.RestFactory;
import org.jazzcommunity.GitConnectorService.base.router.factory.ServiceFactory;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -18,7 +20,29 @@ public void addService(
HttpConstants.HttpMethod method,
ServiceFactory serviceFactory) {

map.add(method, serviceFactory.getPath(), serviceFactory);
map.add(method, serviceFactory.getPath() + ".*", serviceFactory);
}

// It might actually be nice to inject the rest factory type / a factory for
// rest services into the constructor, which is then used for all mappings.
@Override
public void get(String path, Class<? extends AbstractRestService> service) {
get(new RestFactory(path, service));
}

@Override
public void put(String path, Class<? extends AbstractRestService> service) {
put(new RestFactory(path, service));
}

@Override
public void post(String path, Class<? extends AbstractRestService> service) {
post(new RestFactory(path, service));
}

@Override
public void delete(String path, Class<? extends AbstractRestService> service) {
delete(new RestFactory(path, service));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.ibm.team.repository.service.TeamRawService;
import com.siemens.bt.jazz.services.base.rest.RestRequest;
import org.apache.commons.logging.Log;
import org.jazzcommunity.GitConnectorService.base.rest.AbstractRestService;
import org.jazzcommunity.GitConnectorService.base.rest.RestActionBuilder;
import org.jazzcommunity.GitConnectorService.base.router.factory.ServiceFactory;

Expand All @@ -12,6 +13,12 @@

public interface Router {
void addService(HttpMethod method, ServiceFactory factory);

void get(String path, Class<? extends AbstractRestService> service);
void put(String path, Class<? extends AbstractRestService> service);
void post(String path, Class<? extends AbstractRestService> service);
void delete(String path, Class<? extends AbstractRestService> service);

void get(ServiceFactory factory);
void put(ServiceFactory factory);
void post(ServiceFactory factory);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.jazzcommunity.GitConnectorService</groupId>
<artifactId>org.jazzcommunity.GitConnectorService.parent</artifactId>
<version>1.5.1-SNAPSHOT</version>
<version>1.5.3-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
Expand Down
4 changes: 2 additions & 2 deletions test/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: org.jazzcommunity.GitConnectorService
Bundle-SymbolicName: org.jazzcommunity.GitConnectorService.test;singleton:=true
Bundle-Version: 1.5.1.qualifier
Bundle-Version: 1.5.3.qualifier
Bundle-Vendor: VENDOR
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ClassPath: target/dependency/junit-4.12.jar,
target/dependency/hamcrest-core-1.3.jar,
target/dependency/gson-2.8.2.jar,
target/dependency/com.siemens.bt.jazz.services.base-2.0.1-SNAPSHOT.jar,
target/dependency/com.siemens.bt.jazz.services.PersonalTokenService-1.0.2-SNAPSHOT.jar,
target/dependency/org.jazzcommunity.GitConnectorService-1.5.1-SNAPSHOT.jar,
target/dependency/org.jazzcommunity.GitConnectorService-1.5.3-SNAPSHOT.jar,
target/dependency/asm-5.0.3.jar,
target/dependency/asm-analysis-5.0.3.jar,
target/dependency/asm-tree-5.0.3.jar,
Expand Down
4 changes: 2 additions & 2 deletions test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.jazzcommunity.GitConnectorService</groupId>
<artifactId>org.jazzcommunity.GitConnectorService.parent</artifactId>
<version>1.5.1-SNAPSHOT</version>
<version>1.5.3-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>

Expand All @@ -21,7 +21,7 @@
<dependency>
<groupId>org.jazzcommunity.GitConnectorService</groupId>
<artifactId>org.jazzcommunity.GitConnectorService</artifactId>
<version>1.5.1-SNAPSHOT</version>
<version>1.5.3-SNAPSHOT</version>
</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion update-site/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.jazzcommunity.GitConnectorService</groupId>
<artifactId>org.jazzcommunity.GitConnectorService.parent</artifactId>
<version>1.5.1-SNAPSHOT</version>
<version>1.5.3-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>

Expand Down

0 comments on commit fac0d74

Please sign in to comment.