Permalink
Browse files

Support for cavabunga-server authentication added

  • Loading branch information...
Dogukan CELIK
Dogukan CELIK committed Aug 13, 2018
1 parent eccc4d1 commit f6db5b4861191d84546684c40f4aa0bee19f1c37
@@ -14,6 +14,7 @@
private String cavabungaServerPort;
private String cavabungaServerAccessToken;
private String cavabungaServerVersion;
private String cavabungaTokenHeaderName;
@Autowired
public CavabungaClientConfiguration(Environment env){
@@ -22,5 +23,6 @@ public CavabungaClientConfiguration(Environment env){
cavabungaServerPort = this.env.getProperty("cavabunga.server.port");
cavabungaServerAccessToken = this.env.getProperty("cavabunga.server.access.token");
cavabungaServerVersion = this.env.getProperty("cavabunga.server.version");
cavabungaTokenHeaderName = this.env.getProperty("cavabunga.server.tokenheadername");
}
}
@@ -1,5 +1,8 @@
package tr.edu.itu.cavabunga.client.service;
import org.codehaus.plexus.util.Base64;
import org.springframework.http.HttpHeaders;
import tr.edu.itu.cavabunga.client.configuration.CavabungaClientConfiguration;
import tr.edu.itu.cavabunga.client.exception.ClientException;
import tr.edu.itu.cavabunga.lib.entity.Component;
import tr.edu.itu.cavabunga.lib.entity.Participant;
@@ -9,6 +12,7 @@
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMethod;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
@@ -19,12 +23,16 @@
private ParameterRestService parameterRestService;
private ParticipantRestService participantRestService;
private PropertyRestService propertyRestService;
private CavabungaClientConfiguration cavabungaClientConfiguration;
private String username;
private String password;
@Autowired
public CavabungaClientService(ComponentRestService componentRestService,
ParameterRestService parameterRestService,
ParticipantRestService participantRestService,
PropertyRestService propertyRestService){
PropertyRestService propertyRestService,
CavabungaClientConfiguration cavabungaClientConfiguration){
this.componentRestService = componentRestService;
this.parameterRestService = parameterRestService;
this.participantRestService = participantRestService;
@@ -33,23 +41,26 @@ public CavabungaClientService(ComponentRestService componentRestService,
public List<Participant> retrieveParticipant(String userName){
try {
return participantRestService.getParticipantFromServer("participant/" + userName);
return participantRestService.getParticipantFromServer("participant/" + userName,
createHeader(username,password, cavabungaClientConfiguration.getCavabungaTokenHeaderName(), cavabungaClientConfiguration.getCavabungaServerAccessToken()));
}catch (Exception e){
throw new ClientException("Couldnt recieve participant with username: " + userName + " ,message:" + e.getMessage());
}
}
public List<Component> retrieveComponentById(Long id){
try {
return componentRestService.getComponentFromServer("component/" + id.toString());
return componentRestService.getComponentFromServer("component/" + id.toString(),
createHeader(username,password, cavabungaClientConfiguration.getCavabungaTokenHeaderName(), cavabungaClientConfiguration.getCavabungaServerAccessToken()));
}catch (Exception e){
throw new ClientException("Couldnt recieve component with id of " + id.toString() + " ,message:" + e.getMessage());
}
}
public List<Component> retrieveComponentsByOwner(String userName){
try {
return componentRestService.getComponentFromServer("participant/" + userName + "/components");
return componentRestService.getComponentFromServer("participant/" + userName + "/components",
createHeader(username,password, cavabungaClientConfiguration.getCavabungaTokenHeaderName(), cavabungaClientConfiguration.getCavabungaServerAccessToken()));
}catch (Exception e){
throw new ClientException("Coulnd recieve participants components username: " + userName + " ,message:" + e.getMessage());
}
@@ -58,7 +69,8 @@ public CavabungaClientService(ComponentRestService componentRestService,
public List<Component> retrieveCalendarsByOwner(String userName){
List<Component> calendars = new ArrayList<>();
try {
for (Component c : componentRestService.getComponentFromServer("participant/" + userName + "/components")){
for (Component c : componentRestService.getComponentFromServer("participant/" + userName + "/components",
createHeader(username,password, cavabungaClientConfiguration.getCavabungaTokenHeaderName(), cavabungaClientConfiguration.getCavabungaServerAccessToken()))){
if(c instanceof Calendar){
calendars.add(c);
}
@@ -68,4 +80,14 @@ public CavabungaClientService(ComponentRestService componentRestService,
throw new ClientException("Coulnd recieve participants components username: " + userName + " ,message:" + e.getMessage());
}
}
public HttpHeaders createHeader(String username, String password, String tokenHeaderName, String clientToken){
return new HttpHeaders(){{
String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
String authHeader = "Basic " + new String(encodedAuth);
set("Authorization", authHeader);
set(tokenHeaderName, clientToken);
}};
}
}
@@ -1,6 +1,9 @@
package tr.edu.itu.cavabunga.client.service;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;
import tr.edu.itu.cavabunga.client.configuration.CavabungaClientConfiguration;
import lombok.Data;
@@ -27,13 +30,19 @@ public ComponentRestService(CavabungaClientConfiguration cavabungaClientConfigur
this.restTemplate = restTemplateBuilder.build();
}
public void postComponentToServer(tr.edu.itu.cavabunga.lib.entity.Component component, String apiUri){
this.restTemplate.postForEntity(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
component, Response.class);
public void postComponentToServer(tr.edu.itu.cavabunga.lib.entity.Component component, String apiUri, HttpHeaders httpHeaders){
this.restTemplate.exchange(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
HttpMethod.POST,
new HttpEntity<>(httpHeaders),
Response.class ,
component).getBody();
}
public List<Component> getComponentFromServer(String apiUri){
return this.restTemplate.getForEntity(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,ComponentResponse.class).getBody().getData();
public List<Component> getComponentFromServer(String apiUri, HttpHeaders httpHeaders){
return this.restTemplate.exchange(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
HttpMethod.GET,
new HttpEntity<>(httpHeaders),
ComponentResponse.class).getBody().getData();
}
}
@@ -1,6 +1,9 @@
package tr.edu.itu.cavabunga.client.service;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;
import tr.edu.itu.cavabunga.client.configuration.CavabungaClientConfiguration;
import tr.edu.itu.cavabunga.lib.entity.Parameter;
@@ -26,11 +29,18 @@ public ParameterRestService(CavabungaClientConfiguration cavabungaClientConfigur
this.restTemplate = restTemplateBuilder.build();
}
public void postParameterToServer(Parameter parameter, String apiUri){
this.restTemplate.postForEntity(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri, parameter, Response.class);
public void postParameterToServer(Parameter parameter, String apiUri, HttpHeaders httpHeaders){
this.restTemplate.exchange(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
HttpMethod.POST,
new HttpEntity<>(httpHeaders),
Response.class,
parameter);
}
public List<Parameter> getParameterFromServer(String apiUri){
return this.restTemplate.getForEntity(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri, ParameterResponse.class).getBody().getData();
public List<Parameter> getParameterFromServer(String apiUri, HttpHeaders httpHeaders){
return this.restTemplate.exchange(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
HttpMethod.GET,
new HttpEntity<>(httpHeaders),
ParameterResponse.class).getBody().getData();
}
}
@@ -1,6 +1,9 @@
package tr.edu.itu.cavabunga.client.service;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;
import tr.edu.itu.cavabunga.client.configuration.CavabungaClientConfiguration;
import tr.edu.itu.cavabunga.lib.entity.Participant;
@@ -27,11 +30,18 @@ public ParticipantRestService(CavabungaClientConfiguration cavabungaClientConfig
this.restTemplate = restTemplateBuilder.build();
}
public void postParticipantToServer(Participant participant, String apiUri){
this.restTemplate.postForEntity(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri, participant, Response.class);
public void postParticipantToServer(Participant participant, String apiUri, HttpHeaders httpHeaders){
this.restTemplate.exchange(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
HttpMethod.POST,
new HttpEntity<>(httpHeaders),
Response.class,
participant);
}
public List<Participant> getParticipantFromServer(String apiUri){
return this.restTemplate.getForEntity(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,ParticipantResponse.class).getBody().getData();
public List<Participant> getParticipantFromServer(String apiUri, HttpHeaders httpHeaders){
return this.restTemplate.exchange(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
HttpMethod.GET,
new HttpEntity<>(httpHeaders),
ParticipantResponse.class).getBody().getData();
}
}
@@ -1,6 +1,9 @@
package tr.edu.itu.cavabunga.client.service;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;
import tr.edu.itu.cavabunga.client.configuration.CavabungaClientConfiguration;
import tr.edu.itu.cavabunga.lib.entity.Property;
@@ -27,11 +30,18 @@ public PropertyRestService(CavabungaClientConfiguration cavabungaClientConfigura
this.restTemplate = restTemplateBuilder.build();
}
public void postPropertyToServer(Property property, String apiUri){
this.restTemplate.postForEntity(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri, property, Response.class);
public void postPropertyToServer(Property property, String apiUri, HttpHeaders httpHeaders){
this.restTemplate.exchange(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
HttpMethod.POST,
new HttpEntity<>(httpHeaders),
Response.class,
property);
}
public List<Property> getPropertyFromServer(String apiUri){
return this.restTemplate.getForEntity(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri, PropertyResponse.class).getBody().getData();
public List<Property> getPropertyFromServer(String apiUri, HttpHeaders httpHeaders){
return this.restTemplate.exchange(this.cavabungaClientConfiguration.getCavabungaServerUrl() + ":" + this.cavabungaClientConfiguration.getCavabungaServerPort() + "/" + apiUri,
HttpMethod.GET,
new HttpEntity<>(httpHeaders),
PropertyResponse.class).getBody().getData();
}
}
@@ -2,4 +2,5 @@ cavabunga.server.url=http://localhost
cavabunga.server.port=8080
cavabunga.server.access.token=CAVABUNGA_ACCESS_TOKEN
cavabunga.server.version=1.0
cavabunga.server.tokenheadername=X-Cavabunga-Client-Token
server.port=9090

0 comments on commit f6db5b4

Please sign in to comment.