Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge bo-dev Into dev #3

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 60 additions & 0 deletions rest/java/.gitignore
@@ -0,0 +1,60 @@
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# PyDev specific (Python IDE for Eclipse)
*.pydevproject

# CDT-specific (C/C++ Development Tooling)
.cproject

# CDT- autotools
.autotools

# Java annotation processor (APT)
.factorypath

# PDT-specific (PHP Development Tools)
.buildpath

# sbteclipse plugin
.target

# Tern plugin
.tern-project

# TeXlipse plugin
.texlipse

# STS (Spring Tool Suite)
.springBeans

# Code Recommenders
.recommenders/

# Annotation Processing
.apt_generated/
.apt_generated_test/

# Scala IDE specific (Scala & Java development for Eclipse)
.cache-main
.scala_dependencies
.worksheet

# Uncomment this line if you wish to ignore the project description file.
# Typically, this file would be tracked if it contains build/dependency configurations:
#.project
181 changes: 181 additions & 0 deletions rest/java/pom.xml
@@ -0,0 +1,181 @@
<?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>

<groupId>io.werender.examples</groupId>
<artifactId>werender-examples-rest-java</artifactId>
<version>0.1.0</version>

<name>werender-examples-rest-java</name>
<url>https://github.com/j-cube/werender-examples</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<openapi.spec.url>http://192.168.0.105:50000/v1/api</openapi.spec.url>
</properties>

<dependencies>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>4.10.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>io.gsonfire</groupId>
<artifactId>gson-fire</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator</artifactId>
<version>6.2.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>io.werender.examples.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.6.8</version>
<executions>
<execution>
<id>install-whatever</id>
<phase>test</phase>
<goals>
<goal>wget</goal>
</goals>
</execution>
</executions>
<configuration>
<url>${openapi.spec.url}</url>
<outputDirectory>${project.basedir}/src/main/resources/openapi</outputDirectory>
<outputFileName>api.json</outputFileName>
</configuration>
</plugin>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.2.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/openapi/api.json</inputSpec>
<generatorName>java</generatorName>
<configOptions>
<openApiNullable>false</openApiNullable>
<sourceFolder>${project.basedir}/src/gen/java/main</sourceFolder>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
91 changes: 91 additions & 0 deletions rest/java/src/main/java/io/werender/examples/App.java
@@ -0,0 +1,91 @@
package io.werender.examples;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openapitools.client.ApiClient;
import org.openapitools.client.Configuration;
import org.openapitools.client.model.StorageFile;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

public class App {
private static Log log = LogFactory.getLog(App.class);

private static final String APP_NAME = "werender-example-rest-java";

private static final String COMMAND_UPLOAD_FILE = "upload_file";
private static final String COMMAND_HELP = "help";

public static void handleUploadFile(CommandLine cmdLine) {
// Retrieve configuration.
Config config = Config.create();
log.info(String.format("Using server %s", config.getAddress()));

// Create API client.
ApiClient apiClient = Configuration.getDefaultApiClient();
apiClient.setBasePath(config.getAddress());
apiClient.setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15.7) WeRender/0.4.0");

OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new SignatureInterceptor(config.getPublicKey(), config.getPrivateKey()))
.addInterceptor(new HttpLoggingInterceptor()).build();
apiClient.setHttpClient(client);

// Upload the local file to remote directory.
try {
// Get local file path and remote directory path.
String[] optionValues = cmdLine.getOptionValues(COMMAND_UPLOAD_FILE);
String localFilePath = optionValues[0];
String remoteDirPath = optionValues[1];
log.info(String.format("Start to upload file from %s to %s", localFilePath, remoteDirPath));

// Create command to do the actual work.
CommandContext commandContext = new CommandContext(apiClient);
UploadFileCommand command = new UploadFileCommand(localFilePath, remoteDirPath);
StorageFile file = command.execute(commandContext);

log.info(String.format("Uploaded file from %s to %s with id %s", localFilePath, remoteDirPath,
file.getId()));
} catch (Exception e) {
e.printStackTrace();
}
}

public static void handleHelp(Options options) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(APP_NAME, options);
}

public static void main(String[] args) {
// Prepare command line options.
Option uploadFileOption = Option.builder(COMMAND_UPLOAD_FILE).hasArgs()
.desc("upload a file from local path to remote directory").build();
Option helpOption = Option.builder(COMMAND_HELP).desc("print help").build();

Options options = new Options();
options.addOption(uploadFileOption);
options.addOption(helpOption);

// Process command line.
try {
CommandLineParser parser = new DefaultParser();
CommandLine cmdLine = parser.parse(options, args);
if (cmdLine.hasOption(COMMAND_HELP)) {
handleHelp(options);
} else if (cmdLine.hasOption(COMMAND_UPLOAD_FILE)) {
handleUploadFile(cmdLine);
} else {
handleHelp(options);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
7 changes: 7 additions & 0 deletions rest/java/src/main/java/io/werender/examples/Command.java
@@ -0,0 +1,7 @@
package io.werender.examples;

public interface Command<T> {

public T execute(CommandContext context) throws Exception;

}
24 changes: 24 additions & 0 deletions rest/java/src/main/java/io/werender/examples/CommandContext.java
@@ -0,0 +1,24 @@
package io.werender.examples;

import org.openapitools.client.ApiClient;
import org.openapitools.client.api.StorageApi;

public class CommandContext {

private ApiClient apiClient;
private StorageApi storageApi;

public CommandContext(ApiClient apiClient) {
this.apiClient = apiClient;
storageApi = new StorageApi(apiClient);
}

public ApiClient getApiClient() {
return apiClient;
}

public StorageApi getStorageApi() {
return storageApi;
}

}