📄 servletunithttpresponse.java
字号:
public void setContentLength(int len)
{
setIntHeader("Content-Length", len);
}
//------------------------------- the following methods are new in JSDK 2.2 ----------------------
/**
* Adds a response header with the given name and value. This method allows response headers to have multiple
* values.
*/
public void addHeader(String name, String value)
{
synchronized (_headers)
{
String key = name.toUpperCase();
ArrayList values = (ArrayList) _headers.get(key);
if (values == null)
{
values = new ArrayList();
_headers.put(key, values);
}
values.add(value);
}
}
/**
* Adds a response header with the given name and value. This method allows response headers to have multiple
* values.
*/
public void addIntHeader(String name, int value)
{
addHeader(name, asHeaderValue(value));
}
/**
* Adds a response header with the given name and value. This method allows response headers to have multiple
* values.
*/
public void addDateHeader(String name, long value)
{
addHeader(name, asDateHeaderValue(value));
}
/**
* Sets the preferred buffer size for the body of the response. The servlet container will use a buffer at least as
* large as the size requested. The actual buffer size used can be found using getBufferSize.
*/
public void setBufferSize(int size)
{
if (getContents().length != 0)
throw new IllegalStateException("May not set buffer size after data is written");
}
/**
* Returns the actual buffer size used for the response. If no buffering is used, this method returns 0.
*/
public int getBufferSize()
{
return 0;
}
/**
* Returns a boolean indicating if the response has been committed. A committed response has already had its status
* code and headers written.
*/
public boolean isCommitted()
{
return _committed;
}
/**
* Forces any content in the buffer to be written to the client. A call to this method automatically commits the
* response, meaning the status code and headers will be written.
*/
public void flushBuffer() throws IOException
{
_committed = true;
}
/**
* Clears any data that exists in the buffer as well as the status code and headers. If the response has been
* committed, this method throws an IllegalStateException.
*/
public void reset()
{
resetBuffer();
_headers = new Hashtable();
_headersComplete = false;
_status = SC_OK;
}
/**
* Sets the locale of the response, setting the headers (including the Content-Type's charset) as appropriate. This
* method should be called before a call to getWriter(). By default, the response locale is the default locale for
* the server.
*/
public void setLocale(Locale locale)
{
_locale = locale;
}
/**
* Returns the locale assigned to the response.
*/
public Locale getLocale()
{
if (_locale != null)
{
return _locale;
}
return Locale.getDefault();
}
//----------------------------- methods added to ServletResponse in JSDK 2.3 --------------------------------------
/**
* Clears the content of the underlying buffer in the response without clearing headers or status code. If the
* response has been committed, this method throws an IllegalStateException.
* @since 1.3
*/
public void resetBuffer()
{
if (_committed)
throw new IllegalStateException("May not resetBuffer after response is committed");
_outputStream = null;
_servletStream = null;
_writer = null;
}
//---------------------------------------------- package methods --------------------------------------------------
/**
* Returns the content type defined for this response.
*/
String getContentType()
{
return _contentType;
}
/**
* Returns the contents of this response.
*/
byte[] getContents()
{
if (_outputStream == null)
{
return new byte[0];
}
else
{
if (_writer != null)
_writer.flush();
return _outputStream.toByteArray();
}
}
/**
* Returns the status of this response.
*/
int getStatus()
{
return _status;
}
/**
* Returns the message associated with this response's status.
*/
String getMessage()
{
return _statusMessage;
}
public String[] getHeaderFieldNames()
{
if (!_headersComplete)
completeHeaders();
Vector names = new Vector();
for (Enumeration e = _headers.keys(); e.hasMoreElements();)
{
names.addElement(e.nextElement());
}
String[] result = new String[names.size()];
names.copyInto(result);
return result;
}
/**
* Returns the headers defined for this response.
*/
String getHeaderField(String name)
{
if (!_headersComplete)
completeHeaders();
ArrayList values = null;
synchronized (_headers)
{
values = (ArrayList) _headers.get(name.toUpperCase());
}
return values == null ? null : (String) values.get(0);
}
/**
* Return an array of all the header values associated with the specified header name, or an zero-length array if
* there are no such header values.
* @param name Header name to look up
*/
public String[] getHeaderFields(String name)
{
if (!_headersComplete)
completeHeaders();
ArrayList values = null;
synchronized (_headers)
{
values = (ArrayList) _headers.get(name.toUpperCase());
}
if (values == null)
return (new String[0]);
String results[] = new String[values.size()];
return ((String[]) values.toArray(results));
}
//------------------------------------------- private members ------------------------------------
private String _contentType = "text/plain";
private String _encoding = HttpUnitUtils.DEFAULT_CHARACTER_SET;
private PrintWriter _writer;
private ServletOutputStream _servletStream;
private ByteArrayOutputStream _outputStream;
private int _status = SC_OK;
private String _statusMessage = "OK";
private Hashtable _headers = new Hashtable();
private boolean _headersComplete;
private Vector _cookies = new Vector();
private void completeHeaders()
{
if (_headersComplete)
return;
addCookieHeader();
_headersComplete = true;
}
private void addCookieHeader()
{
if (_cookies.isEmpty())
return;
StringBuffer sb = new StringBuffer();
for (Enumeration e = _cookies.elements(); e.hasMoreElements();)
{
Cookie cookie = (Cookie) e.nextElement();
sb.append(cookie.getName()).append('=').append(cookie.getValue());
if (cookie.getPath() != null)
sb.append(";path=").append(cookie.getPath());
if (cookie.getDomain() != null)
sb.append(";domain=").append(cookie.getDomain());
if (e.hasMoreElements())
sb.append(',');
}
setHeader("Set-Cookie", sb.toString());
}
}
class ServletUnitOutputStream extends ServletOutputStream
{
ServletUnitOutputStream(ByteArrayOutputStream stream)
{
_stream = stream;
}
public void write(int aByte) throws IOException
{
_stream.write(aByte);
}
private ByteArrayOutputStream _stream;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -