%@ page import="com.xplorex.modules.linksmapping.LinkMap,
com.xplorex.modules.linksmapping.LinkMapsFactory"
%><%@ include file="/alias.jsp" %><%
if (alias == null) {
%><%
return;
}
final String link = request.getQueryString();
if(link != null && !link.toLowerCase().equals(link)) {
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", link.toLowerCase());
return;
}
final boolean DEBUG = false;
if (DEBUG) {
%>
<%=link%>
<%
}
if (link == null) {
%><%@ include file="404.jsp" %><%
return;
}
LinkMapsFactory linkMapsFactory = new LinkMapsFactory(alias);
if (link.matches("/page([0-9]+)\\.htm")) {
LinkMap[] maps = linkMapsFactory.search(new SearchCriteria[] {new SearchCriteria() {
public String getWhereClause() {
return "trueurl=?";
}
public int getParametersCount() {
return 1;
}
public String[] getKeywords() {
return new String[] {link};
}
public short getCriteriaType() {
return SearchCriteria.AND_TYPE;
}
}});
XContext context = XContext.getSiteContext(alias);
if (maps.length != 0 && !context.getBoolean("linkmap_accesswoalias", true)) {
%><%@ include file="404.jsp" %><%
} else {
if (maps.length > 0) {
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", maps[0].getFalseUrl());
return;
} else {
%>
<%
return;
}
}
return;
}
LinkMap trueUrlLinkMap = linkMapsFactory.getLinkByTrueUrl(link);
if (trueUrlLinkMap != null && link.equals(trueUrlLinkMap.getTrueUrl()) &&
(link.endsWith(".htm") || link.endsWith(".html"))) {
// TODO: ?
// response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
// response.setHeader("Location", trueUrlLinkMap.getFalseUrl());
// Logger.log("3 linkmap set header location = link:" + link + ", " + trueUrlLinkMap.getFalseUrl() + " -> " + trueUrlLinkMap.getTrueUrl());
// return;
}
LinkMap linkMap = linkMapsFactory.getLink(link);
if (linkMap == null) {
String redirUrl = searchRedirect(link, application);
if (redirUrl != null) {
if (redirUrl.endsWith(".xml")) {
response.addHeader("content-type", "text/xml");
InputStream is = application.getResourceAsStream(redirUrl);
if (is == null) {
%><%@ include file="404.jsp" %><%
return;
}
try {
OutputStream os = response.getOutputStream();
int read;
byte[] buff = new byte[1024];
while ((read = is.read(buff)) != -1) {
os.write(buff, 0, read);
}
} finally {
is.close();
}
} else {
%><%
}
} else {
if (link.endsWith(".html")) {
String path = File.separator + "sites" + File.separator + alias + File.separator + link;
String realPath = application.getRealPath(path);
File file = new File(realPath);
if (file.exists()) {
response.addHeader("content-type", "text/html");
InputStream is = application.getResourceAsStream(path);
if (is == null) {
%><%@ include file="404.jsp" %><%
return;
}
try {
OutputStream os = response.getOutputStream();
int read;
byte[] buff = new byte[1024];
while ((read = is.read(buff)) != -1) {
os.write(buff, 0, read);
}
} finally {
try {
is.close();
} catch (Exception e) {
// ignore it
}
}
} else {
%><%@ include file="/404.jsp" %><%
return;
}
}
%><%@ include file="404.jsp" %><%
}
return;
}
int count = 0;
String url = linkMap.getTrueUrl();
LinkMap currenlLinkMap;
while ((currenlLinkMap = linkMapsFactory.getLink(url)) != null && count <= 15) {
url = currenlLinkMap.getTrueUrl();
count++;
}
if (getCatalogLevelByUrl(link) > 1) {
request.setAttribute("setbase", "//"+request.getHeader("host")+"/");
}
Redirect redirect = prepareUrl(url);
if (redirect.isInclude) {
if (linkMap.getFalseUrl().endsWith(".xml")) {
response.addHeader("content-type", "text/xml");
}
%><%
} else if (url.endsWith(".php")) {
%><%@ include file="404.jsp" %><%
} else {
response.addHeader("content-type", redirect.contentType);
InputStream is = application.getResourceAsStream(redirect.url);
if (is == null) {
%><%@ include file="404.jsp" %><%
return;
}
try {
OutputStream os = response.getOutputStream();
int read;
byte[] buff = new byte[1024];
while ((read = is.read(buff)) != -1) {
os.write(buff, 0, read);
}
} finally {
is.close();
}
}
%><%!
private class MimeLinker {
private final String[][] MIME_TYPES = new String[][] {
new String[] {"xml","text/xml"},
new String[] {"txt","text/plain"},
new String[] {"htm","text/html"},
new String[] {"html","text/html"},
new String[] {"bmp","image/bmp"},
new String[] {"gif","image/gif"},
new String[] {"jpg","image/jpeg"},
new String[] {"png","image/png"},
new String[] {"psd","image/x-photoshop"},
new String[] {"zip","application/zip"},
new String[] {"rar","application/rar"},
new String[] {"pdf","application/pdf"}
};
//private final String MEME_DEFAULT = "application/unknown";
private String getMimeType(String fileName) {
int lastPoint = fileName.lastIndexOf('.');
String ext = fileName.substring(lastPoint + 1).toLowerCase();
for (int i = 0; i < MIME_TYPES.length; i++) {
String[] type = MIME_TYPES[i];
if (type[0].equals(ext)) {
return type[1];
}
}
return null;
}
}
private class Redirect {
private String url;
private String contentType;
private boolean isInclude;
public Redirect(String url) {
this(url, true);
}
public Redirect(String url, boolean isInclude) {
this.url = url;
this.isInclude = isInclude;
if (!isInclude) {
MimeLinker linker = new MimeLinker();
this.contentType = linker.getMimeType(url);
}
}
}
private Redirect prepareUrl(String url) {
if (url.matches("/page\\d+.htm")) {
return new Redirect("/main.jsp?p2=page.jsp&page=" + url.substring(5, url.length() - 4));
}
if (url.startsWith("/?")) {
return new Redirect("/main.jsp" + url.substring(1));
}
if (url.equals("/")) {
return new Redirect("/main.jsp");
}
int qIndex = url.indexOf('?');
boolean isJsp;
if (qIndex < 0) {
isJsp = url.endsWith(".jsp");
} else {
isJsp = (qIndex > 4) && ".jsp".equals(url.substring(qIndex-4,qIndex));
}
return new Redirect(url, isJsp);
}
private int getCatalogLevelByUrl(String url) {
int qpos = url.indexOf('?');
if (qpos < 0) {
return getCatalogLevelByPath(url);
}
return getCatalogLevelByPath(url.substring(0, qpos - 1));
}
private int getCatalogLevelByPath(String path) {
int lastPos = 0;
int pos;
int counter = 0;
while ((pos = path.indexOf('/', lastPos)) >= 0) {
lastPos = pos + 1;
counter++;
}
return counter;
}
private String searchRedirect(String link, ServletContext application) {
File file = new File(application.getRealPath(link));
if (file.exists()) return link;
return null;
}
%>