Permalink
Browse files

Object mappers and parser implemented

CavabungaClient service layer implemented
  • Loading branch information...
dgkncelik committed Jun 26, 2018
1 parent 4b02da5 commit 3d442c534e429f044076c62e48e8b6189f5fdb56
View
@@ -37,11 +37,6 @@
<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>
@@ -69,7 +64,7 @@
<dependency>
<groupId>tr.edu.itu</groupId>
<artifactId>cavabunga-lib</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/cavabunga-lib.jar</systemPath>
</dependency>
@@ -1,4 +1,16 @@
package edu.itu.cavabunga.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class CavabungaClient {
public static void main(String[] args) {
SpringApplication.run(CavabungaClient.class, args);
}
}
@@ -7,7 +7,7 @@
@Data
@Component
public class ClientConfiguration {
public class CavabungaClientConfiguration {
private Environment env;
private String cavabungaServerUrl;
@@ -16,7 +16,7 @@
private String cavabungaServerVersion;
@Autowired
public ClientConfiguration(Environment env){
public CavabungaClientConfiguration(Environment env){
this.env = env;
cavabungaServerUrl = this.env.getProperty("cavabunga.server.url");
cavabungaServerPort = this.env.getProperty("cavabunga.server.port");
@@ -0,0 +1,7 @@
package edu.itu.cavabunga.client.exception;
public class JsonParseException extends RuntimeException {
public JsonParseException(){}
public JsonParseException(String message){super(message);}
public JsonParseException(Throwable cause){super(cause);}
}
@@ -2,13 +2,18 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.itu.cavabunga.client.exception.ClientException;
import edu.itu.cavabunga.client.exception.JsonParseException;
import edu.itu.cavabunga.lib.entity.Parameter;
import edu.itu.cavabunga.lib.entity.Participant;
import edu.itu.cavabunga.lib.entity.Property;
import edu.itu.cavabunga.lib.http.*;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.PropertyResourceBundle;
@Data
@Component
public class JsonObjectMapper {
@@ -23,63 +28,112 @@ 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());
throw new JsonParseException("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());
throw new JsonParseException("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());
throw new JsonParseException("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());
throw new JsonParseException("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());
throw new JsonParseException("There is something wrong in toParticipant mapping: " + e.getMessage());
}
}
public List<Participant> mapFromJsonToParticipantResponseList(String jsonInput){
try{
return mapper.readValue(jsonInput,ParticipantResponse.class).getData();
}catch (Exception e){
throw new JsonParseException("There is something wrong in toParticipantResponse 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());
throw new JsonParseException("There is something wrong in toComponent mapping: " + e.getMessage());
}
}
public List<edu.itu.cavabunga.lib.entity.Component> mapFromJsonToComponentResponseList(String jsonInput){
try{
return mapper.readValue(jsonInput,ComponentResponse.class).getData();
}catch (Exception e){
throw new JsonParseException("There is something wrong in toComponentResponse 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());
throw new JsonParseException("There is something wrong in toProperty mapping: " + e.getMessage());
}
}
public List<Property> mapFromJsonToPropertyResponseList(String jsonInput){
try {
return mapper.readValue(jsonInput, PropertyResponse.class).getData();
}catch (Exception e){
throw new JsonParseException("There is something wrong in toPropertyResponse 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());
throw new JsonParseException("There is something wrong in toParameter mapping: " + e.getMessage());
}
}
public List<Parameter> mapFromJsonToParameterResponseList(String jsonInput){
try {
return mapper.readValue(jsonInput, ParameterResponse.class).getData();
}catch (Exception e){
throw new JsonParseException("There 's something wrong in toParameterResponse mapping: " + e.getMessage());
}
}
public Response mapFromJsonToResponse(String jsonInput){
try {
return mapper.readValue(jsonInput, Response.class);
}catch (Exception e){
throw new JsonParseException("There is something wrong in toResponse mapping: " + e.getMessage());
}
}
public ErrorResponse mapFromJsonToErrorResponse(String jsonInput){
try {
return mapper.readValue(jsonInput,ErrorResponse.class);
}catch (Exception e){
throw new JsonParseException("There is something wrong in toErrorResponse mapping: " + e.getMessage());
}
}
}
@@ -1,27 +1,44 @@
package edu.itu.cavabunga.client.service;
import edu.itu.cavabunga.client.configuration.ClientConfiguration;
import edu.itu.cavabunga.client.configuration.CavabungaClientConfiguration;
import edu.itu.cavabunga.client.exception.ClientException;
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.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@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;
private CavabungaClientConfiguration cavabungaClientConfiguration;
@Autowired
public ComponentRestService(ClientConfiguration configuration, HttpAdapter httpAdapter, JsonObjectMapper jsonObjectMapper){
this.configuration = configuration;
public ComponentRestService(HttpAdapter httpAdapter,
JsonObjectMapper jsonObjectMapper,
CavabungaClientConfiguration cavabungaClientConfiguration){
this.cavabungaClientConfiguration = cavabungaClientConfiguration;
this.httpAdapter = httpAdapter;
this.jsonObjectMapper = jsonObjectMapper;
}
public String sendComponentToServer(edu.itu.cavabunga.lib.entity.Component component,
RequestMethod requestMethod,
String apiUri){
return this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
requestMethod,
this.jsonObjectMapper.mapComponentToJson(component));
}
public List<edu.itu.cavabunga.lib.entity.Component> recieveComponentFromServer(String apiUri, RequestMethod requestMethod){
return this.jsonObjectMapper.mapFromJsonToComponentResponseList( this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
requestMethod, ""));
}
}
@@ -1,22 +1,44 @@
package edu.itu.cavabunga.client.service;
import edu.itu.cavabunga.client.configuration.ClientConfiguration;
import edu.itu.cavabunga.client.configuration.CavabungaClientConfiguration;
import edu.itu.cavabunga.client.http.HttpAdapter;
import edu.itu.cavabunga.client.http.JsonObjectMapper;
import edu.itu.cavabunga.lib.entity.Parameter;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@Service
@Data
public class ParameterRestService {
private ClientConfiguration configuration;
private String apiUri;
private Parameter bodyObject;
private String requestResult;
private HttpAdapter httpAdapter;
private JsonObjectMapper jsonObjectMapper;
private CavabungaClientConfiguration cavabungaClientConfiguration;
@Autowired
public ParameterRestService(ClientConfiguration configuration){
this.configuration = configuration;
public ParameterRestService(HttpAdapter httpAdapter,
JsonObjectMapper jsonObjectMapper,
CavabungaClientConfiguration cavabungaClientConfiguration){
this.httpAdapter = httpAdapter;
this.jsonObjectMapper = jsonObjectMapper;
this.cavabungaClientConfiguration = cavabungaClientConfiguration;
}
public String sendParameterToServer(Parameter parameter,
RequestMethod requestMethod,
String apiUri){
return this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
requestMethod,
this.jsonObjectMapper.mapParameterToJson(parameter));
}
public List<Parameter> recieveParameterFromServer(String apiUri,
RequestMethod requestMethod){
return this.jsonObjectMapper.mapFromJsonToParameterResponseList( this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
requestMethod, ""));
}
}
@@ -1,22 +1,46 @@
package edu.itu.cavabunga.client.service;
import edu.itu.cavabunga.client.configuration.ClientConfiguration;
import edu.itu.cavabunga.client.CavabungaClient;
import edu.itu.cavabunga.client.configuration.CavabungaClientConfiguration;
import edu.itu.cavabunga.client.http.HttpAdapter;
import edu.itu.cavabunga.client.http.JsonObjectMapper;
import edu.itu.cavabunga.lib.entity.Component;
import edu.itu.cavabunga.lib.entity.Participant;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@Data
@Service
public class ParticipantRestService {
private ClientConfiguration configuration;
private String apiUri;
private Participant bodyObject;
private String requestResult;
private HttpAdapter httpAdapter;
private JsonObjectMapper jsonObjectMapper;
private CavabungaClientConfiguration cavabungaClientConfiguration;
@Autowired
public ParticipantRestService(ClientConfiguration configuration){
this.configuration = configuration;
public ParticipantRestService(HttpAdapter httpAdapter,
JsonObjectMapper jsonObjectMapper,
CavabungaClientConfiguration cavabungaClientConfiguration){
this.cavabungaClientConfiguration = cavabungaClientConfiguration;
this.jsonObjectMapper = jsonObjectMapper;
this.cavabungaClientConfiguration = cavabungaClientConfiguration;
}
public String sendParticipantToServer(Participant participant,
RequestMethod requestMethod,
String apiUri){
return this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":"+ this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
requestMethod,
this.jsonObjectMapper.mapParticipantToJson(participant));
}
public List<Participant> recieveParticipantFromServer(String apiUri, RequestMethod requestMethod){
return this.jsonObjectMapper.mapFromJsonToParticipantResponseList( this.httpAdapter.doRequest(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
requestMethod, ""));
}
}
Oops, something went wrong.

0 comments on commit 3d442c5

Please sign in to comment.