Java tutorial
/* * Copyright 2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.curly.artifact.web; import io.curly.artifact.model.Artifact; import io.curly.artifact.model.ArtifactReqBody; import io.curly.artifact.service.ArtifactCommand; import io.curly.artifact.web.hateoas.ArtifactResource; import io.curly.artifact.web.hateoas.ArtifactResourceAssembler; import io.curly.commons.github.GitHubAuthentication; import io.curly.commons.github.User; import io.curly.commons.web.BadRequestException; import io.curly.commons.web.ModelErrors; import io.curly.commons.web.hateoas.MediaTypes; import lombok.extern.slf4j.Slf4j; import org.bson.types.ObjectId; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; import org.springframework.data.rest.webmvc.ResourceNotFoundException; import org.springframework.data.web.PageableDefault; import org.springframework.data.web.PagedResourcesAssembler; import org.springframework.hateoas.ExposesResourceFor; import org.springframework.hateoas.PagedResources; import org.springframework.http.HttpEntity; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.security.oauth2.common.exceptions.UnauthorizedUserException; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.request.async.DeferredResult; import rx.Observable; import javax.validation.Valid; import static io.curly.commons.rx.RxResult.defer; import static org.springframework.http.HttpStatus.*; import static org.springframework.web.bind.annotation.RequestMethod.*; /** * @author Joo Pedro Evangelista */ @Slf4j @RestController @ExposesResourceFor(Artifact.class) @RequestMapping("/artifacts") public class ArtifactResourceController { private final ArtifactCommand artifactService; private final ArtifactResourceAssembler assembler; @Autowired ArtifactResourceController(ArtifactCommand artifactService, ArtifactResourceAssembler assembler) { this.artifactService = artifactService; this.assembler = assembler; } /** * Process a page of Artifacts into a HAL representation or return 404 if not found or was null value * * @param pageable acquired from url parameters * @return Observable of a HttpEntity of a Paged Resources of Artifacts */ @RequestMapping(method = GET, produces = MediaTypes.HAL_JSON) public DeferredResult<HttpEntity<PagedResources<ArtifactResource>>> artifactResources( @PageableDefault(20) Pageable pageable, PagedResourcesAssembler<Artifact> assembler) { return defer(artifactService.findAllByPage(pageable) .map(o -> o.<ResourceNotFoundException>orElseThrow(ResourceNotFoundException::new)) .map(artifacts -> assembler.toResource(artifacts, this.assembler)).map(ResponseEntity::ok)); } /** * @param pageable current pagination * @param user current logged user * @param assembler hateoas assember * @return if found a page of resources if not 404 and if no user is found a 401 */ @RequestMapping(value = "/owned", method = GET, produces = MediaTypes.HAL_JSON) public DeferredResult<HttpEntity<PagedResources<ArtifactResource>>> artifactsOwned( @PageableDefault(20) Pageable pageable, @GitHubAuthentication User user, PagedResourcesAssembler<Artifact> assembler) { if (user == null) { throw new UnauthorizedUserException("No user found!"); } return defer(artifactService.findAllOwned(pageable, user) .map(o -> o.<ResourceNotFoundException>orElseThrow(ResourceNotFoundException::new)) .map(artifacts -> assembler.toResource(artifacts, this.assembler)).map(ResponseEntity::ok)); } /** * Get a artifactory and convert into a Resource representation * * @param id the id to look for * @return resource of artifact */ @RequestMapping(value = "/{id}", method = GET, produces = MediaTypes.HAL_JSON) public DeferredResult<HttpEntity<ArtifactResource>> artifactResource(@PathVariable("id") String id) { if (!ObjectId.isValid(id)) throw new BadRequestException("Provided id " + id + " is not valid!"); return defer(Observable.just(artifactService.findOne(id)) .map(artifact -> artifact.<ResourceNotFoundException>orElseThrow(ResourceNotFoundException::new)) .map(assembler::toResource).map(ResponseEntity::ok)); } /** * @param body json body * @param user authentication user * @param bindingResult errors results of artifact * @return no content if saved, bad request if binding results has errors */ @RequestMapping(method = { POST }, consumes = MediaType.APPLICATION_JSON_VALUE) public DeferredResult<HttpEntity<?>> saveResource(@Valid @RequestBody ArtifactReqBody body, @GitHubAuthentication User user, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return defer(Observable.just(new ResponseEntity<>(new ModelErrors(bindingResult), BAD_REQUEST))); } artifactService.save(body.toArtifact(user), user); return defer(Observable.just(new ResponseEntity<>(CREATED))); } /** * deletes a artifact based on the id * * @param id the id to look for * @param user authenticated user * @return no content if deleted */ @RequestMapping(value = "/{id}", method = DELETE) public DeferredResult<HttpEntity<?>> deleteResource(@PathVariable("id") String id, @GitHubAuthentication User user) { if (!ObjectId.isValid(id)) throw new BadRequestException("Provided id " + id + " is not valid!"); artifactService.delete(id, user); return defer(Observable.just(new ResponseEntity<>(NO_CONTENT))); } }