From fb6d7f3b9d34032ec7ed32295b13dd556088989f Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 15 Mar 2019 14:11:47 +0300 Subject: Allow parameters in sape links --- .../juick/server/www/controllers/MessagesWWW.java | 40 ++++++++++------------ src/main/java/ru/sape/Sape.java | 4 ++- src/main/java/ru/sape/SapePageLinks.java | 15 ++++---- 3 files changed, 30 insertions(+), 29 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/juick/server/www/controllers/MessagesWWW.java b/src/main/java/com/juick/server/www/controllers/MessagesWWW.java index 1c69db32..6de8d51d 100644 --- a/src/main/java/com/juick/server/www/controllers/MessagesWWW.java +++ b/src/main/java/com/juick/server/www/controllers/MessagesWWW.java @@ -43,7 +43,9 @@ import ru.sape.Sape; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import java.io.IOException; +import java.net.URI; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -182,10 +184,9 @@ public class MessagesWWW { } model.addAttribute("nextpage", nextpage); } - UriComponents builder = ServletUriComponentsBuilder.fromCurrentRequestUri().build(); - String queryString = builder.getQuery(); - String requestURI = builder.toUri().getPath(); - if (sape.isPresent() && visitor.isAnonymous() && queryString == null) { + UriComponents builder = ServletUriComponentsBuilder.fromCurrentRequest().build(); + URI requestURI = builder.toUri(); + if (sape.isPresent() && visitor.isAnonymous()) { String links = sape.get().getPageLinks(requestURI, sapeCookie).render(); model.addAttribute("links", links); } @@ -295,17 +296,16 @@ public class MessagesWWW { nextpage += "&show=" + paramShow; } if (paramSearch != null) { - nextpage += "&search=" + URLEncoder.encode(paramSearch, CharEncoding.UTF_8); + nextpage += "&search=" + URLEncoder.encode(paramSearch, StandardCharsets.UTF_8); } if (paramTag != null) { - nextpage += "&tag=" + URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8); + nextpage += "&tag=" + URLEncoder.encode(paramTag.getName(), StandardCharsets.UTF_8); } model.addAttribute("nextpage", nextpage); } - UriComponents builder = ServletUriComponentsBuilder.fromCurrentRequestUri().build(); - String queryString = builder.getQuery(); - String requestURI = builder.toUri().getPath(); - if (sape.isPresent() && visitor.isAnonymous() && queryString == null) { + UriComponents builder = ServletUriComponentsBuilder.fromCurrentRequest().build(); + URI requestURI = builder.toUri(); + if (sape.isPresent() && visitor.isAnonymous()) { String links = sape.get().getPageLinks(requestURI, sapeCookie).render(); model.addAttribute("links", links); } @@ -313,7 +313,7 @@ public class MessagesWWW { } @GetMapping("/{uname}/tags") - protected String doGetTags(@PathVariable String uname, ModelMap model) throws IOException { + protected String doGetTags(@PathVariable String uname, ModelMap model) { com.juick.User user = userService.getUserByName(uname); com.juick.User visitor = UserUtils.getCurrentUser(); if (visitor.isBanned()) { @@ -332,7 +332,7 @@ public class MessagesWWW { } @GetMapping("/{uname}/friends") - protected String doGetFriends(@PathVariable String uname, ModelMap model) throws IOException { + protected String doGetFriends(@PathVariable String uname, ModelMap model) { com.juick.User user = userService.getUserByName(uname); com.juick.User visitor = UserUtils.getCurrentUser(); if (visitor.isBanned()) { @@ -444,13 +444,12 @@ public class MessagesWWW { model.addAttribute("isSubscribed", tagService.isSubscribed(visitor, paramTag)); model.addAttribute("isInBL", tagService.isInBL(visitor, paramTag)); if (mids.size() >= 20) { - String nextpage = "/tag/" + URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8) + "?before=" + mids.get(mids.size() - 1); + String nextpage = "/tag/" + URLEncoder.encode(paramTag.getName(), StandardCharsets.UTF_8) + "?before=" + mids.get(mids.size() - 1); model.addAttribute("nextpage", nextpage); } - UriComponents builder = ServletUriComponentsBuilder.fromCurrentRequestUri().build(); - String queryString = builder.getQuery(); - String requestURI = builder.toUri().getPath(); - if (sape.isPresent() && visitor.isAnonymous() && queryString == null) { + UriComponents builder = ServletUriComponentsBuilder.fromCurrentRequest().build(); + URI requestURI = builder.toUri(); + if (sape.isPresent() && visitor.isAnonymous()) { String links = sape.get().getPageLinks(requestURI, sapeCookie).render(); model.addAttribute("links", links); } @@ -591,10 +590,9 @@ public class MessagesWWW { } model.addAttribute("replies", replies); model.addAttribute("showAdv", visitor.isAnonymous()); - UriComponents builder = ServletUriComponentsBuilder.fromCurrentRequestUri().build(); - String queryString = builder.getQuery(); - String requestURI = builder.toUri().getPath(); - if (sape.isPresent() && visitor.isAnonymous() && queryString == null) { + UriComponents builder = ServletUriComponentsBuilder.fromCurrentRequest().build(); + URI requestURI = builder.toUri(); + if (sape.isPresent() && visitor.isAnonymous()) { String links = sape.get().getPageLinks(requestURI, sapeCookie).render(); model.addAttribute("links", links); } diff --git a/src/main/java/ru/sape/Sape.java b/src/main/java/ru/sape/Sape.java index 38577c45..a94bcc62 100644 --- a/src/main/java/ru/sape/Sape.java +++ b/src/main/java/ru/sape/Sape.java @@ -3,6 +3,8 @@ */ package ru.sape; +import java.net.URI; + public class Sape { private final String sapeUser; @@ -17,7 +19,7 @@ public class Sape { } public boolean debug = false; - public SapePageLinks getPageLinks(String requestUri, String cookie) { + public SapePageLinks getPageLinks(URI requestUri, String cookie) { return new SapePageLinks(sapePageLinkConnection, sapeUser, requestUri, cookie, debug); } } diff --git a/src/main/java/ru/sape/SapePageLinks.java b/src/main/java/ru/sape/SapePageLinks.java index e89b4e71..77715aea 100644 --- a/src/main/java/ru/sape/SapePageLinks.java +++ b/src/main/java/ru/sape/SapePageLinks.java @@ -1,17 +1,18 @@ package ru.sape; +import org.apache.commons.lang3.StringUtils; + +import java.net.URI; import java.util.*; public class SapePageLinks { private boolean showCode; - public SapePageLinks(SapeConnection sapeConnection, String sapeUser, String requestUri, String sapeCookie) { - this(sapeConnection, sapeUser, requestUri, sapeCookie, false); - } - @SuppressWarnings("unchecked") - public SapePageLinks(SapeConnection sapeConnection, String sapeUser, String requestUri, String sapeCookie, boolean showCode) { + public SapePageLinks(SapeConnection sapeConnection, String sapeUser, URI request, String sapeCookie, boolean showCode) { + String req = StringUtils.isNotEmpty(request.getQuery()) ? request.getPath() + "?" + request.getQuery() + : request.getPath(); if (sapeUser.equals(sapeCookie)) { showCode = true; } @@ -22,8 +23,8 @@ public class SapePageLinks { linkDelimiter = (String) data.get("__sape_delimiter__"); } - if (data.containsKey(requestUri)) { - pageLinks = new ArrayList<>(((Map) data.get(requestUri)).values()); + if (data.containsKey(req)) { + pageLinks = new ArrayList<>(((Map) data.get(req)).values()); } if (data.containsKey("__sape_new_url__")) { -- cgit v1.2.3