<%-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --%><%@ page import="javax.jcr.Repository, org.apache.jackrabbit.j2ee.RepositoryAccessServlet, org.apache.jackrabbit.util.Text, javax.jcr.Session, javax.jcr.SimpleCredentials, javax.jcr.query.Query, javax.jcr.query.RowIterator, java.text.NumberFormat, javax.jcr.query.Row, javax.jcr.Node, java.net.URLEncoder, java.text.SimpleDateFormat, java.text.DateFormat, java.util.List, java.util.ArrayList, java.util.Iterator, javax.jcr.Value, javax.jcr.RepositoryException"%> <%@ page contentType="text/html;charset=UTF-8" %><% Repository rep; Session jcrSession; try { rep = RepositoryAccessServlet.getRepository(pageContext.getServletContext()); jcrSession = rep.login(new SimpleCredentials("anonymous", "".toCharArray())); } catch (Throwable e) { %>Error while accessing the repository: <%= Text.encodeIllegalXMLCharacters(e.getMessage()) %>
<% %>Check the configuration or use the easy setup wizard.<% return; } try { String wspName = jcrSession.getWorkspace().getName(); String q = request.getParameter("q"); if (q == null) { q = ""; } else { q = new String(q.getBytes("ISO-8859-1"), "UTF-8"); } if (request.getParameter("as_q") != null) { q += " " + new String(request.getParameter("as_q").getBytes("ISO-8859-1"), "UTF-8"); } String executedIn = ""; String queryTerms = ""; String totalResults = ""; long from = 0; long to = 10; long total = 0; long maxPage = 0; long minPage = 0; long currentPageIndex = 0; List indexes = new ArrayList(); RowIterator rows = null; String suggestedQuery = null; if (q != null && q.length() > 0) { String stmt; if (q.startsWith("related:")) { String path = q.substring("related:".length()); stmt = "//element(*, nt:file)[rep:similar(jcr:content, '" + path + "/jcr:content')]/rep:excerpt(.)"; queryTerms = "similar to " + Text.encodeIllegalXMLCharacters(path) + ""; } else { queryTerms = "for " + Text.encodeIllegalXMLCharacters(q) + ""; q = q.replaceAll("'", "''"); stmt = "//element(*, nt:file)[jcr:contains(jcr:content, '" + q + "')]/rep:excerpt(.)"; } Query query = jcrSession.getWorkspace().getQueryManager().createQuery(stmt, Query.XPATH); long time = System.currentTimeMillis(); rows = query.execute().getRows(); time = System.currentTimeMillis() - time; NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(2); nf.setMinimumFractionDigits(2); executedIn = nf.format(((double) time) / 1000d); nf.setMaximumFractionDigits(0); totalResults = nf.format(rows.getSize()); if (request.getParameter("start") != null) { from = Long.parseLong(request.getParameter("start")); try { rows.skip(from); } catch (Exception e) { // make sure rows are consumed while (rows.hasNext()) { rows.nextRow(); } } } to = Math.min(from + 10, rows.getSize()); total = rows.getSize(); maxPage = total / 10L; if (total % 10L > 0) { maxPage++; } currentPageIndex = from / 10L; maxPage = Math.min(maxPage, currentPageIndex + 10); minPage = Math.max(0, currentPageIndex - 10); for (long i = minPage; i < maxPage; i++) { indexes.add(new Long(i)); } if (total < 10 && !q.startsWith("related:")) { try { Value v = jcrSession.getWorkspace().getQueryManager().createQuery( "/jcr:root[rep:spellcheck('" + q + "')]/(rep:spellcheck())", Query.XPATH).execute().getRows().nextRow().getValue("rep:spellcheck()"); if (v != null) { suggestedQuery = v.getString(); } } catch (RepositoryException e) { // ignore } } } request.setAttribute("title", "Search workspace " + wspName); %>



<% if (rows != null && rows.getSize() == 0) { %> <% if (suggestedQuery != null) { %>

Did you mean: <%= Text.encodeIllegalXMLCharacters(suggestedQuery) %>

<% } %>

Your search - <%= Text.encodeIllegalXMLCharacters(q) %> - did not match any documents.

Suggestions:

<% } else if (rows != null) { %>
Results <%= from + 1 %> - <%= to %> of about <%= totalResults %> <%= queryTerms %>. (<%= executedIn %> seconds) 
<% if (suggestedQuery != null) { %>

Did you mean: <%= Text.encodeIllegalXMLCharacters(suggestedQuery) %>

<% } %>
<% while (rows.hasNext() && rows.getPosition() < to) { Row r = rows.nextRow(); Node file = (Node) jcrSession.getItem(r.getValue("jcr:path").getString()); Node resource = file.getNode("jcr:content"); String size = ""; if (resource.hasProperty("jcr:data")) { double length = resource.getProperty("jcr:data").getLength(); size = String.valueOf(Math.round(Math.ceil(length / 1000d))) + "k"; } DateFormat df = SimpleDateFormat.getDateInstance(SimpleDateFormat.LONG); String lastModified = df.format(resource.getProperty("jcr:lastModified").getDate().getTime()); %>
" class=l><%= Text.encodeIllegalXMLCharacters(file.getName()) %>
<%= r.getValue("rep:excerpt(jcr:content)").getString() %> <%= Text.encodeIllegalXMLCharacters(file.getPath()) %> - <%= size %> - <%= lastModified %> - ">Similar pages
<% } // while %>

<% if (indexes.size() > 1) { %>
Result Page:  <% if (currentPageIndex != ((Long) indexes.get(0)).longValue()) { %>&start=<%= (currentPageIndex - 1) * 10 %>>Previous<% } else { %><% } for (Iterator it = indexes.iterator(); it.hasNext(); ) { long pageIdx = ((Long) it.next()).longValue(); if (pageIdx == currentPageIndex) { %><%= pageIdx + 1 %><% } else { %>&start=<%= pageIdx * 10 %>><%= pageIdx + 1 %><% } } if (currentPageIndex < (maxPage - 1)) { %>&start=<%= (currentPageIndex + 1) * 10 %>>Next<% } else { %><% } %>
<% } %>


/search.jsp>

Search within results | Dissatisfied? Help us improve

<% } // if (rows != null) String tableClass = ""; if (rows != null && rows.getSize() == 0) { tableClass = " class=\"t n bt\""; } %> <% } finally { if (jcrSession != null) { jcrSession.logout(); } } %>