Skip to content

Commit

Permalink
Apply Google Java Style Format
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaiser-Yang authored and github-actions[bot] committed Sep 24, 2024
1 parent c4bece6 commit 1e752b8
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 129 deletions.
13 changes: 4 additions & 9 deletions src/main/java/edu/cmipt/gcs/constant/ApiPathConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,8 @@ public class ApiPathConstant {
REPOSITORY_API_PREFIX + "/create";

public static final String SSH_KEY_API_PREFIX = ALL_API_PREFIX + "/ssh";
public static final String SSH_KEY_UPLOAD_SSH_KEY_API_PATH =
SSH_KEY_API_PREFIX + "/upload";
public static final String SSH_KEY_UPDATE_SSH_KEY_API_PATH =
SSH_KEY_API_PREFIX + "/update";
public static final String SSH_KEY_DELETE_SSH_KEY_API_PATH =
SSH_KEY_API_PREFIX + "/delete";
public static final String SSH_KEY_PAGE_SSH_KEY_API_PATH =
SSH_KEY_API_PREFIX + "/page";

public static final String SSH_KEY_UPLOAD_SSH_KEY_API_PATH = SSH_KEY_API_PREFIX + "/upload";
public static final String SSH_KEY_UPDATE_SSH_KEY_API_PATH = SSH_KEY_API_PREFIX + "/update";
public static final String SSH_KEY_DELETE_SSH_KEY_API_PATH = SSH_KEY_API_PREFIX + "/delete";
public static final String SSH_KEY_PAGE_SSH_KEY_API_PATH = SSH_KEY_API_PREFIX + "/page";
}
3 changes: 2 additions & 1 deletion src/main/java/edu/cmipt/gcs/constant/GitConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public class GitConstant {

public static String GIT_SERVER_DOMAIN;

public static final String SSH_KEY_PREFIX = "no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ";
public static final String SSH_KEY_PREFIX =
"no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ";

@Value("${git.user.name}")
public void setGIT_USER_NAME(String gitUserName) {
Expand Down
52 changes: 32 additions & 20 deletions src/main/java/edu/cmipt/gcs/controller/SshKeyController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package edu.cmipt.gcs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import edu.cmipt.gcs.constant.ApiPathConstant;
import edu.cmipt.gcs.constant.HeaderParameter;
import edu.cmipt.gcs.enumeration.ErrorCodeEnum;
Expand All @@ -12,6 +15,7 @@
import edu.cmipt.gcs.util.JwtUtil;
import edu.cmipt.gcs.validation.group.CreateGroup;
import edu.cmipt.gcs.validation.group.UpdateGroup;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
Expand All @@ -22,9 +26,6 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
Expand All @@ -36,8 +37,8 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Tag(name = "SSH", description = "SSH APIs")
Expand All @@ -55,7 +56,8 @@ public class SshKeyController {
description = "Access token",
required = true,
in = ParameterIn.HEADER,
schema = @Schema(implementation = String.class))})
schema = @Schema(implementation = String.class))
})
@ApiResponses({
@ApiResponse(responseCode = "200", description = "SSH key uploaded successfully"),
@ApiResponse(
Expand All @@ -64,8 +66,9 @@ public class SshKeyController {
content = @Content(schema = @Schema(implementation = ErrorVO.class))),
@ApiResponse(responseCode = "500", description = "Internal server error")
})
public void uploadSshKey(@Validated(CreateGroup.class) @RequestBody SshKeyDTO sshKeyDTO,
@RequestHeader(HeaderParameter.ACCESS_TOKEN) String accessToken){
public void uploadSshKey(
@Validated(CreateGroup.class) @RequestBody SshKeyDTO sshKeyDTO,
@RequestHeader(HeaderParameter.ACCESS_TOKEN) String accessToken) {
if (!sshKeyService.save(new SshKeyPO(sshKeyDTO))) {
throw new GenericException(ErrorCodeEnum.SSH_KEY_UPLOAD_FAILED, sshKeyDTO);
}
Expand All @@ -88,10 +91,12 @@ public void uploadSshKey(@Validated(CreateGroup.class) @RequestBody SshKeyDTO ss
description = "SSH key ID",
required = true,
in = ParameterIn.QUERY,
schema = @Schema(implementation = Long.class))})
schema = @Schema(implementation = Long.class))
})
@ApiResponse(responseCode = "200", description = "SSH key deleted successfully")
public void deleteSshKey(@RequestHeader(HeaderParameter.ACCESS_TOKEN) String accessToken,
@RequestParam("id") Long id) {
public void deleteSshKey(
@RequestHeader(HeaderParameter.ACCESS_TOKEN) String accessToken,
@RequestParam("id") Long id) {
var res = sshKeyService.getById(id);
if (res == null) {
throw new GenericException(ErrorCodeEnum.SSH_KEY_NOT_FOUND, id);
Expand All @@ -115,20 +120,22 @@ public void deleteSshKey(@RequestHeader(HeaderParameter.ACCESS_TOKEN) String acc
description = "Access token",
required = true,
in = ParameterIn.HEADER,
schema = @Schema(implementation = String.class))})
schema = @Schema(implementation = String.class))
})
@ApiResponses({
@ApiResponse(responseCode = "200", description = "SSH key updated successfully"),
@ApiResponse(
responseCode = "400",
description = "SSH key update failed",
content = @Content(schema = @Schema(implementation = ErrorVO.class)))})
content = @Content(schema = @Schema(implementation = ErrorVO.class)))
})
public ResponseEntity<SshKeyVO> updateSshKey(
@Validated(UpdateGroup.class) @RequestBody SshKeyDTO sshKeyDTO
) {
@Validated(UpdateGroup.class) @RequestBody SshKeyDTO sshKeyDTO) {
if (!sshKeyService.updateById(new SshKeyPO(sshKeyDTO))) {
throw new GenericException(ErrorCodeEnum.SSH_KEY_UPDATE_FAILED, sshKeyDTO);
}
return ResponseEntity.ok().body(new SshKeyVO(sshKeyService.getById(Long.valueOf(sshKeyDTO.id()))));
return ResponseEntity.ok()
.body(new SshKeyVO(sshKeyService.getById(Long.valueOf(sshKeyDTO.id()))));
}

@GetMapping(ApiPathConstant.SSH_KEY_PAGE_SSH_KEY_API_PATH)
Expand Down Expand Up @@ -162,12 +169,17 @@ public ResponseEntity<SshKeyVO> updateSshKey(
example = "10",
required = true,
in = ParameterIn.QUERY,
schema = @Schema(implementation = Integer.class))})
schema = @Schema(implementation = Integer.class))
})
@ApiResponse(responseCode = "200", description = "SSH key paged successfully")
public List<SshKeyVO> pageSshKey(@RequestParam("id") Long userId,
@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
public List<SshKeyVO> pageSshKey(
@RequestParam("id") Long userId,
@RequestParam("page") Integer page,
@RequestParam("size") Integer size) {
QueryWrapper<SshKeyPO> wrapper = new QueryWrapper<>();
wrapper.eq("user_id", userId);
return sshKeyService.list(new Page<>(page, size), wrapper).stream().map(SshKeyVO::new).collect(Collectors.toList());
return sshKeyService.list(new Page<>(page, size), wrapper).stream()
.map(SshKeyVO::new)
.collect(Collectors.toList());
}
}
16 changes: 11 additions & 5 deletions src/main/java/edu/cmipt/gcs/filter/JwtFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ private void authorize(HttpServletRequest request, String accessToken, String re
} else if (request.getRequestURI()
.equals(ApiPathConstant.USER_PAGE_USER_REPOSITORY_API_PATH)) {
// pass
} else if (request.getRequestURI()
.equals(ApiPathConstant.USER_GET_USER_API_PATH)) {
} else if (request.getRequestURI().equals(ApiPathConstant.USER_GET_USER_API_PATH)) {
// pass
} else if (request.getRequestURI()
.equals(ApiPathConstant.SSH_KEY_PAGE_SSH_KEY_API_PATH)) {
String idInToken = JwtUtil.getId(accessToken);
String idInParam = request.getParameter("id");
if (!idInToken.equals(idInParam)) {
logger.info("User[{}] tried to get SSH key of user[{}]", idInToken, idInParam);
logger.info(
"User[{}] tried to get SSH key of user[{}]", idInToken, idInParam);
throw new GenericException(ErrorCodeEnum.ACCESS_DENIED);
}
} else {
Expand Down Expand Up @@ -201,15 +201,21 @@ private void authorize(HttpServletRequest request, String accessToken, String re
String idInToken = JwtUtil.getId(accessToken);
String idInBody = getFromRequestBody(request, "userId");
if (!idInToken.equals(idInBody)) {
logger.info("User[{}] tried to upload SSH key of user[{}]", idInToken, idInBody);
logger.info(
"User[{}] tried to upload SSH key of user[{}]",
idInToken,
idInBody);
throw new GenericException(ErrorCodeEnum.ACCESS_DENIED);
}
} else if (request.getRequestURI()
.equals(ApiPathConstant.SSH_KEY_UPDATE_SSH_KEY_API_PATH)) {
String idInToken = JwtUtil.getId(accessToken);
String idInBody = getFromRequestBody(request, "userId");
if (!idInToken.equals(idInBody)) {
logger.info("User[{}] tried to update SSH key of user[{}]", idInToken, idInBody);
logger.info(
"User[{}] tried to update SSH key of user[{}]",
idInToken,
idInBody);
throw new GenericException(ErrorCodeEnum.ACCESS_DENIED);
}
} else {
Expand Down
68 changes: 33 additions & 35 deletions src/main/java/edu/cmipt/gcs/pojo/ssh/SshKeyDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,44 @@
import edu.cmipt.gcs.constant.ValidationConstant;
import edu.cmipt.gcs.validation.group.CreateGroup;
import edu.cmipt.gcs.validation.group.UpdateGroup;

import io.swagger.v3.oas.annotations.media.Schema;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Null;
import jakarta.validation.constraints.Size;

@Schema(description = "SSH Key Data Transfer Object")
public record SshKeyDTO(
@Schema(description = "SSH Key ID")
@Null(groups = CreateGroup.class, message = "SSHKEYDTO_ID_NULL {SshKeyDTO.id.Null}")
@NotNull(
groups = UpdateGroup.class,
message = "SSHKEYDTO_ID_NOTNULL {SshKeyDTO.id.NotNull}")
String id,
@Schema(description = "User ID")
@NotBlank(
groups = {CreateGroup.class, UpdateGroup.class},
message = "SSHKEYDTO_USERID_NOTBLANK {SshKeyDTO.userId.NotBlank}")
String userId,
@Schema(
description = "Name",
example = "My SSH Key")
@NotBlank(
groups = {CreateGroup.class, UpdateGroup.class},
message = "SSHKEYDTO_NAME_NOTBLANK {SshKeyDTO.name.NotBlank}")
@Size(
groups = {CreateGroup.class, UpdateGroup.class},
min = ValidationConstant.MIN_SSH_KEY_NAME_LENGTH,
max = ValidationConstant.MAX_SSH_KEY_NAME_LENGTH,
message = "SSHKEYDTO_NAME_SIZE {SshKeyDTO.name.Size}")
String name,
@Schema(
description = "Public Key")
@NotBlank(
groups = CreateGroup.class,
message = "SSHKEYDTO_PUBLICKEY_NOTBLANK {SshKeyDTO.publicKey.NotBlank}")
@Size(
groups = {CreateGroup.class, UpdateGroup.class},
min = ValidationConstant.MIN_SSH_KEY_PUBLICKEY_LENGTH,
max = ValidationConstant.MAX_SSH_KEY_PUBLICKEY_LENGTH,
message = "SSHKEYDTO_PUBLICKEY_SIZE {SshKeyDTO.publicKey.Size}")
String publicKey) {
}
@Schema(description = "SSH Key ID")
@Null(groups = CreateGroup.class, message = "SSHKEYDTO_ID_NULL {SshKeyDTO.id.Null}")
@NotNull(
groups = UpdateGroup.class,
message = "SSHKEYDTO_ID_NOTNULL {SshKeyDTO.id.NotNull}")
String id,
@Schema(description = "User ID")
@NotBlank(
groups = {CreateGroup.class, UpdateGroup.class},
message = "SSHKEYDTO_USERID_NOTBLANK {SshKeyDTO.userId.NotBlank}")
String userId,
@Schema(description = "Name", example = "My SSH Key")
@NotBlank(
groups = {CreateGroup.class, UpdateGroup.class},
message = "SSHKEYDTO_NAME_NOTBLANK {SshKeyDTO.name.NotBlank}")
@Size(
groups = {CreateGroup.class, UpdateGroup.class},
min = ValidationConstant.MIN_SSH_KEY_NAME_LENGTH,
max = ValidationConstant.MAX_SSH_KEY_NAME_LENGTH,
message = "SSHKEYDTO_NAME_SIZE {SshKeyDTO.name.Size}")
String name,
@Schema(description = "Public Key")
@NotBlank(
groups = CreateGroup.class,
message = "SSHKEYDTO_PUBLICKEY_NOTBLANK {SshKeyDTO.publicKey.NotBlank}")
@Size(
groups = {CreateGroup.class, UpdateGroup.class},
min = ValidationConstant.MIN_SSH_KEY_PUBLICKEY_LENGTH,
max = ValidationConstant.MAX_SSH_KEY_PUBLICKEY_LENGTH,
message = "SSHKEYDTO_PUBLICKEY_SIZE {SshKeyDTO.publicKey.Size}")
String publicKey) {}
4 changes: 2 additions & 2 deletions src/main/java/edu/cmipt/gcs/pojo/ssh/SshKeyPO.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package edu.cmipt.gcs.pojo.ssh;

import java.time.LocalDateTime;

import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.time.LocalDateTime;

@Data
@AllArgsConstructor
@TableName("t_ssh_key")
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/edu/cmipt/gcs/pojo/ssh/SshKeyVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
@Schema(description = "SSH Key Value Object")
public record SshKeyVO(String id, String userId, String name, String publicKey) {
public SshKeyVO(SshKeyPO sshKeyPO) {
this(sshKeyPO.getId().toString(), sshKeyPO.getUserId().toString(), sshKeyPO.getName(), sshKeyPO.getPublicKey());
this(
sshKeyPO.getId().toString(),
sshKeyPO.getUserId().toString(),
sshKeyPO.getName(),
sshKeyPO.getPublicKey());
}
}
22 changes: 17 additions & 5 deletions src/main/java/edu/cmipt/gcs/service/RepositoryServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class RepositoryServiceImpl extends ServiceImpl<RepositoryMapper, Reposit
/**
* Save a repository and initialize a git repository in the file system.
*
* <p> Usually, the user will not create the same repository at the same time, so we don't
* <p>Usually, the user will not create the same repository at the same time, so we don't
* consider the thread competition
*/
@Transactional
Expand All @@ -42,16 +42,22 @@ public boolean save(RepositoryPO repositoryPO) {
Paths.get(
GitConstant.GIT_REPOSITORY_DIREDTORY,
userMapper.selectById(repositoryPO.getUserId()).getUsername(),
repositoryPO.getRepositoryName() + GitConstant.GIT_REPOSITORY_SUFFIX)
repositoryPO.getRepositoryName()
+ GitConstant.GIT_REPOSITORY_SUFFIX)
.toString();
// check if the repositorySavePath has been created, if so, remove it
// this may occur, if the last creation failed
if (Files.exists(Paths.get(repositorySavePath))){
if (Files.exists(Paths.get(repositorySavePath))) {
logger.info("Repository save path exists, try to remove it");
try {
ProcessBuilder dirRemover =
new ProcessBuilder(
"sudo", "-u", GitConstant.GIT_USER_NAME, "rm", "-rf", repositorySavePath);
"sudo",
"-u",
GitConstant.GIT_USER_NAME,
"rm",
"-rf",
repositorySavePath);
Process process = dirRemover.start();
if (process.waitFor() != 0) {
throw new GenericException(
Expand All @@ -67,7 +73,13 @@ public boolean save(RepositoryPO repositoryPO) {
try {
ProcessBuilder repositoryInitializer =
new ProcessBuilder(
"sudo", "-u", GitConstant.GIT_USER_NAME, "git", "init", "--bare", repositorySavePath);
"sudo",
"-u",
GitConstant.GIT_USER_NAME,
"git",
"init",
"--bare",
repositorySavePath);
Process process = repositoryInitializer.start();
if (process.waitFor() != 0) {
throw new GenericException(
Expand Down
1 change: 0 additions & 1 deletion src/main/java/edu/cmipt/gcs/service/SshKeyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

import edu.cmipt.gcs.pojo.ssh.SshKeyPO;


public interface SshKeyService extends IService<SshKeyPO> {}
Loading

0 comments on commit 1e752b8

Please sign in to comment.