From 8cbbaca653712802eff744b955b1eb43b53c2ccf Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Tue, 10 Sep 2024 13:13:24 +0200 Subject: [PATCH] fix warnings, mainly unused imports there are 3 remaining but that's for later --- .../java/eu/m724/talkpages/IndexResource.java | 16 +++--------- .../eu/m724/talkpages/orm/entity/Page.java | 2 -- .../orm/generator/PathGenerator.java | 1 - .../eu/m724/talkpages/page/EditResource.java | 8 +----- .../eu/m724/talkpages/page/PageResource.java | 1 - .../talkpages/page/action/ActionResource.java | 26 +++++-------------- .../talkpages/page/action/ActionService.java | 4 +-- .../page/action/DuplicateException.java | 11 ++++++++ .../action/UnacceptableDataException.java | 11 ++++++++ 9 files changed, 36 insertions(+), 44 deletions(-) create mode 100644 src/main/java/eu/m724/talkpages/page/action/DuplicateException.java create mode 100644 src/main/java/eu/m724/talkpages/page/action/UnacceptableDataException.java diff --git a/src/main/java/eu/m724/talkpages/IndexResource.java b/src/main/java/eu/m724/talkpages/IndexResource.java index 2c2eab8..0854a4f 100644 --- a/src/main/java/eu/m724/talkpages/IndexResource.java +++ b/src/main/java/eu/m724/talkpages/IndexResource.java @@ -1,24 +1,16 @@ package eu.m724.talkpages; -import eu.m724.talkpages.orm.entity.Account; -import eu.m724.talkpages.orm.entity.Page; -import eu.m724.talkpages.orm.entity.PageRevision; -import io.quarkus.hibernate.orm.panache.Panache; import io.quarkus.qute.CheckedTemplate; import io.quarkus.qute.TemplateInstance; import jakarta.inject.Inject; -import jakarta.transaction.Transactional; -import jakarta.ws.rs.*; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.QueryParam; import jakarta.ws.rs.core.MediaType; -import jakarta.ws.rs.core.MultivaluedMap; import jakarta.ws.rs.core.Response; -import org.eclipse.microprofile.config.inject.ConfigProperty; -import org.jboss.resteasy.reactive.RestResponse; import org.jboss.resteasy.reactive.RestResponse.Status; -import java.net.URI; -import java.util.Random; - @Path("/") public class IndexResource { @Inject diff --git a/src/main/java/eu/m724/talkpages/orm/entity/Page.java b/src/main/java/eu/m724/talkpages/orm/entity/Page.java index 5232509..a19b4d7 100644 --- a/src/main/java/eu/m724/talkpages/orm/entity/Page.java +++ b/src/main/java/eu/m724/talkpages/orm/entity/Page.java @@ -1,9 +1,7 @@ package eu.m724.talkpages.orm.entity; -import io.quarkus.hibernate.orm.panache.PanacheEntity; import io.quarkus.hibernate.orm.panache.PanacheEntityBase; import jakarta.persistence.*; -import jakarta.transaction.Transactional; import org.hibernate.annotations.GenericGenerator; import java.util.ArrayList; diff --git a/src/main/java/eu/m724/talkpages/orm/generator/PathGenerator.java b/src/main/java/eu/m724/talkpages/orm/generator/PathGenerator.java index edebc31..206be2b 100644 --- a/src/main/java/eu/m724/talkpages/orm/generator/PathGenerator.java +++ b/src/main/java/eu/m724/talkpages/orm/generator/PathGenerator.java @@ -2,7 +2,6 @@ package eu.m724.talkpages.orm.generator; import eu.m724.talkpages.orm.entity.Page; import org.hibernate.HibernateException; -import org.hibernate.annotations.IdGeneratorType; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.id.IdentifierGenerator; diff --git a/src/main/java/eu/m724/talkpages/page/EditResource.java b/src/main/java/eu/m724/talkpages/page/EditResource.java index b6ef196..1d602d6 100644 --- a/src/main/java/eu/m724/talkpages/page/EditResource.java +++ b/src/main/java/eu/m724/talkpages/page/EditResource.java @@ -1,11 +1,8 @@ package eu.m724.talkpages.page; -import eu.m724.talkpages.RedirectService; import eu.m724.talkpages.orm.entity.Page; -import eu.m724.talkpages.orm.entity.PageRevision; import io.quarkus.qute.CheckedTemplate; import io.quarkus.qute.TemplateInstance; -import jakarta.inject.Inject; import jakarta.ws.rs.*; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; @@ -13,9 +10,6 @@ import jakarta.ws.rs.core.Response; @Path("/edit") @Produces(MediaType.TEXT_HTML) public class EditResource { - @Inject - RedirectService redirectService; - @CheckedTemplate public static class Templates { public static native TemplateInstance edit(Page page, String content, String name); @@ -25,7 +19,7 @@ public class EditResource { @GET @Path("/") public Response editPageBlank() { - return Response.status(Response.Status.NOT_FOUND).entity(Templates.create("", "198.51.100.42")).build(); + return Response.ok().entity(Templates.create("", "198.51.100.42")).build(); } @GET diff --git a/src/main/java/eu/m724/talkpages/page/PageResource.java b/src/main/java/eu/m724/talkpages/page/PageResource.java index c9b66bb..21b720f 100644 --- a/src/main/java/eu/m724/talkpages/page/PageResource.java +++ b/src/main/java/eu/m724/talkpages/page/PageResource.java @@ -1,6 +1,5 @@ package eu.m724.talkpages.page; -import eu.m724.talkpages.TemplateExtensions; import eu.m724.talkpages.RedirectService; import eu.m724.talkpages.orm.entity.Page; import eu.m724.talkpages.orm.entity.PageRevision; diff --git a/src/main/java/eu/m724/talkpages/page/action/ActionResource.java b/src/main/java/eu/m724/talkpages/page/action/ActionResource.java index df8a739..aa120a1 100644 --- a/src/main/java/eu/m724/talkpages/page/action/ActionResource.java +++ b/src/main/java/eu/m724/talkpages/page/action/ActionResource.java @@ -3,7 +3,6 @@ package eu.m724.talkpages.page.action; import eu.m724.talkpages.RedirectService; import eu.m724.talkpages.orm.entity.Account; import eu.m724.talkpages.orm.entity.Page; -import eu.m724.talkpages.orm.entity.PageRevision; import jakarta.inject.Inject; import jakarta.transaction.Transactional; import jakarta.ws.rs.Consumes; @@ -46,27 +45,16 @@ public class ActionResource { return redirectService.page(page) .cookie(new NewCookie.Builder("prefilledContent").value("").maxAge(0).build()) .status(RestResponse.Status.SEE_OTHER).build(); - } catch (IllegalArgumentException e) { // TODO I could reduce all this code + } catch (UnacceptableDataException | DuplicateException e) { // TODO I could reduce all this code // illegal title String encodedMessage = URLEncoder.encode(e.getMessage(), StandardCharsets.UTF_8); - return Response - .temporaryRedirect(URI.create("/edit/" + title + "?errorType=1&error=" + encodedMessage)) - .cookie(new NewCookie.Builder("prefilledContent").path("/edit/" + title).value(content).build()) // TODO find a better way - .status(Response.Status.SEE_OTHER).build(); - } catch (IllegalStateException e) { - // page already exists - String encodedMessage = URLEncoder.encode(e.getMessage(), StandardCharsets.UTF_8); - return Response - .temporaryRedirect(URI.create("/edit/" + title + "?errorType=2&error=" + encodedMessage)) - .cookie(new NewCookie.Builder("prefilledContent").path("/edit/" + title).value(content).build()) - .status(Response.Status.SEE_OTHER).build(); - } catch (Exception e) { - e.printStackTrace(); - return Response - .temporaryRedirect(URI.create("/edit/" + title + "?errorType=0&error=Server+error")) - .cookie(new NewCookie.Builder("prefilledContent").path("/edit/" + title).value(content).build()) - .status(Response.Status.SEE_OTHER).build(); + int errorType = e instanceof UnacceptableDataException ? 1 : 2; + return Response + .temporaryRedirect(URI.create("/edit/%s?errorType=%d&error=%s".formatted(title, errorType, encodedMessage))) + .cookie(new NewCookie.Builder("prefilledContent").path("/edit/" + title).value(content).build()) + .status(Response.Status.SEE_OTHER).build(); + // TODO find a better, more concise way. Also maybe create a different type for each error. And consider if I should catch all Exceptions } } diff --git a/src/main/java/eu/m724/talkpages/page/action/ActionService.java b/src/main/java/eu/m724/talkpages/page/action/ActionService.java index d3beb58..884c614 100644 --- a/src/main/java/eu/m724/talkpages/page/action/ActionService.java +++ b/src/main/java/eu/m724/talkpages/page/action/ActionService.java @@ -12,9 +12,9 @@ public class ActionService { Page createPage(String title, String content, Account account) { // title and content is sanitized so only prohibit if necessary if (title.contains("/")) { - throw new IllegalArgumentException("Title cannot contain slashes (/). Those are used for sub-pages."); + throw new UnacceptableDataException("Title cannot contain slashes (/). Those are used for sub-pages."); } else if (Page.findByTitle(title) != null) { - throw new IllegalStateException("Page already exists, I made you edit it."); + throw new DuplicateException("Page already exists, I made you edit it."); } Page page = new Page(title); diff --git a/src/main/java/eu/m724/talkpages/page/action/DuplicateException.java b/src/main/java/eu/m724/talkpages/page/action/DuplicateException.java new file mode 100644 index 0000000..ce6b1e5 --- /dev/null +++ b/src/main/java/eu/m724/talkpages/page/action/DuplicateException.java @@ -0,0 +1,11 @@ +package eu.m724.talkpages.page.action; + +/** + * Page you tried to create already exists
+ * TODO find a better name + */ +public class DuplicateException extends RuntimeException { + public DuplicateException(String message) { + super(message); + } +} diff --git a/src/main/java/eu/m724/talkpages/page/action/UnacceptableDataException.java b/src/main/java/eu/m724/talkpages/page/action/UnacceptableDataException.java new file mode 100644 index 0000000..0041200 --- /dev/null +++ b/src/main/java/eu/m724/talkpages/page/action/UnacceptableDataException.java @@ -0,0 +1,11 @@ +package eu.m724.talkpages.page.action; + +/** + * Title or content or other data are unacceptable, don't fit constraints.
+ * TODO find a better name + */ +public class UnacceptableDataException extends RuntimeException { + public UnacceptableDataException(String message) { + super(message); + } +}