- Timestamp:
- 03/14/13 13:30:06 (11 years ago)
- File:
-
- 1 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,
Note: See TracChangeset
for help on using the changeset viewer.