java.fedora.server.access.FieldSearchServlet Maven / Gradle / Ivy
Show all versions of fcrepo-client Show documentation
/*
* -----------------------------------------------------------------------------
*
* License and Copyright: The contents of this file are subject to 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.fedora-commons.org/licenses.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The entire file consists of original code.
* Copyright © 2008 Fedora Commons, Inc.
*
Copyright © 2002-2007 The Rector and Visitors of the University of
* Virginia and Cornell University
* All rights reserved.
*
* -----------------------------------------------------------------------------
*/
package fedora.server.access;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.log4j.Logger;
import fedora.common.Constants;
import fedora.server.Context;
import fedora.server.ReadOnlyContext;
import fedora.server.Server;
import fedora.server.errors.InitializationException;
import fedora.server.errors.authorization.AuthzException;
import fedora.server.errors.servletExceptionExtensions.BadRequest400Exception;
import fedora.server.errors.servletExceptionExtensions.InternalError500Exception;
import fedora.server.errors.servletExceptionExtensions.RootException;
import fedora.server.search.Condition;
import fedora.server.search.FieldSearchQuery;
import fedora.server.search.FieldSearchResult;
import fedora.server.search.ObjectFields;
import fedora.server.utilities.StreamUtility;
/**
* REST interface for API-A's FieldSearch functionality.
*
* @author [email protected]
* @version $Id: FieldSearchServlet.java 6546 2008-02-04 21:48:49Z cwilper $
*/
public class FieldSearchServlet
extends HttpServlet {
private static final long serialVersionUID = 1L;
/** Logger for this class. */
private static final Logger LOG = Logger.getLogger(FieldSearchServlet.class);
/** Instance of the Server */
private static Server s_server=null;
/** Instance of the access subsystem */
private static Access s_access=null;
private String[] getFieldsArray(HttpServletRequest req) {
ArrayList l=new ArrayList();
if ( (req.getParameter("pid")!=null) && (req.getParameter("pid").equalsIgnoreCase("true")) ) l.add("pid");
if ( (req.getParameter("label")!=null) && (req.getParameter("label").equalsIgnoreCase("true")) ) l.add("label");
if ( (req.getParameter("fType")!=null) && (req.getParameter("fType").equalsIgnoreCase("true")) ) l.add("fType");
if ( (req.getParameter("cModel")!=null) && (req.getParameter("cModel").equalsIgnoreCase("true")) ) l.add("cModel");
if ( (req.getParameter("state")!=null) && (req.getParameter("state").equalsIgnoreCase("true")) ) l.add("state");
if ( (req.getParameter("ownerId")!=null) && (req.getParameter("ownerId").equalsIgnoreCase("true")) ) l.add("ownerId");
if ( (req.getParameter("cDate")!=null) && (req.getParameter("cDate").equalsIgnoreCase("true")) ) l.add("cDate");
if ( (req.getParameter("mDate")!=null) && (req.getParameter("mDate").equalsIgnoreCase("true")) ) l.add("mDate");
if ( (req.getParameter("dcmDate")!=null) && (req.getParameter("dcmDate").equalsIgnoreCase("true")) ) l.add("dcmDate");
if ( (req.getParameter("bDef")!=null) && (req.getParameter("bDef").equalsIgnoreCase("true")) ) l.add("bDef");
if ( (req.getParameter("bMech")!=null) && (req.getParameter("bMech").equalsIgnoreCase("true")) ) l.add("bMech");
if ( (req.getParameter("title")!=null) && (req.getParameter("title").equalsIgnoreCase("true")) ) l.add("title");
if ( (req.getParameter("creator")!=null) && (req.getParameter("creator").equalsIgnoreCase("true")) ) l.add("creator");
if ( (req.getParameter("subject")!=null) && (req.getParameter("subject").equalsIgnoreCase("true")) ) l.add("subject");
if ( (req.getParameter("description")!=null) && (req.getParameter("description").equalsIgnoreCase("true")) ) l.add("description");
if ( (req.getParameter("publisher")!=null) && (req.getParameter("publisher").equalsIgnoreCase("true")) ) l.add("publisher");
if ( (req.getParameter("contributor")!=null) && (req.getParameter("contributor").equalsIgnoreCase("true")) ) l.add("contributor");
if ( (req.getParameter("date")!=null) && (req.getParameter("date").equalsIgnoreCase("true")) ) l.add("date");
if ( (req.getParameter("type")!=null) && (req.getParameter("type").equalsIgnoreCase("true")) ) l.add("type");
if ( (req.getParameter("format")!=null) && (req.getParameter("format").equalsIgnoreCase("true")) ) l.add("format");
if ( (req.getParameter("identifier")!=null) && (req.getParameter("identifier").equalsIgnoreCase("true")) ) l.add("identifier");
if ( (req.getParameter("source")!=null) && (req.getParameter("source").equalsIgnoreCase("true")) ) l.add("source");
if ( (req.getParameter("language")!=null) && (req.getParameter("language").equalsIgnoreCase("true")) ) l.add("language");
if ( (req.getParameter("relation")!=null) && (req.getParameter("relation").equalsIgnoreCase("true")) ) l.add("relation");
if ( (req.getParameter("coverage")!=null) && (req.getParameter("coverage").equalsIgnoreCase("true")) ) l.add("coverage");
if ( (req.getParameter("rights")!=null) && (req.getParameter("rights").equalsIgnoreCase("true")) ) l.add("rights");
String[] ret=new String[l.size()];
for (int i=0; i fieldHash=new HashSet();
if (fieldsArray!=null) {
for (int i=0; i");
html.append("\n");
html.append("Fields to display: ");
html.append("");
html.append(" pid
");
html.append(" label
");
html.append(" fType
");
html.append(" cModel
");
html.append(" state
");
html.append(" ownerId
");
html.append(" cDate
");
html.append(" mDate
");
html.append(" dcmDate
");
html.append(" ");
html.append(" bDef
");
html.append(" bMech
");
html.append(" title
");
html.append(" creator
");
html.append(" subject
");
html.append(" description
");
html.append(" publisher
");
html.append(" contributor
");
html.append(" date
");
html.append(" ");
html.append(" type
");
html.append(" format
");
html.append(" identifier
");
html.append(" source
");
html.append(" language
");
html.append(" relation
");
html.append(" coverage
");
html.append(" rights
");
html.append(" ");
html.append("Search all fields for phrase: help ");
html.append("Or search specific field(s): help
");
html.append("Maximum Results: ");
html.append("
");
html.append("
");
html.append("
");
}
FieldSearchResult fsr=null;
if ((fieldsArray!=null && fieldsArray.length>0) || (sessionToken!=null)) {
if (sessionToken!=null) {
fsr=s_access.resumeFindObjects(context, sessionToken);
} else {
if ((terms!=null) && (terms.length()!=0)) {
fsr=s_access.findObjects(context, fieldsArray,
maxResults, new FieldSearchQuery(terms));
} else {
fsr=s_access.findObjects(context, fieldsArray,
maxResults, new FieldSearchQuery(
Condition.getConditions(query)));
}
}
List searchResults=fsr.objectFieldsList();
if (!xml) {
html.append("\n");
html.append("");
for (int i=0; i");
html.append(fieldsArray[i]);
html.append("");
}
html.append(" ");
}
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
for (int i=0; i\n");
appendXML("pid", f.getPid(), xmlBuf);
appendXML("label", f.getLabel(), xmlBuf);
appendXML("fType", f.getFType(), xmlBuf);
appendXML("cModel", f.getCModel(), xmlBuf);
appendXML("state", f.getState(), xmlBuf);
appendXML("ownerId", f.getOwnerId(), xmlBuf);
appendXML("cDate", f.getCDate(), formatter, xmlBuf);
appendXML("mDate", f.getMDate(), formatter, xmlBuf);
appendXML("dcmDate", f.getDCMDate(), formatter, xmlBuf);
appendXML("bDef", f.bDefs(), xmlBuf);
appendXML("bMech", f.bMechs(), xmlBuf);
appendXML("title", f.titles(), xmlBuf);
appendXML("creator", f.creators(), xmlBuf);
appendXML("subject", f.subjects(), xmlBuf);
appendXML("description", f.descriptions(), xmlBuf);
appendXML("publisher", f.publishers(), xmlBuf);
appendXML("contributor", f.contributors(), xmlBuf);
appendXML("date", f.dates(), xmlBuf);
appendXML("type", f.types(), xmlBuf);
appendXML("format", f.formats(), xmlBuf);
appendXML("identifier", f.identifiers(), xmlBuf);
appendXML("source", f.sources(), xmlBuf);
appendXML("language", f.languages(), xmlBuf);
appendXML("relation", f.relations(), xmlBuf);
appendXML("coverage", f.coverages(), xmlBuf);
appendXML("rights", f.rights(), xmlBuf);
xmlBuf.append(" \n");
} else {
html.append("");
for (int j=0; j");
if (l.equalsIgnoreCase("pid")) {
html.append("");
html.append(f.getPid());
html.append("");
} else if (l.equalsIgnoreCase("label")) {
if (f.getLabel()!=null) {
html.append(StreamUtility.enc(f.getLabel()));
}
} else if (l.equalsIgnoreCase("fType")) {
html.append(f.getFType());
} else if (l.equalsIgnoreCase("cModel")) {
if (f.getCModel()!=null) {
html.append(StreamUtility.enc(f.getCModel()));
}
} else if (l.equalsIgnoreCase("state")) {
html.append(f.getState());
} else if (l.equalsIgnoreCase("ownerId")) {
if (f.getOwnerId()!=null) {
html.append(f.getOwnerId());
}
} else if (l.equalsIgnoreCase("cDate")) {
html.append(formatter.format(f.getCDate()));
} else if (l.equalsIgnoreCase("mDate")) {
html.append(formatter.format(f.getMDate()));
} else if (l.equalsIgnoreCase("dcmDate")) {
if (f.getDCMDate()!=null) html.append(formatter.format(f.getDCMDate()));
} else if (l.equalsIgnoreCase("bDef")) {
html.append(getList(f.bDefs()));
} else if (l.equalsIgnoreCase("bMech")) {
html.append(getList(f.bMechs()));
} else if (l.equalsIgnoreCase("title")) {
html.append(getList(f.titles()));
} else if (l.equalsIgnoreCase("creator")) {
html.append(getList(f.creators()));
} else if (l.equalsIgnoreCase("subject")) {
html.append(getList(f.subjects()));
} else if (l.equalsIgnoreCase("description")) {
html.append(getList(f.descriptions()));
} else if (l.equalsIgnoreCase("publisher")) {
html.append(getList(f.publishers()));
} else if (l.equalsIgnoreCase("contributor")) {
html.append(getList(f.contributors()));
} else if (l.equalsIgnoreCase("date")) {
html.append(getList(f.dates()));
} else if (l.equalsIgnoreCase("type")) {
html.append(getList(f.types()));
} else if (l.equalsIgnoreCase("format")) {
html.append(getList(f.formats()));
} else if (l.equalsIgnoreCase("identifier")) {
html.append(getList(f.identifiers()));
} else if (l.equalsIgnoreCase("source")) {
html.append(getList(f.sources()));
} else if (l.equalsIgnoreCase("language")) {
html.append(getList(f.languages()));
} else if (l.equalsIgnoreCase("relation")) {
html.append(getList(f.relations()));
} else if (l.equalsIgnoreCase("coverage")) {
html.append(getList(f.coverages()));
} else if (l.equalsIgnoreCase("rights")) {
html.append(getList(f.rights()));
}
html.append("");
}
html.append(" ");
html.append(" ");
}
}
if (!xml) {
html.append("
");
if (fsr!=null && fsr.getToken()!=null) {
if (fsr.getCursor()!=-1) {
long viewingStart=fsr.getCursor()+1;
long viewingEnd=fsr.objectFieldsList().size() + viewingStart - 1;
html.append("Viewing results " + viewingStart + " to " + viewingEnd);
if (fsr.getCompleteListSize()!=-1) {
html.append(" of " + fsr.getCompleteListSize());
}
html.append("
\n");
}
html.append("");
}
html.append(" \n");
}
}
if (!xml) {
response.setContentType("text/html; charset=UTF-8");
PrintWriter out=response.getWriter();
out.print("Search Repository ");
out.print("");
out.println("");
out.println(" ");
out.println("");
out.println("Fedora Repository
");
out.println("Find Objects
");
out.println("
");
out.print(html.toString());
out.print(" ");
out.print("");
out.flush();
out.close();
} else {
response.setContentType("text/xml; charset=UTF-8");
PrintWriter out=new PrintWriter(
new OutputStreamWriter(
response.getOutputStream(), "UTF-8"));
out.println("");
out.println("");
if ((fsr!=null) && (fsr.getToken()!=null)) {
out.println(" ");
out.println(" " + fsr.getToken() + " ");
if (fsr.getCursor()!=-1) {
out.println(" " + fsr.getCursor() + " ");
}
if (fsr.getCompleteListSize()!=-1) {
out.println(" " + fsr.getCompleteListSize() + " ");
}
if (fsr.getExpirationDate()!=null) {
out.println(" " + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(fsr.getExpirationDate()) + " ");
}
out.println(" ");
}
out.println("");
out.println(xmlBuf.toString());
out.println(" ");
out.println(" ");
out.flush(); out.close();
}
} catch (AuthzException ae) {
throw RootException.getServletException (ae, request, ACTION_LABEL, new String[0]);
} catch (ServletException e) {
throw e;
} catch (Throwable th) {
throw new InternalError500Exception("", th, request, actionLabel, "", new String[0]);
}
}
private void appendXML(String name, String value, StringBuffer out) {
if (value!=null) {
out.append(" <" + name + ">" + StreamUtility.enc(value) + "" + name + ">\n");
}
}
private void appendXML(String name, List values, StringBuffer out) {
for (int i=0; i0) {
ret.append(", ");
}
ret.append(StreamUtility.enc((String) l.get(i)));
}
return ret.toString();
}
/** Exactly the same behavior as doGet. */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/** Gets the Fedora Server instance. */
public void init() throws ServletException {
try {
s_server=Server.getInstance(new File(Constants.FEDORA_HOME), false);
s_access=(Access) s_server.getModule("fedora.server.access.Access");
} catch (InitializationException ie) {
throw new ServletException("Error getting Fedora Server instance: "
+ ie.getMessage());
}
}
}