mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 16:05:44 +01:00
Use try-with-resources
This commit is contained in:
@@ -6,12 +6,10 @@ import com.webcohesion.enunciate.metadata.rs.TypeHint;
|
||||
import sonia.scm.repository.Branches;
|
||||
import sonia.scm.repository.NamespaceAndName;
|
||||
import sonia.scm.repository.RepositoryException;
|
||||
import sonia.scm.repository.RepositoryManager;
|
||||
import sonia.scm.repository.RepositoryNotFoundException;
|
||||
import sonia.scm.repository.api.CommandNotSupportedException;
|
||||
import sonia.scm.repository.api.RepositoryService;
|
||||
import sonia.scm.repository.api.RepositoryServiceFactory;
|
||||
import sonia.scm.util.IOUtil;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@@ -24,13 +22,11 @@ import java.io.IOException;
|
||||
|
||||
public class BranchCollectionResource {
|
||||
|
||||
private final RepositoryManager manager;
|
||||
private final RepositoryServiceFactory servicefactory;
|
||||
private final BranchCollectionToDtoMapper branchCollectionToDtoMapper;
|
||||
|
||||
@Inject
|
||||
public BranchCollectionResource(RepositoryManager manager, RepositoryServiceFactory servicefactory, BranchCollectionToDtoMapper branchCollectionToDtoMapper) {
|
||||
this.manager = manager;
|
||||
public BranchCollectionResource(RepositoryServiceFactory servicefactory, BranchCollectionToDtoMapper branchCollectionToDtoMapper) {
|
||||
this.servicefactory = servicefactory;
|
||||
this.branchCollectionToDtoMapper = branchCollectionToDtoMapper;
|
||||
}
|
||||
@@ -46,20 +42,13 @@ public class BranchCollectionResource {
|
||||
@ResponseCode(code = 500, condition = "internal server error")
|
||||
})
|
||||
public Response getAll(@PathParam("namespace") String namespace, @PathParam("name") String name) throws IOException, RepositoryException {
|
||||
RepositoryService repositoryService;
|
||||
try {
|
||||
repositoryService = servicefactory.create(new NamespaceAndName(namespace, name));
|
||||
} catch (RepositoryNotFoundException e) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
|
||||
try {
|
||||
try (RepositoryService repositoryService = servicefactory.create(new NamespaceAndName(namespace, name))) {
|
||||
Branches branches = repositoryService.getBranchesCommand().getBranches();
|
||||
return Response.ok(branchCollectionToDtoMapper.map(namespace, name, branches.getBranches())).build();
|
||||
} catch (CommandNotSupportedException ex) {
|
||||
return Response.status(Response.Status.BAD_REQUEST).build();
|
||||
} finally {
|
||||
IOUtil.close(repositoryService);
|
||||
} catch (RepositoryNotFoundException e) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,12 +7,10 @@ import com.webcohesion.enunciate.metadata.rs.TypeHint;
|
||||
import sonia.scm.repository.Branches;
|
||||
import sonia.scm.repository.NamespaceAndName;
|
||||
import sonia.scm.repository.RepositoryException;
|
||||
import sonia.scm.repository.RepositoryManager;
|
||||
import sonia.scm.repository.RepositoryNotFoundException;
|
||||
import sonia.scm.repository.api.CommandNotSupportedException;
|
||||
import sonia.scm.repository.api.RepositoryService;
|
||||
import sonia.scm.repository.api.RepositoryServiceFactory;
|
||||
import sonia.scm.util.IOUtil;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import javax.ws.rs.GET;
|
||||
@@ -24,13 +22,11 @@ import java.io.IOException;
|
||||
|
||||
public class BranchResource {
|
||||
|
||||
private final RepositoryManager manager;
|
||||
private final RepositoryServiceFactory servicefactory;
|
||||
private final BranchToBranchDtoMapper branchToDtoMapper;
|
||||
|
||||
@Inject
|
||||
public BranchResource(RepositoryManager manager, RepositoryServiceFactory servicefactory, BranchToBranchDtoMapper branchToDtoMapper) {
|
||||
this.manager = manager;
|
||||
public BranchResource(RepositoryServiceFactory servicefactory, BranchToBranchDtoMapper branchToDtoMapper) {
|
||||
this.servicefactory = servicefactory;
|
||||
this.branchToDtoMapper = branchToDtoMapper;
|
||||
}
|
||||
@@ -47,14 +43,7 @@ public class BranchResource {
|
||||
@ResponseCode(code = 500, condition = "internal server error")
|
||||
})
|
||||
public Response get(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("branch") String branchName) throws IOException, RepositoryException {
|
||||
RepositoryService repositoryService;
|
||||
try {
|
||||
repositoryService = servicefactory.create(new NamespaceAndName(namespace, name));
|
||||
} catch (RepositoryNotFoundException e) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
|
||||
try {
|
||||
try (RepositoryService repositoryService = servicefactory.create(new NamespaceAndName(namespace, name))) {
|
||||
Branches branches = repositoryService.getBranchesCommand().getBranches();
|
||||
return branches.getBranches()
|
||||
.stream()
|
||||
@@ -66,8 +55,8 @@ public class BranchResource {
|
||||
.build();
|
||||
} catch (CommandNotSupportedException ex) {
|
||||
return Response.status(Response.Status.BAD_REQUEST).build();
|
||||
} finally {
|
||||
IOUtil.close(repositoryService);
|
||||
} catch (RepositoryNotFoundException e) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user