aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ru/sape/SapePageLinks.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2015-10-24 19:35:41 +0300
committerGravatar Vitaly Takmazov2015-10-24 19:35:41 +0300
commitaf80956ec669cf48ea6423959b5a4d8f16385d6f (patch)
tree3589c77814185cd8bf43408f46793aaf8917c27a /src/main/java/ru/sape/SapePageLinks.java
parent7a96f3f799e2e2358be246b3e9f0aa412ef28a2d (diff)
moving to Gradle
Diffstat (limited to 'src/main/java/ru/sape/SapePageLinks.java')
-rw-r--r--src/main/java/ru/sape/SapePageLinks.java95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/main/java/ru/sape/SapePageLinks.java b/src/main/java/ru/sape/SapePageLinks.java
new file mode 100644
index 00000000..498aeac0
--- /dev/null
+++ b/src/main/java/ru/sape/SapePageLinks.java
@@ -0,0 +1,95 @@
+package ru.sape;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import javax.servlet.http.Cookie;
+
+public class SapePageLinks {
+
+ private boolean showCode;
+
+ public SapePageLinks(SapeConnection sapeConnection, String sapeUser, String requestUri, Cookie[] cookies) {
+ this(sapeConnection, sapeUser, requestUri, cookies, false);
+ }
+
+ @SuppressWarnings("unchecked")
+ public SapePageLinks(SapeConnection sapeConnection, String sapeUser, String requestUri, Cookie[] cookies, boolean showCode) {
+ if (sapeUser.equals(getCookieValue(cookies, "sape_cookie"))) {
+ showCode = true;
+ }
+
+ Map<String, Object> data = sapeConnection.getData();
+
+ if (data.containsKey("__sape_delimiter__")) {
+ linkDelimiter = (String) data.get("__sape_delimiter__");
+ }
+
+ if (data.containsKey(requestUri)) {
+ pageLinks = new ArrayList<String>(((Map<Object, String>) data.get(requestUri)).values());
+ }
+
+ if (data.containsKey("__sape_new_url__")) {
+ if (showCode) {
+ Object newUrl = data.get("__sape_new_url__");
+
+ if (newUrl instanceof Map) {
+ pageLinks = new ArrayList<String>(((Map<Object, String>) newUrl).values());
+ } else {
+ pageLinks = new ArrayList<String>(Arrays.asList((String) newUrl));
+ }
+ }
+ }
+
+ this.showCode = showCode;
+ }
+ private String linkDelimiter = ".";
+ private List<String> pageLinks = new ArrayList<String>();
+
+ public String render() {
+ return render(-1);
+ }
+
+ public String render(int count) {
+ StringBuilder s = new StringBuilder();
+
+ if (count < 0) {
+ count = pageLinks.size();
+ }
+
+ for (Iterator<String> i = pageLinks.iterator(); i.hasNext() && count > 0; count--) {
+ if (s.length() > 0) {
+ s.append(linkDelimiter);
+ }
+
+ String l = i.next();
+
+ s.append(l);
+
+ i.remove();
+ }
+
+ if (showCode) {
+ s.insert(0, "<sape_noindex>");
+ s.append("</sape_noindex>");
+ }
+
+ return s.toString();
+ }
+
+ private static String getCookieValue(Cookie[] cookies, String name) {
+ if (cookies == null) {
+ return null;
+ }
+
+ for (Cookie cookie : cookies) {
+ if (cookie.getName().equals(name)) {
+ return cookie.getValue();
+ }
+ }
+
+ return null;
+ }
+}