Use try-with-resources

This commit is contained in:
René Pfeuffer
2018-08-07 16:52:40 +02:00
parent 42aac06f0a
commit bf0c559a7d
2 changed files with 8 additions and 30 deletions

View File

@@ -6,12 +6,10 @@ import com.webcohesion.enunciate.metadata.rs.TypeHint;
import sonia.scm.repository.Branches; import sonia.scm.repository.Branches;
import sonia.scm.repository.NamespaceAndName; import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.RepositoryException; import sonia.scm.repository.RepositoryException;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryNotFoundException; import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.api.CommandNotSupportedException; import sonia.scm.repository.api.CommandNotSupportedException;
import sonia.scm.repository.api.RepositoryService; import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory; import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.util.IOUtil;
import sonia.scm.web.VndMediaType; import sonia.scm.web.VndMediaType;
import javax.inject.Inject; import javax.inject.Inject;
@@ -24,13 +22,11 @@ import java.io.IOException;
public class BranchCollectionResource { public class BranchCollectionResource {
private final RepositoryManager manager;
private final RepositoryServiceFactory servicefactory; private final RepositoryServiceFactory servicefactory;
private final BranchCollectionToDtoMapper branchCollectionToDtoMapper; private final BranchCollectionToDtoMapper branchCollectionToDtoMapper;
@Inject @Inject
public BranchCollectionResource(RepositoryManager manager, RepositoryServiceFactory servicefactory, BranchCollectionToDtoMapper branchCollectionToDtoMapper) { public BranchCollectionResource(RepositoryServiceFactory servicefactory, BranchCollectionToDtoMapper branchCollectionToDtoMapper) {
this.manager = manager;
this.servicefactory = servicefactory; this.servicefactory = servicefactory;
this.branchCollectionToDtoMapper = branchCollectionToDtoMapper; this.branchCollectionToDtoMapper = branchCollectionToDtoMapper;
} }
@@ -46,20 +42,13 @@ public class BranchCollectionResource {
@ResponseCode(code = 500, condition = "internal server error") @ResponseCode(code = 500, condition = "internal server error")
}) })
public Response getAll(@PathParam("namespace") String namespace, @PathParam("name") String name) throws IOException, RepositoryException { public Response getAll(@PathParam("namespace") String namespace, @PathParam("name") String name) throws IOException, RepositoryException {
RepositoryService repositoryService; try (RepositoryService repositoryService = servicefactory.create(new NamespaceAndName(namespace, name))) {
try {
repositoryService = servicefactory.create(new NamespaceAndName(namespace, name));
} catch (RepositoryNotFoundException e) {
return Response.status(Response.Status.NOT_FOUND).build();
}
try {
Branches branches = repositoryService.getBranchesCommand().getBranches(); Branches branches = repositoryService.getBranchesCommand().getBranches();
return Response.ok(branchCollectionToDtoMapper.map(namespace, name, branches.getBranches())).build(); return Response.ok(branchCollectionToDtoMapper.map(namespace, name, branches.getBranches())).build();
} catch (CommandNotSupportedException ex) { } catch (CommandNotSupportedException ex) {
return Response.status(Response.Status.BAD_REQUEST).build(); return Response.status(Response.Status.BAD_REQUEST).build();
} finally { } catch (RepositoryNotFoundException e) {
IOUtil.close(repositoryService); return Response.status(Response.Status.NOT_FOUND).build();
} }
} }
} }

View File

@@ -7,12 +7,10 @@ import com.webcohesion.enunciate.metadata.rs.TypeHint;
import sonia.scm.repository.Branches; import sonia.scm.repository.Branches;
import sonia.scm.repository.NamespaceAndName; import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.RepositoryException; import sonia.scm.repository.RepositoryException;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryNotFoundException; import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.api.CommandNotSupportedException; import sonia.scm.repository.api.CommandNotSupportedException;
import sonia.scm.repository.api.RepositoryService; import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory; import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.util.IOUtil;
import sonia.scm.web.VndMediaType; import sonia.scm.web.VndMediaType;
import javax.ws.rs.GET; import javax.ws.rs.GET;
@@ -24,13 +22,11 @@ import java.io.IOException;
public class BranchResource { public class BranchResource {
private final RepositoryManager manager;
private final RepositoryServiceFactory servicefactory; private final RepositoryServiceFactory servicefactory;
private final BranchToBranchDtoMapper branchToDtoMapper; private final BranchToBranchDtoMapper branchToDtoMapper;
@Inject @Inject
public BranchResource(RepositoryManager manager, RepositoryServiceFactory servicefactory, BranchToBranchDtoMapper branchToDtoMapper) { public BranchResource(RepositoryServiceFactory servicefactory, BranchToBranchDtoMapper branchToDtoMapper) {
this.manager = manager;
this.servicefactory = servicefactory; this.servicefactory = servicefactory;
this.branchToDtoMapper = branchToDtoMapper; this.branchToDtoMapper = branchToDtoMapper;
} }
@@ -47,14 +43,7 @@ public class BranchResource {
@ResponseCode(code = 500, condition = "internal server error") @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 { public Response get(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("branch") String branchName) throws IOException, RepositoryException {
RepositoryService repositoryService; try (RepositoryService repositoryService = servicefactory.create(new NamespaceAndName(namespace, name))) {
try {
repositoryService = servicefactory.create(new NamespaceAndName(namespace, name));
} catch (RepositoryNotFoundException e) {
return Response.status(Response.Status.NOT_FOUND).build();
}
try {
Branches branches = repositoryService.getBranchesCommand().getBranches(); Branches branches = repositoryService.getBranchesCommand().getBranches();
return branches.getBranches() return branches.getBranches()
.stream() .stream()
@@ -66,8 +55,8 @@ public class BranchResource {
.build(); .build();
} catch (CommandNotSupportedException ex) { } catch (CommandNotSupportedException ex) {
return Response.status(Response.Status.BAD_REQUEST).build(); return Response.status(Response.Status.BAD_REQUEST).build();
} finally { } catch (RepositoryNotFoundException e) {
IOUtil.close(repositoryService); return Response.status(Response.Status.NOT_FOUND).build();
} }
} }
} }