refactor: move GSA specific date formatter to GSAservlet

adjust return type to String for HeaderFrameWork.getSingle
pull/93/head
reger 8 years ago
parent d525967999
commit 66cc0dd173

@ -35,6 +35,7 @@ import java.util.regex.Pattern;
import net.yacy.cora.protocol.HeaderFramework;
import net.yacy.cora.util.CommonPattern;
import net.yacy.http.servlets.GSAsearchServlet;
import net.yacy.peers.operation.yacyVersion;
import net.yacy.search.Switchboard;
import net.yacy.search.schema.CollectionSchema;
@ -279,7 +280,7 @@ public class GSAResponseWriter implements QueryResponseWriter {
}
if (CollectionSchema.last_modified.getSolrFieldName().equals(fieldName)) {
Date d = new Date(Long.parseLong(value.stringValue()));
writer.write("<FS NAME=\"date\" VALUE=\"" + HeaderFramework.formatGSAFS(d) + "\"/>\n");
writer.write("<FS NAME=\"date\" VALUE=\"" + formatGSAFS(d) + "\"/>\n");
//OpensearchResponseWriter.solitaireTag(writer, GSAToken.CACHE_LAST_MODIFIED.getSolrFieldName(), HeaderFramework.formatRFC1123(d));
//texts.add(value.stringValue());
continue;
@ -350,4 +351,19 @@ public class GSAResponseWriter implements QueryResponseWriter {
}
return text;
}
/**
* Format date for GSA (short form of ISO8601 date format)
* @param date
* @return datestring "yyyy-mm-dd"
* @see ISO8601Formatter
*/
public final String formatGSAFS(final Date date) {
if (date == null) return "";
synchronized (GSAsearchServlet.FORMAT_GSAFS) {
final String s = GSAsearchServlet.FORMAT_GSAFS.format(date);
return s;
}
}
}

@ -246,11 +246,9 @@ public class HeaderFramework extends TreeMap<String, String> implements Map<Stri
private static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss Z"; // with numeric time zone indicator as defined in RFC5322
private static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz";
private static final String PATTERN_ANSIC = "EEE MMM d HH:mm:ss yyyy";
private static final String PATTERN_GSAFS = "yyyy-MM-dd";
public static final SimpleDateFormat FORMAT_RFC1123 = new SimpleDateFormat(PATTERN_RFC1123, Locale.US);
public static final SimpleDateFormat FORMAT_RFC1036 = new SimpleDateFormat(PATTERN_RFC1036, Locale.US);
public static final SimpleDateFormat FORMAT_ANSIC = new SimpleDateFormat(PATTERN_ANSIC, Locale.US);
public static final SimpleDateFormat FORMAT_GSAFS = new SimpleDateFormat(PATTERN_GSAFS, Locale.US);
private static final TimeZone TZ_GMT = TimeZone.getTimeZone("GMT");
private static final Calendar CAL_GMT = Calendar.getInstance(TZ_GMT, Locale.US);
@ -285,22 +283,6 @@ public class HeaderFramework extends TreeMap<String, String> implements Map<Stri
}
}
public static final String formatGSAFS(final Date date) {
if (date == null) return "";
synchronized (FORMAT_GSAFS) {
final String s = FORMAT_GSAFS.format(date);
return s;
}
}
public static final Date parseGSAFS(final String datestring) {
try {
return FORMAT_GSAFS.parse(datestring);
} catch (final ParseException e) {
return null;
}
}
/** Initialization of static formats */
static {
// 2-digit dates are automatically parsed by SimpleDateFormat,
@ -355,6 +337,12 @@ public class HeaderFramework extends TreeMap<String, String> implements Map<Stri
return put("*" + key + "-" + Integer.toString(c), value);
}
/**
* Count occurence of header keys, look for original header name and a
* numbered version of the header *headername-NUMBER , with NUMBER starting at 1
* @param key the raw header name
* @return number of headers with same name
*/
public int keyCount(final String key) {
if (!(containsKey(key))) return 0;
int c = 1;
@ -370,15 +358,27 @@ public class HeaderFramework extends TreeMap<String, String> implements Map<Stri
return result;
}
// return multiple results
/**
* Get one Header of headers with same name.
* The headers are internally numbered
* @param key the raw header name
* @param count the number of the numbered header name (0 = same as get(key))
* @return value of header with number=count
*/
public String getSingle(final String key, final int count) {
if (count == 0) return get(key, null);
return get("*" + key + "-" + count, null);
if (count == 0) return get(key); // first look for just the key
return get("*" + key + "-" + count); // now for the numbered header names
}
public Object[] getMultiple(final String key) {
/**
* Get multiple header values with same header name.
* The header names are internally numbered (format *key-1)
* @param key the raw header name
* @return header values
*/
public String[] getMultiple(final String key) {
final int count = keyCount(key);
final Object[] result = new Object[count];
final String[] result = new String[count];
for (int i = 0; i < count; i++) result[i] = getSingle(key, i);
return result;
}

@ -24,9 +24,12 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletException;
@ -69,7 +72,11 @@ import org.apache.solr.util.FastWriter;
public class GSAsearchServlet extends HttpServlet {
private static final long serialVersionUID = 7835985518515673885L;
// GSA date formatter (short form of ISO8601 date format)
private static final String PATTERN_GSAFS = "yyyy-MM-dd";
public static final SimpleDateFormat FORMAT_GSAFS = new SimpleDateFormat(PATTERN_GSAFS, Locale.US);
private final static GSAResponseWriter responseWriter = new GSAResponseWriter();
@Override
@ -88,12 +95,11 @@ public class GSAsearchServlet extends HttpServlet {
// ------------------------------------------
/**
* from here copy of htroot/gsa/gsasearchresult.java
* from here copy of old htroot/gsa/gsasearchresult.java
* with modification to use HttpServletRequest instead of (yacy) RequestHeader
*/
public static void respond(final HttpServletRequest header, final Switchboard sb, final OutputStream out) {
private void respond(final HttpServletRequest header, final Switchboard sb, final OutputStream out) {
// remember the peer contact for peer statistics
String clientip = header.getRemoteAddr();
@ -169,9 +175,9 @@ public class GSAsearchServlet extends HttpServlet {
for (String dr: daterange) {
String from_to[] = dr.endsWith("..") ? new String[]{dr.substring(0, dr.length() - 2), ""} : dr.startsWith("..") ? new String[]{"", dr.substring(2)} : dr.split("\\.\\.");
if (from_to.length != 2) continue;
Date from = HeaderFramework.parseGSAFS(from_to[0]);
Date from = this.parseGSAFS(from_to[0]);
if (from == null) from = new Date(0);
Date to = HeaderFramework.parseGSAFS(from_to[1]);
Date to = this.parseGSAFS(from_to[1]);
if (to == null) to = new Date();
to.setTime(to.getTime() + 24L * 60L * 60L * 1000L); // we add a day because the day is inclusive
String z = CollectionSchema.last_modified.getSolrFieldName() + ":[" + ISO8601Formatter.FORMATTER.format(from) + " TO " + ISO8601Formatter.FORMATTER.format(to) + "]";
@ -258,4 +264,18 @@ public class GSAsearchServlet extends HttpServlet {
AccessTracker.addToDump(originalQuery, Integer.toString(matches));
ConcurrentLog.info("GSA Query", "results: " + matches + ", for query:" + post.toString());
}
/**
* Parse GSA date string (short form of ISO8601 date format)
* @param datestring
* @return date or null
* @see ISO8601Formatter
*/
public final Date parseGSAFS(final String datestring) {
try {
return FORMAT_GSAFS.parse(datestring);
} catch (final ParseException e) {
return null;
}
}
}

Loading…
Cancel
Save