1.1.42. fejezet, RestEasy szolgáltatás
Beküldte pzoli - 2021, március 15 - 4:35du
RestApplication.java
package hu.exprog.utils.rs.webservices; import java.util.HashSet; import java.util.Set; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; import hu.infokristaly.hirdetesek.back.rest.HirdetesEndpoint; @ApplicationPath("/rest") public class RestApplication extends Application { private Set<Object> singletons = new HashSet<Object>(); private Set<Class<?>> classes = new HashSet<Class<?>>(); public RestApplication(){ singletons.add(new HirdetesEndpoint()); } @Override public Set<Class<?>> getClasses() { return classes; } @Override public Set<Object> getSingletons() { return singletons; } }
HirdetesEndpoint.java
package hu.infokristaly.hirdetesek.back.rest; import java.util.List; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.HeaderParam; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import org.wildfly.security.auth.AuthenticationException; import hu.exprog.beecomposit.back.model.SystemUser; import hu.exprog.beecomposit.back.resources.AuthBackingBean; import hu.exprog.beecomposit.middle.services.SystemUserService; import hu.infokristaly.hirdetesek.back.domain.Hirdetes; import hu.infokristaly.hirdetesek.back.domain.Kategoria; import hu.infokristaly.hirdetesek.back.domain.Telepules; import hu.infokristaly.hirdetesek.middle.services.HirdetesService; import hu.infokristaly.hirdetesek.middle.services.KategoriaService; import hu.infokristaly.hirdetesek.middle.services.StatusService; import hu.infokristaly.hirdetesek.middle.services.TelepulesService; @RequestScoped @Path("/hirdetesek") @Produces({ "application/json; charset=UTF-8" }) @Consumes({ "application/json; charset=UTF-8" }) public class HirdetesEndpoint { @Inject private HirdetesService hirdetesService; @Inject private StatusService statusService; @Inject private KategoriaService kategoriaService; @Inject private SystemUserService systemUserService; @Inject private TelepulesService telepulesService; @Inject private AuthBackingBean authBean; @POST public Response create(@HeaderParam(value = "Username") String username, @HeaderParam(value = "Password") String password, final Hirdetes hirdetes) { try { password = authBean.getUserPassword(password); SystemUser user = new SystemUser(username, password); user = systemUserService.findByAuth(user); if (user == null) { throw new AuthenticationException("A felhasználó nem található"); } long count = hirdetesService.countActiveAdsByUser(user); if (count >= user.getActiveAdsMaxCount()) { throw new Exception("Elérte a hirdetések maximális számát."); } hirdetes.setId(null); hirdetes.setStatus(statusService.findById(statusService.STATUS_POSTED)); if (hirdetes.getKategoria() == null || hirdetes.getKategoria().getId() == null) { throw new Exception("A kategória nem lehet üres!"); } Kategoria kategoria = kategoriaService.findById(hirdetes.getKategoria().getId()); if (kategoria == null) { throw new Exception("Nincs ilyen kategória!"); } hirdetes.setKategoria(kategoria); hirdetes.setSystemUser(systemUserService.findByAuth(user)); if (hirdetes.getTelepules() == null || hirdetes.getTelepules().getId() == null) { throw new Exception("A település nem lehet üres!"); } Telepules telepules = telepulesService.findById(hirdetes.getTelepules().getId()); if (telepules == null) { throw new Exception("Nincs ilyen település!"); } hirdetes.setTelepules(telepules); hirdetes.setVersion(1L); hirdetesService.persist(hirdetes); } catch (Exception e) { return Response.serverError().status(Response.Status.BAD_REQUEST).header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN+"; charset=UTF-8").entity(e.getLocalizedMessage()).build(); } hirdetes.setSystemUser(null); return Response.ok(hirdetes).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON+"; charset=utf-8").build(); } @GET @Path("/{id:[0-9][0-9]*}") public Response findById(@PathParam("id") final Long id) { Hirdetes hirdetes = hirdetesService.findById(id); if (hirdetes == null) { return Response.status(Status.NOT_FOUND).build(); } hirdetes.setSystemUser(null); Response result = Response.ok(hirdetes).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON+"; charset=utf-8").build(); return result; } @GET @Path("/listAll") public List<Hirdetes> listAll(@QueryParam("start") final Integer startPosition, @QueryParam("max") final Integer maxResult) { final List<Hirdetes> hirdetesek = hirdetesService.findActiveAds(startPosition, maxResult, null, null, null); hirdetesek.forEach(f -> f.setSystemUser(null)); return hirdetesek; } @GET @Path("/osszesTelepules") public List<Telepules> osszesTelepules() { final List<Telepules> telepulesek = telepulesService.findAll(); return telepulesek; } @GET @Path("/osszesKategoria") public List<Kategoria> osszesKategoria() { final List<Kategoria> kategoriak = kategoriaService.findAll(); return kategoriak; } @DELETE @Path("/{id:[0-9][0-9]*}") public Response deleteById(@HeaderParam(value = "Username") String username, @HeaderParam(value = "Password") String password, @PathParam("id") final Long id) { password = authBean.getUserPassword(password); SystemUser systemUser = systemUserService.findByAuth(new SystemUser(username, password)); try { if (systemUser == null) { throw new AuthenticationException("A felhasználó nem található!"); } Hirdetes hirdetes = hirdetesService.findById(id); if (hirdetes == null) { return Response.noContent().build(); } if (hirdetes.getSystemUser() == null || !hirdetes.getSystemUser().equals(systemUser)) { throw new AuthenticationException( "A felhasználó nem egyezik a törölni kívánt hirdetésben szereplővel!"); } hirdetesService.remove(hirdetes); } catch (Exception ex) { return Response.serverError().status(Response.Status.BAD_REQUEST).entity(ex.getLocalizedMessage()).header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN+"; charset=utf-8").build(); } return Response.noContent().build(); } }
- A hozzászóláshoz be kell jelentkezni