private void printPagination(XMLWriter xml) throws IOException {
    // print all
    if (this._totalPages < 5) {
      for (int i = 1; i <= this._totalPages; i++) {
        xml.element("page", String.valueOf(i));
      }
    } else {

      long middle = 2;
      long leftStart = this._currentPage - middle;
      long rightEnd = this._currentPage + middle;

      if (leftStart <= 0) {
        rightEnd += Math.abs(leftStart) + 1;
        leftStart = 1;
      }

      if (rightEnd > this._totalPages) {
        leftStart -= Math.abs(rightEnd - this._totalPages);
        rightEnd = this._totalPages;
      }

      for (long i = leftStart; i < rightEnd + 1; i++) {
        xml.element("page", String.valueOf(i));
      }
    }
  }
Example #2
0
 private void outputResults(
     SearchQuery query, SearchResults results, List<FieldFacet> facets, XMLWriter xml)
     throws IOException {
   xml.openElement("index-search", true);
   xml.openElement("facets");
   for (FieldFacet facet : facets) {
     facet.toXML(xml);
   }
   xml.closeElement();
   query.toXML(xml);
   results.toXML(xml);
   xml.closeElement();
 }
Example #3
0
 @Override
 public void processMultiple(Collection<IndexMaster> indexes, ContentRequest req, XMLWriter xml)
     throws BerliozException, IOException {
   String facets = req.getParameter("facets", "");
   int maxNumber = req.getIntParameter("max-facets", 20);
   // get first catalog as they should all have the same one
   SearchQuery query = buildQuery(req, indexes.iterator().next().getCatalog());
   if (query == null) {
     xml.emptyElement("index-search");
     return;
   }
   SearchPaging paging = buildPaging(req);
   ArrayList<Index> theIndexes = new ArrayList<Index>();
   for (IndexMaster index : indexes) {
     theIndexes.add(index.getIndex());
   }
   try {
     SearchResults results = LuceneIndexQueries.query(theIndexes, query, paging);
     List<FieldFacet> facetsList =
         Facets.getFacets(
             Arrays.asList(facets.split(",")), maxNumber, query.toQuery(), theIndexes);
     outputResults(query, results, facetsList, xml);
   } catch (IndexException ex) {
     LOGGER.warn(
         "Fail to retrieve search result using query: {}", (Object) query.toString(), (Object) ex);
   }
 }
Example #4
0
  @Override
  public void process(ContentRequest req, XMLWriter xml) throws BerliozException, IOException {
    Runtime runtime = Runtime.getRuntime();

    xml.openElement("runtime");
    xml.attribute("processors", runtime.availableProcessors());

    // Memory information
    xml.openElement("memory");
    xml.attribute("free", Long.toString(runtime.freeMemory()));
    xml.attribute("total", Long.toString(runtime.totalMemory()));
    xml.attribute("max", Long.toString(runtime.maxMemory()));
    xml.closeElement();

    xml.closeElement();
  }
Example #5
0
 @Override
 public void processSingle(IndexMaster index, ContentRequest req, XMLWriter xml)
     throws BerliozException, IOException {
   String facets = req.getParameter("facets", "");
   int maxNumber = req.getIntParameter("max-facets", 20);
   SearchQuery query = buildQuery(req, index.getCatalog());
   if (query == null) {
     xml.emptyElement("index-search");
     return;
   }
   SearchPaging paging = buildPaging(req);
   try {
     SearchResults results = index.query(query, paging);
     List<FieldFacet> facetsList =
         Facets.getFacets(
             Arrays.asList(facets.split(",")), maxNumber, query.toQuery(), index.getIndex());
     outputResults(query, results, facetsList, xml);
   } catch (IndexException ex) {
     LOGGER.warn(
         "Fail to retrieve search result using query: {}", (Object) query.toString(), (Object) ex);
   }
 }
 private void toXML(XMLWriter xml, PSProject project, Status status) throws IOException {
   xml.openElement("create-project");
   xml.attribute("name", this.project.getName());
   xml.attribute("status", status.name());
   xml.closeElement();
 }
  @Override
  public void toXML(XMLWriter xml) throws IOException {
    xml.openElement("results");
    xml.attribute("total", String.valueOf(this._numFound));
    xml.attribute("start", String.valueOf(this._start));
    xml.attribute("row", String.valueOf(this._row));
    xml.attribute("last-page", String.valueOf(this._totalPages));
    xml.attribute("current-page", String.valueOf(this._currentPage));

    if (this._totalPages > 1) {
      xml.openElement("pages");
      printPagination(xml);
      xml.closeElement(); // pages
    }

    // query
    xml.openElement("query");
    for (String name : this._query.getParameterNames()) {
      xml.openElement("param");
      xml.attribute("name", name);
      String[] values = this._query.getParams(name);
      if (values != null) {
        if (values.length == 1) xml.attribute("value", values[0]);
        else {
          StringBuilder sb = new StringBuilder();
          for (String value : values) {
            sb.append(',').append(value);
          }
          xml.attribute("value", sb.substring(1));
        }
      }
      xml.closeElement(); // param
    }
    xml.closeElement(); // query

    xml.closeElement(); // result
  }
  /**
   * Writes the XML response for this header.
   *
   * <pre class="xml">{@code
   * <header>
   *   <!-- Deprecated in Berlioz 1.0 -->
   *   <group>[service group name]</group>
   *   <service>[service name]</service>
   *   <path-info>[berlioz path]</path-info>
   *   <context-path>[servlet context path]</context-path>
   *   <!-- Deprecated in Berlioz 1.0 -->
   *   <host>[server host]</host>
   *   <port>[server port]</port>
   *   <url>[url (up to query)]</url>
   *   <query-string>[query string]</query-string>
   *   <!-- End deprecated in Berlioz 1.0 -->
   *   <location scheme="[http|https]"
   *               host="[hostname]"
   *               port="[post]"
   *               path="[path]"
   *               base="[base]"
   *              query="[query]">[full url]</location>
   *   <path context="[servlet context path]"
   *          prefix="[prefix if berlioz mapped to a prefix]"
   *            info="[berlioz path]"
   *       extension="[prefix if berlioz mapped to an extension]"/>
   *   <http-parameters>
   *     <parameter name="[name-A]">[value-A]</parameter>
   *     <parameter name="[name-B]">[value-B1]</parameter>
   *     <parameter name="[name-B]">[value-B2]</parameter>
   *     <parameter name="[name-C]">[value-C]</parameter>
   *     <parameter name="[name-D]">[value-D]</parameter>
   *     <!-- ... -->
   *   </http-parameters>
   *   <uri-parameters>
   *     <parameter name="[name-X]">[value-X]</parameter>
   *     <parameter name="[name-Y]">[value-Y]</parameter>
   *     <!-- ... -->
   *   </uri-parameters>
   *   <berlioz version="[version]" mode="[mode]"/>
   * </header>
   * }</pre>
   *
   * @see XMLWritable#toXML(org.pageseeder.xmlwriter.XMLWriter)
   * @param xml The XML Writer to use.
   * @throws IOException If thrown by the underlying XML Writer.
   */
  @Override
  public void toXML(XMLWriter xml) throws IOException {
    HttpServletRequest req = this._core.request();

    boolean compatibility =
        GlobalSettings.has(BerliozOption.XML_HEADER_COMPATIBILITY)
            && !"1.0".equals(GlobalSettings.get(BerliozOption.XML_HEADER_VERSION));

    // start serialising
    xml.openElement("header", true);
    if (compatibility) {
      xml.writeComment("Elements below will be deprecated in Berlioz 1.0");
      xml.element("group", this._group);
      xml.element("service", this._service);
      xml.writeComment("Use 'path' instead");
      xml.element("path-info", HttpRequestWrapper.getBerliozPath(req));
      xml.element("context-path", req.getContextPath());
      xml.writeComment("Use 'location' instead");
      xml.element("scheme", req.getScheme());
      xml.element("host", req.getServerName());
      xml.element("port", Integer.toString(req.getServerPort()));
      xml.element("url", req.getRequestURL().toString());
      xml.element("query-string", req.getQueryString());
      xml.writeComment("End deprecated elements");
    }

    // New location info
    Location location = this._core.location();
    if (location != null) {
      location.toXML(xml);
      PathInfo path = location.info();
      path.toXML(xml);
    }

    // Write the http parameters
    xml.openElement("http-parameters", true);
    Enumeration<?> names = req.getParameterNames();
    while (names.hasMoreElements()) {
      String paramName = (String) names.nextElement();
      String[] values = req.getParameterValues(paramName);
      for (String value : values) {
        xml.openElement("parameter", false);
        xml.attribute("name", paramName);
        xml.writeText(value);
        xml.closeElement();
      }
    }
    xml.closeElement(); // close http-parameters

    // Write the URI parameters
    if (this._results != null) {
      Set<String> unames = this._results.names();
      xml.openElement("uri-parameters", !unames.isEmpty());
      for (String name : unames) {
        Object value = this._results.get(name);
        xml.openElement("parameter", false);
        xml.attribute("name", name);
        xml.writeText(value != null ? value.toString() : "");
        xml.closeElement();
      }
      xml.closeElement();
    }

    // Include Berlioz version and mode
    xml.openElement("berlioz");
    xml.attribute("version", GlobalSettings.getVersion());
    xml.attribute("mode", GlobalSettings.getMode());
    xml.closeElement();

    xml.closeElement(); // close header
  }