Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…rverless into master
  • Loading branch information
mmajis committed Jun 15, 2021
2 parents 9df4347 + 9f959d2 commit 6590e2d
Show file tree
Hide file tree
Showing 14 changed files with 742 additions and 299 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<plantuml.version>1.2021.7</plantuml.version>
</properties>

<dependencies>
Expand All @@ -33,7 +34,7 @@
<dependency>
<groupId>net.sourceforge.plantuml</groupId>
<artifactId>plantuml</artifactId>
<version>1.2021.7</version>
<version>${plantuml.version}</version>
</dependency>
<!-- Math dependencies -->
<dependency>
Expand Down
62 changes: 55 additions & 7 deletions sam-deploy.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,71 @@
#!/bin/bash

# Note! This needs an ECR repo created like this:
# aws ecr create-repository --repository-name plantuml-sam \
# --image-tag-mutability IMMUTABLE --image-scanning-configuration scanOnPush=true
#
# After repo creation, edit the script to replace the repo URL in the commands below

set -eo pipefail
readonly basedir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
usage() {
cat >&2 <<EOUSAGE
$(basename "${BASH_SOURCE[0]}") -b <s3bucket> [ -s <stackName> ] [ -p <awsProfile> ] [ -p <awsRegion> ]
Options
-b | --bucket (Required) The S3 bucket to upload artifacts to.
-s | --stack-name The name of the CloudFormation stack to create.
(default: sam-plantuml)
-p | --profile Specify an AWS profile
-r | --region Specify an AWS region
-h | --help Print this usage message and exit
EOUSAGE
}
awsProfile=()
awsRegion=()
s3bucket=""
stackName="sam-plantuml"
while [[ $# -gt 0 ]]; do
opt="$1"
shift
case "$opt" in
-h|--help) usage; exit;;
-p|--profile) awsProfile=(--profile "$1"); shift;;
-r|--region) awsRegion=(--region "$1"); shift;;
-b|--bucket) s3bucket="$1"; shift;;
-s|--stack-name) stackName="$1"; shift;;
*) echo "Unknown option $opt"; usage; exit 1;;
esac
done

if [[ "$s3bucket" == "" ]]; then
echo "please provide an S3 bucket name with -b" >&2
exit 1
fi

if [[ "$stackName" == "" ]]; then
echo "please provide a name for the CFN stack with -s" >&2
exit 1
fi

pushd "${basedir}" 2>/dev/null
codeUri="$(ls target/plantuml-serverless-*.jar | head -n 1)"
if [[ ! -f "$codeUri" ]]; then
echo "missing plantuml-serverless-*.jar. please run 'mvn clean package'." >&2
exit 1
fi

sam build --cached

# Edit the AWS account id and region in the repo URL here
aws ecr get-login-password | docker login --username AWS \
--password-stdin 293246570391.dkr.ecr.eu-west-1.amazonaws.com

# Replace image-repository and s3-bucket values with your own
sam deploy \
--stack-name plantuml-sam \
# Replace image-repository with your own
sam deploy "${awsProfile[@]}" "${awsRegion[@]}" \
--stack-name "${stackName}" \
--capabilities CAPABILITY_IAM \
--image-repository 293246570391.dkr.ecr.eu-west-1.amazonaws.com/plantuml-sam \
--s3-bucket plantuml-serverlessrepo
--s3-bucket "${s3bucket}"

aws s3 cp LICENSE s3://plantuml-serverlessrepo/LICENSE
aws s3 cp serverlessrepo/README.md s3://plantuml-serverlessrepo/README.md
#These would be for serverless application repository, but it doesn't support container based lambdas at the moment.
#aws s3 cp LICENSE s3://"${s3bucket}"/LICENSE
#aws s3 cp serverlessrepo/README.md s3://"${s3bucket}"/README.md
11 changes: 11 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ functions:
name: lambdacontainer
command:
- com.nitor.plantuml.lambda.UIHandler::handleRequest
uml:
events:
- http:
path: uml/{encodedUml}
method: get
integration: lambda-proxy
cors: true
image:
name: lambdacontainer
command:
- com.nitor.plantuml.lambda.UmlHandler::handleRequest
png:
events:
- http:
Expand Down
57 changes: 39 additions & 18 deletions src/main/java/com/nitor/plantuml/PlantUmlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,59 @@
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.LineLocation;
import net.sourceforge.plantuml.SourceStringReader;
import net.sourceforge.plantuml.code.AsciiEncoder;
import net.sourceforge.plantuml.servlet.utility.UmlExtractor;
import net.sourceforge.plantuml.syntax.SyntaxChecker;
import net.sourceforge.plantuml.syntax.SyntaxResult;
import net.sourceforge.plantuml.BlockUml;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.core.Diagram;
import net.sourceforge.plantuml.error.PSystemError;
import net.sourceforge.plantuml.version.Version;
import org.apache.log4j.Logger;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.regex.Pattern;

public class PlantUmlUtil {

private static final Logger logger = Logger.getLogger(PlantUmlUtil.class);
public static final String DIAGRAM_TYPE_UNKNOWN = "UNKNOWN";
public static final String NOETAG = "NOETAG";

public ByteArrayOutputStream renderDiagram(String encodedUml, DiagramType diagramType) throws IOException {
public SourceStringReader readDiagram(String encodedUml) {
String uml = decodeUml(encodedUml);
SourceStringReader reader = new SourceStringReader(uml);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
final BlockUml blockUml = reader.getBlocks().get(0);
final Diagram diagram = blockUml.getDiagram();
if (diagram instanceof PSystemError) {
return null;
return new SourceStringReader(uml);
}

public String getEtag(String encodedUml, String typeIdentifier) {
String uml = decodeUml(encodedUml);
if (Pattern.compile("!include(_many|_once)?\\s*https?://").matcher(uml).find()) {
return NOETAG;
}
diagram.exportDiagram(baos, 0, new FileFormatOption(DiagramTypeUtil.asFileFormat(diagramType), true));
String baseEtag = typeIdentifier + Version.etag();
try {
final AsciiEncoder coder = new AsciiEncoder();
final MessageDigest msgDigest = MessageDigest.getInstance("MD5");
msgDigest.update(uml.getBytes(StandardCharsets.UTF_8));
final byte[] digest = msgDigest.digest();
return baseEtag + coder.encode(digest);
} catch (Exception e) {
logger.error("failed to digest uml", e);
return NOETAG;
}
}

public ByteArrayOutputStream renderDiagram(SourceStringReader reader, DiagramType diagramType) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
reader.outputImage(baos, new FileFormatOption(DiagramTypeUtil.asFileFormat(diagramType), true));
return baos;
}

public String renderImageMap(String encodedUml) throws IOException {
String uml = decodeUml(encodedUml);
SourceStringReader reader = new SourceStringReader(uml);
public String renderImageMap(SourceStringReader reader) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
return reader.generateImage(baos, new FileFormatOption(FileFormat.PNG, true));
return reader.outputImage(baos, new FileFormatOption(FileFormat.PNG, true)).getDescription();
}

public SyntaxCheckResult checkSyntax(String encodedUml) throws IOException {
Expand All @@ -68,9 +83,15 @@ public SyntaxCheckResult checkSyntax(String encodedUml) throws IOException {
return result;
}

private String decodeUml(String encodedUml) {
public String decodeUml(String encodedUml) {
logger.debug(String.format("Got encoded uml: %s", encodedUml));
try {
if (encodedUml.length() > 10) {
int period = encodedUml.indexOf('.');
if (period > encodedUml.length() - 10) {
encodedUml = encodedUml.substring(0, period);
}
}
String decodedUml = UmlExtractor.getUmlSource(encodedUml);
logger.debug(String.format("Decoded uml: %s", decodedUml));
return decodedUml;
Expand Down
Loading

0 comments on commit 6590e2d

Please sign in to comment.