Skip to content

Commit

Permalink
Merge pull request #8 from SBI-/configuration-enhancement
Browse files Browse the repository at this point in the history
Configuration enhancement
  • Loading branch information
SBI- authored Jul 22, 2019
2 parents 072ef1a + aa21d25 commit 1ab64dd
Show file tree
Hide file tree
Showing 25 changed files with 335 additions and 212 deletions.
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: Base
Bundle-SymbolicName: com.siemens.bt.jazz.services.base;singleton:=true
Bundle-Version: 3.0.3.qualifier
Bundle-Version: 4.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Export-Package: com.siemens.bt.jazz.services.base,
com.siemens.bt.jazz.services.base.rest,
Expand Down
2 changes: 1 addition & 1 deletion plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.siemens.bt.jazz.services.base</groupId>
<artifactId>com.siemens.bt.jazz.services.base.parent</artifactId>
<version>3.0.3-SNAPSHOT</version>
<version>4.0.0-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.ibm.team.repository.service.TeamRawService;
import com.siemens.bt.jazz.services.base.rest.RestAction;
import com.siemens.bt.jazz.services.base.rest.RestActionBuilder;
import com.siemens.bt.jazz.services.base.rest.parameters.RestRequest;
import com.siemens.bt.jazz.services.base.router.Router;
import com.siemens.bt.jazz.services.base.router.map.MapRouter;
import java.io.IOException;
Expand All @@ -22,11 +21,7 @@ public abstract class BaseService extends TeamRawService {

protected final Router router = new MapRouter();

/**
* Constructs a new ClearQuestService
*
* <p>This constructor is only called by the Jazz class loader.
*/
/** This constructor is only called by the Jazz class loader. */
public BaseService() {
super();
}
Expand Down Expand Up @@ -79,16 +74,16 @@ protected void performAction(String uri, HttpServletRequest request, HttpServlet
try {
RestActionBuilder builder = prepareRequest(uri, request, response);
RestAction action = builder.create();
action.prepare();
action.execute();
action.cleanUp();
} catch (IOException e) {
throw e;
} catch (Exception e) {
// catch everything and log. Makes sure that there is no checked exception from our service
// back
// to jazz, except for the expected IOException when the response isn't writable. We need to
// make
// sure that our plug-in conforms to the contract that no exceptions bubble out into the
// system.
// back to jazz, except for the expected IOException when the response isn't writable. We need
// to make sure that our plug-in conforms to the contract that no exceptions bubble out into
// the system.
super.getLog().error(e);
this.http500return(request, response, e);
}
Expand All @@ -97,8 +92,6 @@ protected void performAction(String uri, HttpServletRequest request, HttpServlet
protected final RestActionBuilder prepareRequest(
String uri, HttpServletRequest request, HttpServletResponse response) {
HttpMethod method = HttpMethod.fromString(request.getMethod());
@SuppressWarnings("unchecked")
RestRequest restRequest = new RestRequest(method, uri, request.getParameterMap());
return router.prepareAction(this, this.getLog(), request, response, restRequest);
return router.prepareAction(uri, this, this.getLog(), request, response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.siemens.bt.jazz.services.base.configuration;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;

public class Configuration {
// make sure every configurator is added exactly once
protected Collection<ServiceConfigurator> configurations = new HashSet<>();

public Configuration() {}

public Configuration(ServiceConfigurator... configurators) {
configurations.addAll(Arrays.asList(configurators));
}

public Configuration(Configuration... configurations) {
for (Configuration c : configurations) {
this.configurations.addAll(c.get());
}
}

public Collection<ServiceConfigurator> get() {
return configurations;
}

public void add(ServiceConfigurator configurator) {
configurations.add(configurator);
}

public void add(Collection<ServiceConfigurator> configurators) {
configurations.addAll(configurators);
}

public void merge(Configuration other) {
this.configurations.addAll(other.get());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.siemens.bt.jazz.services.base.configuration;

import com.ibm.team.repository.service.TeamRawService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;

public interface ServiceConfigurator {
void configure(
Log log,
HttpServletRequest request,
HttpServletResponse response,
TeamRawService parentService);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.siemens.bt.jazz.services.base.configuration.preset;

import com.ibm.team.repository.service.TeamRawService;
import com.siemens.bt.jazz.services.base.configuration.ServiceConfigurator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;

public class ContentConfigurator implements ServiceConfigurator {

private final String contentType;

public ContentConfigurator(String contentType) {
this.contentType = contentType;
}

@Override
public void configure(
Log log,
HttpServletRequest request,
HttpServletResponse response,
TeamRawService parentService) {
response.setContentType(contentType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.siemens.bt.jazz.services.base.configuration.preset;

import com.ibm.team.repository.service.TeamRawService;
import com.siemens.bt.jazz.services.base.configuration.ServiceConfigurator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;

public class EncodingConfigurator implements ServiceConfigurator {

private final String encoding;

public EncodingConfigurator(String encoding) {
this.encoding = encoding;
}

@Override
public void configure(
Log log,
HttpServletRequest request,
HttpServletResponse response,
TeamRawService parentService) {
response.setCharacterEncoding(encoding);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@
* @see <a href="https://en.wikipedia.org/wiki/Command_pattern">Wikipedia: Command Pattern</a>
*/
public interface RestAction {
/**
* Calls the preparation hook of the service. This step is called before execute and provides a
* hook where optional configurations can be applied to an action before executing it.
*/
void prepare();

/**
* Executes the implemented action.
*
* @throws Exception Any exception that occurs while executing a service action.
*/
void execute() throws Exception;

/** Optional post-execution hook for actions that are run after the execute phase. */
void cleanUp();
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.siemens.bt.jazz.services.base.rest;

import com.ibm.team.repository.service.TeamRawService;
import com.siemens.bt.jazz.services.base.configuration.Configuration;
import com.siemens.bt.jazz.services.base.rest.parameters.PathParameters;
import com.siemens.bt.jazz.services.base.rest.parameters.RestRequest;
import com.siemens.bt.jazz.services.base.rest.service.AbstractRestService;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -25,15 +25,29 @@ public class RestActionBuilder {

protected final Class<? extends AbstractRestService> serviceClass;
protected final String path;
protected String uri;
protected HttpServletRequest request;
protected HttpServletResponse response;
protected Configuration configuration;
protected Log log;
protected RestRequest restRequest;
protected TeamRawService parentService;

public RestActionBuilder(String path, Class<? extends AbstractRestService> serviceClass) {
public RestActionBuilder(
String path, Class<? extends AbstractRestService> serviceClass, Configuration configuration) {
this.path = path;
this.serviceClass = serviceClass;
this.configuration = configuration;
}

/**
* Sets the URI passed in from the calling servlet
*
* @param uri String representation of the URI resolving this service
* @return RestActionBuilder in construction
*/
public RestActionBuilder setUri(String uri) {
this.uri = uri;
return this;
}

/**
Expand Down Expand Up @@ -70,13 +84,13 @@ public RestActionBuilder setResponse(HttpServletResponse response) {
}

/**
* Set a RestRequest.
* Add a configuration wrapper to this RestAction
*
* @param restRequest Summary of REST call information
* @param configuration Zero or more configurators that are applied before this action is executed
* @return RestActionBuilder in construction
*/
public RestActionBuilder setRestRequest(RestRequest restRequest) {
this.restRequest = restRequest;
public RestActionBuilder setConfiguration(Configuration configuration) {
this.configuration = configuration;
return this;
}

Expand All @@ -103,19 +117,15 @@ public RestAction create()
InstantiationException {
Constructor<? extends AbstractRestService> constructor =
serviceClass.getConstructor(
String.class,
Log.class,
HttpServletRequest.class,
HttpServletResponse.class,
RestRequest.class,
Configuration.class,
TeamRawService.class,
PathParameters.class);

return constructor.newInstance(
log,
request,
response,
restRequest,
parentService,
new PathParameters(path, restRequest.toString()));
uri, log, request, response, configuration, parentService, new PathParameters(path, uri));
}
}
Loading

0 comments on commit 1ab64dd

Please sign in to comment.