-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from assimbly/62-include-oriflame-in-assimbly
Oriflame custom component
- Loading branch information
Showing
32 changed files
with
2,103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<artifactId>custom-components</artifactId> | ||
<groupId>org.assimbly</groupId> | ||
<version>4.0.4-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>oriflame</artifactId> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.release>11</maven.compiler.release> | ||
<maven.versions.rules>file:///${project.basedir}/../rules.xml</maven.versions.rules> | ||
</properties> | ||
|
||
<distributionManagement> | ||
<repository> | ||
<id>github</id> | ||
<name>Temporary Staging Repository</name> | ||
<url>https://maven.pkg.github.com/assimbly/custom-components</url> | ||
</repository> | ||
</distributionManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>smooksnoxml</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-toolchains-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>toolchain</goal> | ||
</goals> | ||
<configuration> | ||
<toolchains> | ||
<jdk> | ||
<version>11</version> | ||
</jdk> | ||
</toolchains> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
= Oriflame Component | ||
:doctitle: ORIFLAME | ||
:shortname: oriflame | ||
:artifactid: oriflame | ||
:description: Transforms a standardized csv into a soap message for Oriflame | ||
:since: 2.23.0 | ||
:supportlevel: Stable | ||
:component-header: Producer is supported | ||
//Manually maintained attributes | ||
|
||
*Since Camel {since}* | ||
|
||
*{component-header}* | ||
|
||
The components transforms a standardized csv into a soap message for Oriflame. | ||
|
||
Maven users will need to add the following dependency to their `pom.xml` | ||
for this component: | ||
|
||
[source,xml] | ||
------------------------------------------------------------ | ||
<dependency> | ||
<groupId>org.assimbly</groupId> | ||
<artifactId>oriflame</artifactId> | ||
<version>x.x.x</version> | ||
<!-- use the same version as your Assimbly version --> | ||
</dependency> | ||
------------------------------------------------------------ | ||
|
||
== URI format | ||
|
||
-------------------------------------------- | ||
oriflame:// | ||
-------------------------------------------- | ||
|
37 changes: 37 additions & 0 deletions
37
oriflame/src/main/java/org/assimbly/oriflame/OriflameComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.assimbly.oriflame; | ||
|
||
import org.apache.camel.Endpoint; | ||
import org.apache.camel.support.DefaultComponent; | ||
import org.assimbly.oriflame.details.EdiDetails; | ||
import org.assimbly.oriflame.details.EdiType; | ||
import org.milyn.Smooks; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class OriflameComponent extends DefaultComponent { | ||
|
||
@Override | ||
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { | ||
Smooks smooks = new Smooks(); | ||
List<OriflameLineReader> lineReaders = new ArrayList<>(); | ||
|
||
String type = parameters.get("ediType").toString(); | ||
EdiDetails details = EdiType.details(type); | ||
|
||
// manually handle parameters | ||
// the alternative would be having setters on the endpoint itself | ||
for(Map.Entry<String, String> entry : details.lineReaders.entrySet()) { | ||
lineReaders.add(OriflameLineReader.fromEncodedConfig(entry.getKey(), entry.getValue())); | ||
} | ||
|
||
// clear out parameters so that no setter inspection occurs | ||
parameters.clear(); | ||
|
||
OriflameConfigurator config = new OriflameConfigurator(lineReaders, details); | ||
smooks.setReaderConfig(config); | ||
|
||
return new OriflameEndpoint(uri, this, smooks); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
oriflame/src/main/java/org/assimbly/oriflame/OriflameConfigurator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.assimbly.oriflame; | ||
|
||
import org.assimbly.oriflame.details.EdiDetails; | ||
import org.milyn.GenericReaderConfigurator; | ||
import org.milyn.ReaderConfigurator; | ||
import org.milyn.cdr.Parameter; | ||
import org.milyn.cdr.SmooksResourceConfiguration; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class OriflameConfigurator implements ReaderConfigurator { | ||
|
||
public static final String PARAM_LINE_READERS = "LINE_READERS"; | ||
public static final String PARAM_EDI_TYPE = "EDI_TYPE"; | ||
|
||
private final List<OriflameLineReader> lineReaders; | ||
private final EdiDetails type; | ||
|
||
public OriflameConfigurator(List<OriflameLineReader> lineReaders, EdiDetails type) { | ||
this.lineReaders = lineReaders; | ||
this.type = type; | ||
} | ||
|
||
@Override | ||
public List<SmooksResourceConfiguration> toConfig() { | ||
List<SmooksResourceConfiguration> configurations = new GenericReaderConfigurator(OriflameReader.class).toConfig(); | ||
|
||
// using line readers means we have to skip the @ConfigParam annotation | ||
// as that only supports string parameters | ||
Parameter lineReadersParam = new Parameter(PARAM_LINE_READERS, lineReaders); | ||
Parameter ediTypeParam = new Parameter(PARAM_EDI_TYPE, type); | ||
|
||
// re-use smooks default configuration, hence the magic 0 | ||
Map<String, Object> defaultParams = configurations.get(0).getParameters(); | ||
|
||
// drop our custom param together with the default params | ||
defaultParams.put(PARAM_LINE_READERS, lineReadersParam); | ||
defaultParams.put(PARAM_EDI_TYPE, ediTypeParam); | ||
|
||
return configurations; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
oriflame/src/main/java/org/assimbly/oriflame/OriflameEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.assimbly.oriflame; | ||
|
||
import org.apache.camel.Consumer; | ||
import org.apache.camel.Processor; | ||
import org.apache.camel.Producer; | ||
import org.apache.camel.support.DefaultEndpoint; | ||
import org.milyn.Smooks; | ||
|
||
public class OriflameEndpoint extends DefaultEndpoint { | ||
|
||
private final Smooks smooks; | ||
|
||
public OriflameEndpoint(String uri, OriflameComponent oriflameComponent, Smooks smooks) { | ||
super(uri, oriflameComponent); | ||
this.smooks = smooks; | ||
} | ||
|
||
@Override | ||
public Producer createProducer() { | ||
return new OriflameProducer(this, smooks); | ||
} | ||
|
||
@Override | ||
public Consumer createConsumer(Processor processor) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean isSingleton() { | ||
return false; | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
oriflame/src/main/java/org/assimbly/oriflame/OriflameLineReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package org.assimbly.oriflame; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class OriflameLineReader { | ||
|
||
// inbound meta | ||
private static final String GROUP_PREFIX = "$group$"; | ||
|
||
private final String header; | ||
private final String fields; | ||
private final List<MappedField> mappedFields; | ||
private final String group; | ||
|
||
private OriflameLineReader(String header, String fields, List<MappedField> mappedFields, String group) { | ||
this.header = header; | ||
this.fields = fields; | ||
this.mappedFields = mappedFields; | ||
this.group = group; | ||
} | ||
|
||
public String getGroup() { | ||
return group; | ||
} | ||
|
||
protected boolean isGroup() { | ||
return group != null; | ||
} | ||
|
||
protected String getHeader() { | ||
return header; | ||
} | ||
|
||
public boolean supports(String line) { | ||
return line.startsWith(header); | ||
} | ||
|
||
public HashMap<String, String> parseLine(String line) { | ||
HashMap<String, String> entries = new HashMap<String, String>(); | ||
int pos = 0; | ||
|
||
for(MappedField mappedField: mappedFields) { | ||
int rightBound = pos + mappedField.nrCharacters > line.length() | ||
? line.length() | ||
: pos + mappedField.nrCharacters; | ||
|
||
String fieldValue = line.substring(pos, rightBound); | ||
entries.put(mappedField.fieldName, fieldValue.trim()); | ||
|
||
pos += mappedField.nrCharacters; | ||
} | ||
|
||
return entries; | ||
} | ||
|
||
public static OriflameLineReader fromEncodedConfig(String key, String fields) { | ||
if(key.startsWith(GROUP_PREFIX)) { | ||
String group = key.substring(GROUP_PREFIX.length()); | ||
return new OriflameLineReader(group, fields, parseFieldMappings(fields), group); | ||
} | ||
else | ||
return new OriflameLineReader(key, fields, parseFieldMappings(fields), null); | ||
} | ||
|
||
private static List<MappedField> parseFieldMappings(String mappings) { | ||
// match against fieldName[characterCount] | ||
Pattern pattern = Pattern.compile("(\\w+)\\[(\\d+)]"); | ||
Matcher matcher = pattern.matcher(mappings); | ||
|
||
List<MappedField> mappedFields = new LinkedList<MappedField>(); | ||
|
||
while (matcher.find()) { | ||
String fieldName = matcher.group(1); | ||
int fieldLength = Integer.parseInt(matcher.group(2)); | ||
|
||
mappedFields.add(new MappedField(fieldName, fieldLength)); | ||
} | ||
|
||
return mappedFields; | ||
} | ||
|
||
private static class MappedField { | ||
|
||
private final String fieldName; | ||
private final int nrCharacters; | ||
|
||
private MappedField(String fieldName, int nrCharacters) { | ||
this.fieldName = fieldName; | ||
this.nrCharacters = nrCharacters; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
oriflame/src/main/java/org/assimbly/oriflame/OriflameProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.assimbly.oriflame; | ||
|
||
import org.apache.camel.Endpoint; | ||
import org.apache.camel.Exchange; | ||
import org.apache.camel.support.DefaultProducer; | ||
import org.milyn.Smooks; | ||
|
||
import javax.xml.transform.stream.StreamResult; | ||
import javax.xml.transform.stream.StreamSource; | ||
import java.io.StringReader; | ||
import java.io.StringWriter; | ||
|
||
public class OriflameProducer extends DefaultProducer { | ||
|
||
private final Smooks smooks; | ||
|
||
public OriflameProducer(Endpoint endpoint, Smooks smooks) { | ||
super(endpoint); | ||
this.smooks = smooks; | ||
} | ||
|
||
@Override | ||
public void process(Exchange exchange) { | ||
StringReader ori = new StringReader(exchange.getIn().getBody(String.class)); | ||
StringWriter xml = new StringWriter(); | ||
|
||
smooks.filterSource(new StreamSource(ori), new StreamResult(xml)); | ||
|
||
exchange.getIn().setBody(xml.toString()); | ||
} | ||
} | ||
|
Oops, something went wrong.