Skip to content

Commit

Permalink
refactored code according to SONAR
Browse files Browse the repository at this point in the history
version 1.0.0 release ready
  • Loading branch information
grimsi committed Apr 9, 2019
1 parent 54104e6 commit 6a322c3
Show file tree
Hide file tree
Showing 19 changed files with 188 additions and 101 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
<artifactId>java-jwt</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>org.owasp.encoder</groupId>
<artifactId>encoder</artifactId>
<version>1.2.2</version>
</dependency>
<!-- Spring data -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package grimsi.accservermanager.backend.configuration;

import grimsi.accservermanager.backend.error.ApiError;
import org.owasp.encoder.Encode;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -31,7 +32,12 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotV
)
);

ApiError apiError = new ApiError(status, errorsMap, "Validation error", ((ServletWebRequest) request).getRequest().getRequestURI());

String requestUri = ((ServletWebRequest) request).getRequest().getRequestURI();

requestUri = Encode.forHtml(requestUri);

ApiError apiError = new ApiError(status, errorsMap, "Validation error", requestUri);

return new ResponseEntity<>(apiError, headers, status);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package grimsi.accservermanager.backend.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class FrontendController {
@RequestMapping(value = "/**/{path:[^\\.]*}")

@GetMapping(value = "/**/{path:[^\\.]*}")
public String gui() {
return "forward:/index.html";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@Getter
@Setter
public class Configuration {
public int udpPort;
public int tcpPort;
public int maxClients;
public int configVersion = 1;
private int udpPort;
private int tcpPort;
private int maxClients;
private int configVersion = 1;
}
26 changes: 13 additions & 13 deletions src/main/java/grimsi/accservermanager/backend/entity/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
@Setter
public class Event {
@Id
public String id;
private String id;

@Indexed(unique = true)
public String name;
private String name;

public Track track;
public EventType eventType;
public int preRaceWaitingTimeSeconds;
public int sessionOverTimeSeconds;
public int ambientTemp;
public int trackTemp;
public BigDecimal cloudLevel;
public BigDecimal rain;
public BigDecimal weatherRandomness;
public List<Session> sessions;
public int configVersion = 1;
private Track track;
private EventType eventType;
private int preRaceWaitingTimeSeconds;
private int sessionOverTimeSeconds;
private int ambientTemp;
private int trackTemp;
private BigDecimal cloudLevel;
private BigDecimal rain;
private BigDecimal weatherRandomness;
private List<Session> sessions;
private int configVersion = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@
@Setter
public class Instance {
@Id
public String id;
private String id;

public boolean restartRequired;
private boolean restartRequired;

@Indexed(unique = true)
public String name;
private String name;

public InstanceState state;
private InstanceState state;

public String container;
private String container;

public Configuration configuration;
private Configuration configuration;

public Settings settings;
private Settings settings;

@DBRef
public Event event;
private Event event;

private String version;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
@Getter
@Setter
public class Session {
public int hourOfDay;
public int dayOfWeekend;
public BigDecimal timeMultiplier;
public SessionType sessionType;
public int sessionDurationMinutes;
private int hourOfDay;
private int dayOfWeekend;
private BigDecimal timeMultiplier;
private SessionType sessionType;
private int sessionDurationMinutes;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
@Getter
@Setter
public class Settings {
public String serverName;
public String password;
public String adminPassword;
public int trackMedalsRequirement;
public int safetyRatingRequirement;
public int configVersion = 1;
private String serverName;
private String password;
private String adminPassword;
private int trackMedalsRequirement;
private int safetyRatingRequirement;
private int configVersion = 1;
}
13 changes: 9 additions & 4 deletions src/main/java/grimsi/accservermanager/backend/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serializable;

@Document
@Getter
@Setter
public class User {
public class User implements Serializable {

private static final long serialVersionUID = 7650367368584885329L;

@Id
public String id;
private String id;

public String username;
public String password;
private String username;
private String password;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public Authentication attemptAuthentication(HttpServletRequest req,

return authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(
creds.username,
creds.password,
creds.getUsername(),
creds.getPassword(),
new ArrayList<>())
);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

public class JWTAuthorizationFilter extends BasicAuthenticationFilter {

private final Logger logger = LoggerFactory.getLogger(JWTAuthorizationFilter.class);
private final Logger log = LoggerFactory.getLogger(JWTAuthorizationFilter.class);

private ApplicationConfiguration config;

Expand Down Expand Up @@ -61,10 +61,10 @@ private UsernamePasswordAuthenticationToken getAuthentication(HttpServletRequest
return new UsernamePasswordAuthenticationToken(user, null, new ArrayList<>());
}
} catch (SignatureVerificationException e) {
logger.warn("Error verifying JWT: " + e.getMessage());
log.warn("Error verifying JWT: " + e.getMessage());
}
return null;
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@Setter
public class UserPrincipal implements UserDetails {

private static final long serialVersionUID = 6850396258498499969L;
private User user;
private List<SimpleGrantedAuthority> authorities;

Expand All @@ -23,12 +24,12 @@ public UserPrincipal(User user, SimpleGrantedAuthority authority) {

@Override
public String getPassword() {
return user.password;
return user.getPassword();
}

@Override
public String getUsername() {
return user.username;
return user.getUsername();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ public ContainerService() {
public List<Container> getAllContainers() {
try {
return docker.listContainers(DockerClient.ListContainersParam.allContainers());
} catch (DockerException | InterruptedException e) {
} catch (DockerException e) {
throw new ContainerException("Could not get a list of containers: " + e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

return Collections.emptyList();
}

public void deployInstance(InstanceDto instance) {
Expand All @@ -75,11 +79,13 @@ public void deployInstance(InstanceDto instance) {
if (images.isEmpty()) {
docker.pull(config.getContainerImage());
}
} catch (DockerException | InterruptedException e) {
} catch (DockerException e) {
throw new ContainerException("Could not find/pull image '" + config.getContainerImage() + "': " + e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

log.info("Mounting dir: '" + fileSystemService.getInstanceFolderPath(instance, true) + ":" + config.getFolderPathContainerized() + "'.");
log.debug("Mounting dir: '" + fileSystemService.getInstanceFolderPath(instance, true) + ":" + config.getFolderPathContainerized() + "'.");

HostConfig hostConfig = HostConfig.builder()
.portBindings(portBindings)
Expand All @@ -94,47 +100,58 @@ public void deployInstance(InstanceDto instance) {
.build();

try {
log.debug("Container config: \n\n" + gson.toJson(containerConfig) + "\n\n");

String containerName = buildContainerName(instance);
log.info("Container config: \n\n" + gson.toJson(containerConfig) + "\n\n");
ContainerCreation container = docker.createContainer(containerConfig, containerName);

instance.setContainer(container.id());
instanceService.save(instance);

} catch (DockerException | InterruptedException e) {
} catch (DockerException e) {
throw new ContainerException("Could not create container for instance '" + instance.getId() + "': " + e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

public void startInstance(InstanceDto instance) {
try {
docker.startContainer(instance.getContainer());
} catch (DockerException | InterruptedException e) {
} catch (DockerException e) {
throw new ContainerException("Could not start container '" + instance.getContainer() + "': " + e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

public void stopInstance(InstanceDto instance) {
try {
docker.stopContainer(instance.getContainer(), 0);
} catch (DockerException | InterruptedException e) {
} catch (DockerException e) {
throw new ContainerException("Could not stop container '" + instance.getContainer() + "': " + e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

public void pauseInstance(InstanceDto instance) {
try {
docker.pauseContainer(instance.getContainer());
} catch (DockerException | InterruptedException e) {
} catch (DockerException e) {
throw new ContainerException("Could not pause container '" + instance.getContainer() + "': " + e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

public void resumeInstance(InstanceDto instance) {
try {
docker.unpauseContainer(instance.getContainer());
} catch (DockerException | InterruptedException e) {
} catch (DockerException e) {
throw new ContainerException("Could not resume container '" + instance.getContainer() + "': " + e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

Expand All @@ -146,16 +163,20 @@ public void deleteContainer(String id) {
try {
docker.stopContainer(id, 0);
docker.removeContainer(id);
} catch (DockerException | InterruptedException | NullPointerException e) {
} catch (DockerException | NullPointerException e) {
throw new ContainerException("Cant delete container '" + id + "': " + e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

public void pullImage(String imageName) {
try {
docker.pull(imageName);
} catch (DockerException | InterruptedException e) {
} catch (DockerException e) {
throw new ContainerException("Cant pull image '" + imageName + "': " + e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

Expand All @@ -169,9 +190,12 @@ public boolean instanceHasContainer(InstanceDto instance) {
public ContainerStats getContainerStats(InstanceDto instance) {
try {
return docker.stats(instance.getContainer());
} catch (DockerException | InterruptedException e) {
} catch (DockerException e) {
log.error(e.toString());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public EventDto save(EventDto eventDto) {
try {
event = eventRepository.save(event);
} catch (DuplicateKeyException e) {
throw new ConflictException("Name '" + event.name + "' is already in use.");
throw new ConflictException("Name '" + event.getName() + "' is already in use.");
}

return convertToDto(event);
Expand Down

0 comments on commit 6a322c3

Please sign in to comment.