package ru.sape; import org.apache.commons.lang3.StringUtils; import java.net.URI; import java.util.*; public class SapePageLinks { private boolean showCode; @SuppressWarnings("unchecked") 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; } Map data = sapeConnection.getData(); if (data.containsKey("__sape_delimiter__")) { linkDelimiter = (String) data.get("__sape_delimiter__"); } if (data.containsKey(req)) { pageLinks = new ArrayList<>(((Map) data.get(req)).values()); } if (data.containsKey("__sape_new_url__")) { if (showCode) { Object newUrl = data.get("__sape_new_url__"); if (newUrl instanceof Map) { pageLinks = new ArrayList<>(((Map) newUrl).values()); } else { pageLinks = new ArrayList<>(Collections.singletonList((String) newUrl)); } } } this.showCode = showCode; } private String linkDelimiter = "."; private List pageLinks = new ArrayList<>(); public String render() { return render(-1); } public String render(int count) { StringBuilder s = new StringBuilder(); if (count < 0) { count = pageLinks.size(); } for (Iterator 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, ""); s.append(""); } return s.toString(); } }