Permalink
Browse files

first implemantion of client

  • Loading branch information...
dgkncelik authored and karakayasemi committed May 21, 2018
1 parent 4eb1a43 commit 4b02da503b7295d8c6b9a70faaf79b2d7e6c576f
View
@@ -1,6 +1,6 @@
# Compiled class file
*.class
.idea
# Log file
*.log
View
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$" charset="UTF-8" />
</component>
</project>
View
108 pom.xml
@@ -0,0 +1,108 @@
<?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>tr.edu.itu</groupId>
<artifactId>cavabunga-client-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cavabunga-client-java</name>
<description>Cavabunga calendaring application client for java</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from reposi/otory -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.tngtech.java/junit-dataprovider -->
<dependency>
<groupId>com.tngtech.junit.dataprovider</groupId>
<artifactId>junit4-dataprovider</artifactId>
<version>2.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>tr.edu.itu</groupId>
<artifactId>cavabunga-lib</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/cavabunga-lib.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- JaCoCo configuration -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,4 @@
package edu.itu.cavabunga.client;
public class CavabungaClient {
}
@@ -0,0 +1,26 @@
package edu.itu.cavabunga.client.configuration;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Data
@Component
public class ClientConfiguration {
private Environment env;
private String cavabungaServerUrl;
private String cavabungaServerPort;
private String cavabungaServerAccessToken;
private String cavabungaServerVersion;
@Autowired
public ClientConfiguration(Environment env){
this.env = env;
cavabungaServerUrl = this.env.getProperty("cavabunga.server.url");
cavabungaServerPort = this.env.getProperty("cavabunga.server.port");
cavabungaServerAccessToken = this.env.getProperty("cavabunga.server.access.token");
cavabungaServerVersion = this.env.getProperty("cavabunga.server.version");
}
}
@@ -0,0 +1,11 @@
package edu.itu.cavabunga.client.exception;
public class ClientException extends RuntimeException {
public ClientException(){}
public ClientException(String message){
super(message);
}
public ClientException(Throwable cause){
super(cause);
}
}
@@ -0,0 +1,46 @@
package edu.itu.cavabunga.client.http;
import edu.itu.cavabunga.client.exception.ClientException;
import lombok.Data;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
@Data
@Component
public class HttpAdapter {
public static String doRequest(String requestUrl, RequestMethod requestMethod, String bodyParameters){
try {
URL url = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(requestMethod.toString());
if(bodyParameters != null && !bodyParameters.isEmpty()){
connection.setDoOutput(true);
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(bodyParameters);
out.flush();
out.close();
}
connection.setRequestProperty("Content-Type", "application/json");
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer result = new StringBuffer();
while((inputLine = in.readLine()) != null){
result.append(inputLine);
}
in.close();
connection.disconnect();
return result.toString();
}catch (Exception e){
throw new ClientException("There is a problem about http connection: " + e.getMessage());
}
}
}
@@ -0,0 +1,85 @@
package edu.itu.cavabunga.client.http;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.itu.cavabunga.client.exception.ClientException;
import edu.itu.cavabunga.lib.entity.Parameter;
import edu.itu.cavabunga.lib.entity.Participant;
import edu.itu.cavabunga.lib.entity.Property;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Data
@Component
public class JsonObjectMapper {
private ObjectMapper mapper;
@Autowired
public JsonObjectMapper(ObjectMapper objectMapper){
this.mapper = objectMapper;
}
public String mapParticipantToJson(Participant participant){
try {
return mapper.writeValueAsString(participant);
}catch (Exception e){
throw new ClientException("There is something wrong in toJson mapping: " + e.getMessage());
}
}
public String mapComponentToJson(edu.itu.cavabunga.lib.entity.Component component){
try {
return mapper.writeValueAsString(component);
}catch (Exception e){
throw new ClientException("There is something wrong in toJson mapping: " + e.getMessage());
}
}
public String mapPropertyToJson(Property property){
try {
return mapper.writeValueAsString(property);
}catch (Exception e){
throw new ClientException("There is something wrong in toJson mapping: " + e.getMessage());
}
}
public String mapParameterToJson(Parameter parameter){
try {
return mapper.writeValueAsString(parameter);
}catch (Exception e){
throw new ClientException("There is something wrong in toJson mapping: " + e.getMessage());
}
}
public Participant mapFromJsonToParticipant(String jsonInput){
try {
return mapper.readValue(jsonInput, Participant.class);
}catch (Exception e){
throw new ClientException("There is something wrong in toParticipant mapping: " + e.getMessage());
}
}
public edu.itu.cavabunga.lib.entity.Component mapFromJsonToComponent(String jsonInput){
try {
return mapper.readValue(jsonInput, edu.itu.cavabunga.lib.entity.Component.class);
}catch (Exception e){
throw new ClientException("There is something wrong in toComponent mapping: " + e.getMessage());
}
}
public Property mapFromJsonToProperty(String jsonInput){
try {
return mapper.readValue(jsonInput, Property.class);
}catch (Exception e){
throw new ClientException("There is something wrong in toProperty mapping: " + e.getMessage());
}
}
public Parameter mapFromJsonToParameter(String jsonInput){
try {
return mapper.readValue(jsonInput, Parameter.class);
}catch (Exception e){
throw new ClientException("There is something wrong in toParameter mapping: " + e.getMessage());
}
}
}
@@ -0,0 +1,27 @@
package edu.itu.cavabunga.client.service;
import edu.itu.cavabunga.client.configuration.ClientConfiguration;
import edu.itu.cavabunga.client.http.HttpAdapter;
import edu.itu.cavabunga.client.http.JsonObjectMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Data
@Service
public class ComponentRestService {
private HttpAdapter httpAdapter;
private JsonObjectMapper jsonObjectMapper;
private ClientConfiguration configuration;
private String apiUri;
private edu.itu.cavabunga.lib.entity.Component bodyObject;
private String requestResult;
@Autowired
public ComponentRestService(ClientConfiguration configuration, HttpAdapter httpAdapter, JsonObjectMapper jsonObjectMapper){
this.configuration = configuration;
this.httpAdapter = httpAdapter;
this.jsonObjectMapper = jsonObjectMapper;
}
}
@@ -0,0 +1,22 @@
package edu.itu.cavabunga.client.service;
import edu.itu.cavabunga.client.configuration.ClientConfiguration;
import edu.itu.cavabunga.lib.entity.Parameter;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Data
public class ParameterRestService {
private ClientConfiguration configuration;
private String apiUri;
private Parameter bodyObject;
private String requestResult;
@Autowired
public ParameterRestService(ClientConfiguration configuration){
this.configuration = configuration;
}
}
@@ -0,0 +1,22 @@
package edu.itu.cavabunga.client.service;
import edu.itu.cavabunga.client.configuration.ClientConfiguration;
import edu.itu.cavabunga.lib.entity.Participant;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Data
@Service
public class ParticipantRestService {
private ClientConfiguration configuration;
private String apiUri;
private Participant bodyObject;
private String requestResult;
@Autowired
public ParticipantRestService(ClientConfiguration configuration){
this.configuration = configuration;
}
}
@@ -0,0 +1,22 @@
package edu.itu.cavabunga.client.service;
import edu.itu.cavabunga.client.configuration.ClientConfiguration;
import edu.itu.cavabunga.lib.entity.Property;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Data
@Service
public class PropertyRestService {
private ClientConfiguration configuration;
private String apiUri;
private Property bodyObject;
private String requestResult;
@Autowired
public PropertyRestService(ClientConfiguration configuration){
this.configuration = configuration;
}
}
Oops, something went wrong.

0 comments on commit 4b02da5

Please sign in to comment.