Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kusanagi/katana-sdk-java7
Java 7 SDK for the KATANA Framework
https://github.com/kusanagi/katana-sdk-java7
framework java java7 katana kusanagi sdk
Last synced: 4 days ago
JSON representation
Java 7 SDK for the KATANA Framework
- Host: GitHub
- URL: https://github.com/kusanagi/katana-sdk-java7
- Owner: kusanagi
- License: mit
- Created: 2016-08-18T13:50:00.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-01T09:16:57.000Z (over 6 years ago)
- Last Synced: 2024-03-18T12:14:04.931Z (10 months ago)
- Topics: framework, java, java7, katana, kusanagi, sdk
- Language: Java
- Homepage: https://kusanagi.io
- Size: 3.43 MB
- Stars: 2
- Watchers: 8
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
KATANA SDK for Java 7
=====================[![Build Status](https://travis-ci.org/kusanagi/katana-sdk-java7.svg?branch=master)](https://travis-ci.org/kusanagi/katana-sdk-java7)
[![Coverage Status](https://coveralls.io/repos/github/kusanagi/katana-sdk-java7/badge.svg?branch=master)](https://coveralls.io/github/kusanagi/katana-sdk-java7?branch=master)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)Java SDK to interface with the **KATANA**™ framework (https://kusanagi.io).
Requirements
------------* KATANA Framework 2.1
* [JDK](http://docs.oracle.com/javase/7/docs/webnotes/install/) 1.7
* [libzmq](http://zeromq.org/intro:get-the-software) 4.1.5+Installation
------------To install and use the Java SDK you'll need to first install the JDK 1.7. To do so using **apt** you can run the following from the command-line:
```
$ sudo apt-add-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java7-installer
```The SDK can then be built with either [Maven](https://maven.apache.org) or [Gradle](https://gradle.org).
If using **Maven**, add the following in your `pom.xml` file:
```xml
io.kusanagi
katana-sdk-java7
2.1.0```
Or, if using **Gradle**, add the following in your `build.gradle` file:
```gradle
dependencies {
compile group: 'io.kusanagi', name: 'katana-sdk-java7', version: '2.1.0'
}
```Getting Started
---------------The **KATANA**™ SDK is fairly simple to use. We begin by first defining the model of our components using configuration files.
Here we'll use `XML`, but the framework also supports both `JSON` and `YAML` formats.
First, create a file named `middleware.xml`, with the following configuration:
```
...
...```
Then, create a file named `service.xml`, with the following configuration:
```
...
...```
Now, create a file named `middleware.class.java`, and add the following source code for the **Middleware**, which handles both a **Request** and a **Response**:
```java
import io.kusanagi.katana.sdk.Middleware;
import io.kusanagi.katana.sdk.Callable;
import io.kusanagi.katana.sdk.Request;
import io.kusanagi.katana.sdk.Response;public class Middleware {
public static void main(String[] args) {
Middleware middleware = new Middleware(args);
middleware.request(new Callable() {
@Override
public Request run(Request request) {
// your logic here
return request;
}
});
middleware.response(new Callable() {
@Override
public Response run(Response response) {
// your logic here
return response;
}
});
middleware.run();
}
}
```And also create a file named `service.class.java`, and add the following source code for the **Service**, which registers an action:
```java
import io.kusanagi.katana.sdk.Service;
import io.kusanagi.katana.sdk.Callable;
import io.kusanagi.katana.sdk.Action;public class Service {
public static void main(String[] args) {
Service service = new Service(args);
service.action("actionName", new Callable() {
@Override
public Action run(Action action) {
// your logic here
return action;
}
});
service.run();
}
}
```Examples
--------The following example is a **Middleware** which translates HTTP requests to CRUD actions based on REST conventions:
```java
package com.katana.example;import io.kusanagi.katana.sdk.Middleware;
import io.kusanagi.katana.sdk.Callable;
import io.kusanagi.katana.sdk.Request;import java.util.Iterator;
import java.util.Map;public class Rest {
public static void main(String[] args) {
Middleware middleware = new Middleware(args);
middleware.request(new Callable() {
@Override
public Request run(Request request) {
// the URL format expected is "/{version}/{service}/{extra}"
String[] parts = request.getHttpRequest().getUrlPath().split("/");
// set the Service version
request.setServiceVersion(parts[1]);
// set the Service name
request.setServiceName(parts[2]);
boolean hasExtraPath = parts.length == 4 && !parts[3].isEmpty();
String method = request.getHttpRequest().getMethod();
// resolve the Service action to call
switch (method) {
case "GET":
return request.setActionName(hasExtraPath ? "read" : "list");
case "POST":
return request.setActionName("create");
case "PUT":
return request.setActionName("replace");
case "PATCH":
return request.setActionName("update");
case "DELETE":
return request.setActionName("delete");
default:
return request.setActionName(actionName);
}
}
});
middleware.run();
}
}
```The following example is a **Service** named "users", with a "read" action which retrieves a user from a `List` and returns the entity according to the `user_id` parameter:
```java
package com.katana.example;import io.kusanagi.katana.sdk.Service;
import io.kusanagi.katana.sdk.Callable;
import io.kusanagi.katana.sdk.Action;import java.util.ArrayList;
import java.util.List;public class UserService {
public static void main(String[] args) {
Service service = new Service(args);
service.action("read", new Callable() {
@Override
public Action run(Action action) {
// list of users, this would normally be a DB call
final List users = new ArrayList<>();
users.add(new User(1, "James"));
users.add(new User(2, "Jeronimo"));
users.add(new User(3, "Fernando"));
users.add(new User(4, "Ricardo"));
users.add(new User(5, "Hugo"));
// read the incoming "id" parameter
int userId = (Integer) action.getParam("id").getValue();
User entity = null;
// find the user in the list
for (User user : users) {
if (user.getId() == userId) {
entity = user;
break;
}
}
if (entity == null) {
return action.error("User does not exist", 1, "404 Not Found");
}
action.setEntity(entity);
action.setLink("self", "/0.1.0/users/" + userId);
return action;
}
});
service.run();
}
}
```Documentation
-------------See the [API](https://app.kusanagi.io#katana/docs/sdk) for a technical reference of the SDK.
For help using the framework check the [documentation](https://app.kusanagi.io#katana/docs).
Support
-------Please first read our [contribution guidelines](https://app.kusanagi.io#katana/open-source/contributing).
* [Requesting help](https://app.kusanagi.io#katana/open-source/help)
* [Reporting a bug](https://app.kusanagi.io#katana/open-source/bug)
* [Submitting a patch](https://app.kusanagi.io#katana/open-source/patch)
* [Security issues](https://app.kusanagi.io#katana/open-source/security)We use [milestones](https://github.com/kusanagi/katana-sdk-java7/milestones) to track upcoming releases inline with our [versioning](https://app.kusanagi.io#katana/docs/framework/versions) strategy, and as defined in our [roadmap](https://app.kusanagi.io#katana/docs/framework/roadmap).
For commercial support see the [solutions](https://kusanagi.io/solutions) available or [contact us](https://kusanagi.io/contact) for more information.
Contributing
------------If you'd like to know how you can help and support our Open Source efforts see the many ways to [get involved](https://app.kusanagi.io#katana/open-source).
Please also be sure to review our [community guidelines](https://app.kusanagi.io#katana/open-source/conduct).
License
-------Copyright 2016-2018 KUSANAGI S.L. (https://kusanagi.io). All rights reserved.
KUSANAGI, the sword logo, KATANA and the "K" logo are trademarks and/or registered trademarks of KUSANAGI S.L. All other trademarks are property of their respective owners.
Licensed under the [MIT License](https://app.kusanagi.io#katana/open-source/license). Redistributions of the source code included in this repository must retain the copyright notice found in each file.