Skip to content

Commit

Permalink
Use StandardCharsets.UTF_8 instead of String based UTF-8 for getting …
Browse files Browse the repository at this point in the history
…String bytes
  • Loading branch information
filiphr committed Nov 30, 2023
1 parent cd50dac commit eecd492
Show file tree
Hide file tree
Showing 19 changed files with 39 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import static org.flowable.common.rest.api.PaginateListUtil.paginateList;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -242,11 +242,7 @@ public Map<String, String> splitQueryString(String queryString) {

protected String decode(String string) {
if (string != null) {
try {
return URLDecoder.decode(string, "UTF-8");
} catch (UnsupportedEncodingException uee) {
throw new IllegalStateException("JVM does not support UTF-8 encoding.", uee);
}
return URLDecoder.decode(string, StandardCharsets.UTF_8);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
Expand Down Expand Up @@ -301,11 +300,7 @@ protected void closeHttpConnections() {

protected String encode(String string) {
if (string != null) {
try {
return URLEncoder.encode(string, "UTF-8");
} catch (UnsupportedEncodingException uee) {
throw new IllegalStateException("JVM does not support UTF-8 encoding.", uee);
}
return URLEncoder.encode(string, StandardCharsets.UTF_8);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

Expand All @@ -31,8 +31,6 @@

public class AppDeploymentBuilderImpl implements AppDeploymentBuilder {

protected static final String DEFAULT_ENCODING = "UTF-8";

protected transient AppRepositoryServiceImpl repositoryService;
protected transient AppResourceEntityManager resourceEntityManager;

Expand Down Expand Up @@ -93,11 +91,7 @@ public AppDeploymentBuilder addString(String resourceName, String text) {

AppResourceEntity resource = resourceEntityManager.create();
resource.setName(resourceName);
try {
resource.setBytes(text.getBytes(DEFAULT_ENCODING));
} catch (UnsupportedEncodingException e) {
throw new FlowableException("Unable to get bytes.", e);
}
resource.setBytes(text.getBytes(StandardCharsets.UTF_8));
deployment.addResource(resource);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

Expand All @@ -32,8 +32,6 @@

public class CmmnDeploymentBuilderImpl implements CmmnDeploymentBuilder {

protected static final String DEFAULT_ENCODING = "UTF-8";

protected transient CmmnRepositoryServiceImpl repositoryService;
protected transient CmmnResourceEntityManager resourceEntityManager;

Expand Down Expand Up @@ -93,11 +91,7 @@ public CmmnDeploymentBuilder addString(String resourceName, String text) {

CmmnResourceEntity resource = resourceEntityManager.create();
resource.setName(resourceName);
try {
resource.setBytes(text.getBytes(DEFAULT_ENCODING));
} catch (UnsupportedEncodingException e) {
throw new FlowableException("Unable to get bytes.", e);
}
resource.setBytes(text.getBytes(StandardCharsets.UTF_8));
deployment.addResource(resource);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import static org.flowable.common.rest.api.PaginateListUtil.paginateList;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -257,11 +257,7 @@ public Map<String, String> splitQueryString(String queryString) {

protected String decode(String string) {
if (string != null) {
try {
return URLDecoder.decode(string, "UTF-8");
} catch (UnsupportedEncodingException uee) {
throw new IllegalStateException("JVM does not support UTF-8 encoding.", uee);
}
return URLDecoder.decode(string, StandardCharsets.UTF_8);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
Expand Down Expand Up @@ -337,11 +336,7 @@ protected void closeHttpConnections() {

protected String encode(String string) {
if (string != null) {
try {
return URLEncoder.encode(string, "UTF-8");
} catch (UnsupportedEncodingException uee) {
throw new IllegalStateException("JVM does not support UTF-8 encoding.", uee);
}
return URLEncoder.encode(string, StandardCharsets.UTF_8);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;
Expand All @@ -37,7 +36,6 @@
public class DmnDeploymentBuilderImpl implements DmnDeploymentBuilder, Serializable {

private static final long serialVersionUID = 1L;
protected static final String DEFAULT_ENCODING = "UTF-8";

protected transient DmnRepositoryServiceImpl repositoryService;
protected transient DmnResourceEntityManager resourceEntityManager;
Expand Down Expand Up @@ -98,11 +96,7 @@ public DmnDeploymentBuilder addString(String resourceName, String text) {

DmnResourceEntity resource = resourceEntityManager.create();
resource.setName(resourceName);
try {
resource.setBytes(text.getBytes(DEFAULT_ENCODING));
} catch (UnsupportedEncodingException e) {
throw new FlowableException("Unable to get decision table bytes.", e);
}
resource.setBytes(text.getBytes(StandardCharsets.UTF_8));
deployment.addResource(resource);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -223,11 +222,7 @@ protected void closeHttpConnections() {

protected String encode(String string) {
if (string != null) {
try {
return URLEncoder.encode(string, "UTF-8");
} catch (UnsupportedEncodingException uee) {
throw new IllegalStateException("JVM does not support UTF-8 encoding.", uee);
}
return URLEncoder.encode(string, StandardCharsets.UTF_8);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,34 @@

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.flowable.common.engine.api.FlowableException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
* @author Tom Baeyens
*/
public class StringStreamSource implements StreamSource {

String string;
String byteArrayEncoding = "utf-8";
protected final String string;
protected final Charset byteArrayEncoding;

public StringStreamSource(String string) {
this.string = string;
this(string, StandardCharsets.UTF_8);
}

public StringStreamSource(String string, String byteArrayEncoding) {
this.string = string;
this.byteArrayEncoding = Charset.forName(byteArrayEncoding);
}

public StringStreamSource(String string, Charset byteArrayEncoding) {
this.string = string;
this.byteArrayEncoding = byteArrayEncoding;
}

@Override
public InputStream getInputStream() {
try {
return new ByteArrayInputStream(byteArrayEncoding == null ? string.getBytes() : string.getBytes(byteArrayEncoding));
} catch (UnsupportedEncodingException e) {
throw new FlowableException("Unsupported encoding for string", e);
}
return new ByteArrayInputStream(byteArrayEncoding == null ? string.getBytes() : string.getBytes(byteArrayEncoding));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -44,7 +43,6 @@
public class DeploymentBuilderImpl implements DeploymentBuilder, Serializable {

private static final long serialVersionUID = 1L;
protected static final String DEFAULT_ENCODING = "UTF-8";

protected transient RepositoryServiceImpl repositoryService;
protected transient ResourceEntityManager resourceEntityManager;
Expand Down Expand Up @@ -96,11 +94,7 @@ public DeploymentBuilder addString(String resourceName, String text) {
}
ResourceEntity resource = resourceEntityManager.create();
resource.setName(resourceName);
try {
resource.setBytes(text.getBytes(DEFAULT_ENCODING));
} catch (UnsupportedEncodingException e) {
throw new FlowableException("Unable to get process bytes.", e);
}
resource.setBytes(text.getBytes(StandardCharsets.UTF_8));
deployment.addResource(resource);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -72,7 +71,7 @@ public void tearDown() {
}

@Test
public void testCreateAndQuery() throws UnsupportedEncodingException {
public void testCreateAndQuery() {
DeploymentEntity entity = assembleUnpersistedDeploymentEntity();

ParsedDeploymentBuilderFactory builderFactory = processEngineConfiguration.getParsedDeploymentBuilderFactory();
Expand Down Expand Up @@ -111,14 +110,14 @@ private ProcessDefinitionEntity getProcessDefinitionEntityFromList(List<ProcessD
return null;
}

private DeploymentEntity assembleUnpersistedDeploymentEntity() throws UnsupportedEncodingException {
private DeploymentEntity assembleUnpersistedDeploymentEntity() {
DeploymentEntity entity = new DeploymentEntityImpl();
entity.addResource(buildResource(IDR_XML_NAME, IDR_PROCESS_XML));
entity.addResource(buildResource(EN_XML_NAME, EN_PROCESS_XML));
return entity;
}

private ResourceEntity buildResource(String name, String text) throws UnsupportedEncodingException {
private ResourceEntity buildResource(String name, String text) {
ResourceEntityImpl result = new ResourceEntityImpl();
result.setName(name);
result.setBytes(text.getBytes(StandardCharsets.UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -260,7 +259,7 @@ public void verifyValidation() throws Exception {
}

@Test
public void testWarningError() throws UnsupportedEncodingException, XMLStreamException {
public void testWarningError() throws XMLStreamException {
String flowWithoutConditionNoDefaultFlow = "<?xml version='1.0' encoding='UTF-8'?>"
+ "<definitions id='definitions' xmlns='http://www.omg.org/spec/BPMN/20100524/MODEL' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:activiti='http://activiti.org/bpmn' targetNamespace='Examples'>"
+ " <process id='exclusiveGwDefaultSequenceFlow'> " + " <startEvent id='theStart' /> "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import static org.flowable.common.rest.api.PaginateListUtil.paginateList;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -246,11 +246,7 @@ public Map<String, String> splitQueryString(String queryString) {

protected String decode(String string) {
if (string != null) {
try {
return URLDecoder.decode(string, "UTF-8");
} catch (UnsupportedEncodingException uee) {
throw new IllegalStateException("JVM does not support UTF-8 encoding.", uee);
}
return URLDecoder.decode(string, StandardCharsets.UTF_8);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
Expand Down Expand Up @@ -343,11 +342,7 @@ protected void closeHttpConnections() {

protected String encode(String string) {
if (string != null) {
try {
return URLEncoder.encode(string, "UTF-8");
} catch (UnsupportedEncodingException uee) {
throw new IllegalStateException("JVM does not support UTF-8 encoding.", uee);
}
return URLEncoder.encode(string, StandardCharsets.UTF_8);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;
import org.flowable.common.engine.api.FlowableException;
Expand All @@ -34,7 +34,6 @@
public class EventDeploymentBuilderImpl implements EventDeploymentBuilder, Serializable {

private static final long serialVersionUID = 1L;
protected static final String DEFAULT_ENCODING = "UTF-8";

protected transient EventRepositoryServiceImpl repositoryService;
protected transient EventResourceEntityManager resourceEntityManager;
Expand Down Expand Up @@ -94,11 +93,7 @@ public EventDeploymentBuilder addString(String resourceName, String text) {

EventResourceEntity resource = resourceEntityManager.create();
resource.setName(resourceName);
try {
resource.setBytes(text.getBytes(DEFAULT_ENCODING));
} catch (UnsupportedEncodingException e) {
throw new FlowableException("Unable to get event definition bytes.", e);
}
resource.setBytes(text.getBytes(StandardCharsets.UTF_8));
deployment.addResource(resource);
return this;
}
Expand Down
Loading

0 comments on commit eecd492

Please sign in to comment.