Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Check for bad charset names and log/throw appropriately
Browse files Browse the repository at this point in the history
  • Loading branch information
barns096 authored and barns096 committed Oct 16, 2016
1 parent f531314 commit 6af205e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/googlecode/t7mp/AbstractT7BaseMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public abstract class AbstractT7BaseMojo extends AbstractMojo {

public static final String CONTEXT_PATH_ROOT = "ROOT";

public static final String DEFAULT_URI_ENCODING = "ISO-8859-1";

/**
* @parameter expression="${project}"
* @required
Expand Down Expand Up @@ -171,7 +173,7 @@ public abstract class AbstractT7BaseMojo extends AbstractMojo {
* @parameter expression="${t7.tomcatUriEncoding}" default-value="ISO-8859-1"
*
*/
protected String tomcatUriEncoding = "ISO-8859-1";
protected String tomcatUriEncoding = DEFAULT_URI_ENCODING;

/**
*
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/googlecode/t7mp/BaseConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class BaseConfiguration implements T7Configuration {
public static final int DEFAULT_TOMCAT_HTTP_PORT = 8080;
public static final int DEFAULT_TOMCAT_SHUTDOWN_PORT = 8005;
public static final String DEFAULT_CONTEXT_PATH_ROOT = "ROOT";
public static final String DEFAULT_URI_ENCODING = "ISO-8859-1";
protected TomcatArtifact tomcatArtifact = new TomcatArtifact();

/**
Expand Down Expand Up @@ -79,7 +80,7 @@ public class BaseConfiguration implements T7Configuration {
* @parameter expression="${t7.tomcatUriEncoding}" default-value="ISO-8859-1"
*
*/
protected String tomcatUriEncoding = "ISO-8859-1";
protected String tomcatUriEncoding = DEFAULT_URI_ENCODING;

/**
* @parameter default-value="${project.build.directory}/tomcat"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
Expand Down Expand Up @@ -46,7 +49,19 @@ public void execute(Context context) {
velocityContext.put("tomcatShutdownPort", configuration.getTomcatShutdownPort() + "");
velocityContext.put("tomcatShutdownCommand", configuration.getTomcatShutdownCommand());
velocityContext.put("tomcatHostName", configuration.getTomcatHostName());
velocityContext.put("tomcatUriEncoding", configuration.getTomcatUriEncoding());

// check if URI encoding is valid. If not, throw a descriptive setup exception
try {
Charset.forName(configuration.getTomcatUriEncoding());
velocityContext.put("tomcatUriEncoding", configuration.getTomcatUriEncoding());
} catch (UnsupportedCharsetException uce) {
throw new TomcatSetupException("Unsupported URI encoding: " + configuration.getTomcatUriEncoding(), uce);
} catch (IllegalCharsetNameException icne) {
throw new TomcatSetupException("Illegal URI encoding: " + configuration.getTomcatUriEncoding(), icne);
} catch (IllegalArgumentException iae) {
throw new TomcatSetupException("Null URI encoding not allowed", iae);
}

Writer writer = new FileWriter(new File(configuration.getCatalinaBase(), "/conf/catalina.properties"));
template.merge(velocityContext, writer);
writer.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void testConfigFilesSequence() throws IOException {
Mockito.when(configuration.getCatalinaBase()).thenReturn(catalinaBaseDir);
// Mockito.when(mojo.getLog()).thenReturn(log);
Mockito.when(configuration.getTomcatShutdownCommand()).thenReturn("SHUTDOWN");
Mockito.when(configuration.getTomcatUriEncoding()).thenReturn(BaseConfiguration.DEFAULT_URI_ENCODING);
Context context = new DefaultContext(new ChainedArtifactResolver(),configuration);
ConfigFilesSequence sequence = new ConfigFilesSequence();
sequence.execute(context);
Expand Down

0 comments on commit 6af205e

Please sign in to comment.