Skip to content
Snippets Groups Projects
Commit 87f243c6 authored by Martin Stähr's avatar Martin Stähr
Browse files

feat: introduce dependency injection

parent 717497cd
No related branches found
No related tags found
No related merge requests found
package unipotsdam.gf.config;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import unipotsdam.gf.interfaces.ICommunication;
import unipotsdam.gf.modules.communication.service.CommunicationDummyService;
public class GFApplicationBinder extends AbstractBinder {
@Override
protected void configure() {
bind(CommunicationDummyService.class).to(ICommunication.class);
}
}
package unipotsdam.gf;
package unipotsdam.gf.config;
/**
* Created by dehne on 31.05.2018.
......
package unipotsdam.gf.config;
import org.glassfish.jersey.server.ResourceConfig;
public class GFResourceConfig extends ResourceConfig {
public GFResourceConfig() {
register(new GFApplicationBinder());
packages("unipotsdam.gf");
}
}
package unipotsdam.gf.core.database.mysql;
import unipotsdam.gf.GFDatabaseConfig;
import unipotsdam.gf.config.GFDatabaseConfig;
import java.sql.*;
import java.util.Date;
......
......@@ -6,11 +6,17 @@ import unipotsdam.gf.modules.communication.model.Message;
import unipotsdam.gf.modules.communication.model.chat.ChatMessage;
import unipotsdam.gf.modules.communication.model.chat.ChatRoom;
import javax.annotation.ManagedBean;
import javax.annotation.Resource;
import javax.inject.Singleton;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Resource
@ManagedBean
@Singleton
public class CommunicationDummyService implements ICommunication {
@Override
......
......@@ -3,10 +3,13 @@ package unipotsdam.gf.modules.communication.view;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import unipotsdam.gf.core.management.user.User;
import unipotsdam.gf.interfaces.ICommunication;
import unipotsdam.gf.modules.communication.model.chat.ChatMessage;
import unipotsdam.gf.modules.communication.model.chat.ChatRoom;
import unipotsdam.gf.modules.communication.service.CommunicationDummyService;
import javax.annotation.ManagedBean;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
......@@ -21,18 +24,19 @@ import java.util.List;
import static java.util.Objects.isNull;
@Path("/chat")
@ManagedBean
public class CommunicationView {
// TODO: introduce dependency injection
private static final Logger log = LoggerFactory.getLogger(SampleView.class);
Logger log = LoggerFactory.getLogger(SampleView.class);
@Inject
private ICommunication communicationService;
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/info/{roomId}")
public Response getChatRoomInformation(@PathParam("roomId") String roomId) {
CommunicationDummyService communicationDummyService = new CommunicationDummyService();
ChatRoom chatRoom = communicationDummyService.getChatRoomInfo(roomId);
ChatRoom chatRoom = communicationService.getChatRoomInfo(roomId);
if (isNull(chatRoom)) {
log.error("chatRoom not found for roomId: {}", roomId);
return Response.status(Response.Status.NOT_FOUND).build();
......@@ -45,8 +49,7 @@ public class CommunicationView {
@Produces(MediaType.APPLICATION_JSON)
@Path("/history/{roomId}")
public Response getChatHistory(@PathParam("roomId") String roomId) {
CommunicationDummyService communicationDummyService = new CommunicationDummyService();
List<ChatMessage> chatMessages = communicationDummyService.getChatHistory(roomId);
List<ChatMessage> chatMessages = communicationService.getChatHistory(roomId);
if (isNull(chatMessages)) {
log.error("chatRoom not found for roomId: {}", roomId);
return Response.status(Response.Status.NOT_FOUND).build();
......@@ -63,8 +66,7 @@ public class CommunicationView {
if (isNull(name)) {
return Response.status(Response.Status.BAD_REQUEST).entity("no name is not allowed").build();
}
CommunicationDummyService communicationDummyService = new CommunicationDummyService();
String chatId = communicationDummyService.createChatRoom(name, users);
String chatId = communicationService.createChatRoom(name, users);
if (isNull(chatId)) {
log.error("error while creating chatRoom for: name: {}, users: {}", name, users);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
......@@ -79,8 +81,7 @@ public class CommunicationView {
@Path("/user")
@Produces(MediaType.APPLICATION_JSON)
public Response getUser() {
CommunicationDummyService communicationDummyService = new CommunicationDummyService();
User user = communicationDummyService.getUser();
User user = ((CommunicationDummyService) communicationService).getUser();
return Response.ok(user).build();
}
......
......@@ -26,6 +26,10 @@
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>unipotsdam.gf.config.GFResourceConfig</param-value>
</init-param>
<!-- <init-param>
<param-name>com.sun.jersey.config.feature.DisableWADL</param-name>
<param-value>true</param-value>
......
package unipotsdam.gf.modules.communication.view;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.util.runner.ConcurrentRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
import unipotsdam.gf.config.GFResourceConfig;
import unipotsdam.gf.core.management.user.User;
import unipotsdam.gf.modules.communication.model.chat.ChatRoom;
......@@ -28,9 +28,9 @@ public class CommunicationViewTest extends JerseyTest {
@Override
protected Application configure() {
CommunicationView communicationView = new CommunicationView();
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(communicationView);
return resourceConfig;
GFResourceConfig gfResourceConfig = new GFResourceConfig();
gfResourceConfig.register(communicationView);
return gfResourceConfig;
}
@Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment