- Timestamp:
- 03/14/13 13:30:06 (11 years ago)
- Location:
- SRUServer/trunk/src/main/java/eu/clarin/sru/server
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUServer.java
r2692 r2698 17 17 package eu.clarin.sru.server; 18 18 19 import java.io.FilterOutputStream; 19 20 import java.io.IOException; 20 21 import java.io.OutputStream; … … 65 66 static final String RESPONSE_ENCODING = "utf-8"; 66 67 private static final String RESPONSE_CONTENT_TYPE = "application/xml"; 67 private static final int RESPONSE_BUFFER_SIZE = 64 * 1024;68 68 private static final Logger logger = 69 69 LoggerFactory.getLogger(SRUServer.class); … … 116 116 response.setStatus(HttpServletResponse.SC_OK); 117 117 // make sure we can reset the stream later in case of error ... 118 response.setBufferSize(RESPONSE_BUFFER_SIZE); 119 118 response.setBufferSize(config.getResponseBufferSize()); 120 119 try { 121 120 if (req.checkParameters()) { … … 135 134 SRUXMLStreamWriter out = 136 135 createXMLStreamWriter(response.getOutputStream(), 137 SRURecordPacking.XML, req.getIndentResponse()); 136 SRURecordPacking.XML, false, 137 req.getIndentResponse()); 138 138 writeFatalError(out, req, req.getDiagnostics()); 139 139 } … … 150 150 } catch (SRUException e) { 151 151 if (!response.isCommitted()) { 152 if (logger.isInfoEnabled()) { 153 final String message = e.getDiagnostic().getMessage(); 154 if (message != null) { 155 logger.info("Sending fatal diagnostic '{}{}' with " + 156 "message '{}'", 157 new Object[] { 158 SRUConstants.SRU_DIAGNOSTIC_URI_PREFIX, 159 e.getDiagnostic().getCode(), 160 message 161 }); 162 } else { 163 logger.info("Sending fatal diagnostic '{}{}'", 164 SRUConstants.SRU_DIAGNOSTIC_URI_PREFIX, 165 e.getDiagnostic().getCode()); 166 } 167 logger.debug("Fatal diagnostic was caused by " + 168 "this exception", e); 169 } 152 170 response.resetBuffer(); 153 171 try { … … 160 178 SRUXMLStreamWriter out = 161 179 createXMLStreamWriter(response.getOutputStream(), 162 SRURecordPacking.XML, 180 SRURecordPacking.XML, false, 163 181 req.getIndentResponse()); 164 182 writeFatalError(out, req, diagnostics); … … 168 186 } 169 187 } else { 188 /* 189 * The servlet already flushed the output buffer, so cannot 190 * degrade gracefully anymore and, unfortunately, will produce 191 * ill-formed XML output. 192 * Increase the response buffer size, if you want to avoid 193 * this (at the cost of memory). 194 */ 170 195 logger.error("A fatal error occurred, but the response was " 171 + "already committed ", e);196 + "already committed. Unable to recover gracefully.", e); 172 197 } 173 198 } … … 188 213 createXMLStreamWriter(response.getOutputStream(), 189 214 request.getRecordPacking(), 215 true, 190 216 request.getIndentResponse()); 191 217 … … 238 264 createXMLStreamWriter(response.getOutputStream(), 239 265 request.getRecordPacking(), 266 true, 240 267 request.getIndentResponse()); 241 268 … … 364 391 createXMLStreamWriter(response.getOutputStream(), 365 392 request.getRecordPacking(), 393 true, 366 394 request.getIndentResponse()); 367 395 … … 1044 1072 1045 1073 private SRUXMLStreamWriter createXMLStreamWriter(OutputStream out, 1046 SRURecordPacking recordPacking, int indent) throws SRUException { 1074 SRURecordPacking recordPacking, boolean skipFlush, int indent) 1075 throws SRUException { 1047 1076 try { 1048 return new SRUXMLStreamWriter(out, writerFactory, recordPacking, 1049 indent); 1077 if (skipFlush) { 1078 /* 1079 * Add a FilterOutputStream to delay flush() as long as 1080 * possible. Doing so, enabled us to send an appropriate SRU 1081 * diagnostic in case an error occurs during the serialization 1082 * of the response. 1083 * Of course, if an error occurs when the Servlet response 1084 * buffer already had been flushed, because it was to large, 1085 * we cannot fail gracefully and we will produce ill-formed 1086 * XML output. 1087 */ 1088 out = new FilterOutputStream(out) { 1089 @Override 1090 public void flush() throws IOException { 1091 } 1092 1093 1094 @Override 1095 public void close() throws IOException { 1096 super.flush(); 1097 super.close(); 1098 } 1099 }; 1100 } 1101 return new SRUXMLStreamWriter(out, writerFactory, 1102 recordPacking, indent); 1050 1103 } catch (Exception e) { 1051 1104 throw new SRUException(SRUConstants.SRU_GENERAL_SYSTEM_ERROR, -
SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUServerConfig.java
r2683 r2698 224 224 "eu.clarin.sru.server.allowOverrideIndentResponse"; 225 225 /** 226 * Parameter constant for configuring the size of response buffer size. The 227 * servlet will buffer up to this amount of data before sending a response 228 * to the client. The value if the response buffer in bytes. 229 * <p> 230 * Valid values: any positive integer (default 65536) 231 * </p> 232 */ 233 public static final String SRU_RESPONSE_BUFFER_SIZE = 234 "eu.clarin.sru.server.responseBufferSize"; 235 /** 226 236 * @deprecated use {@link #SRU_TRANSPORT} 227 237 */ … … 283 293 private static final String LEGACY_SRU_ALLOW_OVERRIDE_INDENT_RESPONSE = 284 294 "sru.allowOverrideIndentResponse"; 285 private static final int DEFAULT_NUMBER_OF_RECORDS = 100; 286 private static final int DEFAULT_MAXIMUM_RECORDS = 250; 287 private static final int DEFAULT_NUMBER_OF_TERMS = 250; 288 private static final int DEFAULT_MAXIMUM_TERMS = 500; 295 private static final int DEFAULT_NUMBER_OF_RECORDS = 100; 296 private static final int DEFAULT_MAXIMUM_RECORDS = 250; 297 private static final int DEFAULT_NUMBER_OF_TERMS = 250; 298 private static final int DEFAULT_MAXIMUM_TERMS = 500; 299 private static final int DEFAULT_RESPONSE_BUFFER_SIZE = 64 * 1024; 289 300 private static final String CONFIG_FILE_NAMESPACE_URI = 290 301 "http://www.clarin.eu/sru-server/1.0/"; … … 626 637 private final boolean echoRequests; 627 638 private final int indentResponse; 639 private final int responseBufferSize; 628 640 private final boolean allowOverrideMaximumRecords; 629 641 private final boolean allowOverrideMaximumTerms; … … 638 650 String database, int numberOfRecords, int maximumRecords, 639 651 int numberOfTerms, int maximumTerms, boolean echoRequests, 640 int indentResponse, boolean allowOverrideMaximumRecords, 652 int indentResponse, int responseBufferSize, 653 boolean allowOverrideMaximumRecords, 641 654 boolean allowOverrideMaximumTerms, 642 655 boolean allowOverrideIndentResponse, DatabaseInfo databaseinfo, … … 652 665 this.echoRequests = echoRequests; 653 666 this.indentResponse = indentResponse; 667 this.responseBufferSize = responseBufferSize; 654 668 this.allowOverrideMaximumRecords = allowOverrideMaximumRecords; 655 669 this.allowOverrideMaximumTerms = allowOverrideMaximumTerms; … … 751 765 public boolean allowOverrideIndentResponse() { 752 766 return allowOverrideIndentResponse; 767 } 768 769 770 public int getResponseBufferSize() { 771 return responseBufferSize; 753 772 } 754 773 … … 988 1007 SRU_ALLOW_OVERRIDE_INDENT_RESPONSE, false, false); 989 1008 1009 int responseBufferSize = parseNumber(params, 1010 SRU_RESPONSE_BUFFER_SIZE, false, 1011 DEFAULT_RESPONSE_BUFFER_SIZE, 0, -1); 1012 990 1013 return new SRUServerConfig(transport, host, port, database, 991 1014 numberOfRecords, maximumRecords, numberOfTerms, 992 1015 maximumTerms, echoRequests, indentResponse, 993 allowOverrideMaximumRecords, allowOverrideMaximumTerms,994 allowOverride IndentResponse, databaseInfo, indexInfo,995 schemaInfo);1016 responseBufferSize, allowOverrideMaximumRecords, 1017 allowOverrideMaximumTerms, allowOverrideIndentResponse, 1018 databaseInfo, indexInfo, schemaInfo); 996 1019 } catch (IOException e) { 997 1020 throw new SRUConfigException("error reading configuration file", e);
Note: See TracChangeset
for help on using the changeset viewer.