📄 importsupport.java
字号:
public void close() throws IOException
{
if(null != huc)
{
huc.disconnect();
}
wrappedReader.close();
}
// Pass-through methods.
public void mark(int readAheadLimit) throws IOException
{
wrappedReader.mark(readAheadLimit);
}
public boolean markSupported()
{
return wrappedReader.markSupported();
}
public int read() throws IOException
{
return wrappedReader.read();
}
public int read(char[] buf) throws IOException
{
return wrappedReader.read(buf);
}
public int read(char[] buf, int off, int len) throws IOException
{
return wrappedReader.read(buf, off, len);
}
public boolean ready() throws IOException
{
return wrappedReader.ready();
}
public void reset() throws IOException
{
wrappedReader.reset();
}
public long skip(long n) throws IOException
{
return wrappedReader.skip(n);
}
}
/** Wraps responses to allow us to retrieve results as Strings. */
protected class ImportResponseWrapper extends HttpServletResponseWrapper
{
/*
* We provide either a Writer or an OutputStream as requested.
* We actually have a true Writer and an OutputStream backing
* both, since we don't want to use a character encoding both
* ways (Writer -> OutputStream -> Writer). So we use no
* encoding at all (as none is relevant) when the target resource
* uses a Writer. And we decode the OutputStream's bytes
* using OUR tag's 'charEncoding' attribute, or ISO-8859-1
* as the default. We thus ignore setLocale() and setContentType()
* in this wrapper.
*
* In other words, the target's asserted encoding is used
* to convert from a Writer to an OutputStream, which is typically
* the medium through with the target will communicate its
* ultimate response. Since we short-circuit that mechanism
* and read the target's characters directly if they're offered
* as such, we simply ignore the target's encoding assertion.
*/
/** The Writer we convey. */
private StringWriter sw;
/** A buffer, alternatively, to accumulate bytes. */
private ByteArrayOutputStream bos;
/** 'True' if getWriter() was called; false otherwise. */
private boolean isWriterUsed;
/** 'True if getOutputStream() was called; false otherwise. */
private boolean isStreamUsed;
/** The HTTP status set by the target. */
private int status = 200;
//************************************************************
// Constructor and methods
/**
* Constructs a new ImportResponseWrapper.
* @param response the response to wrap
*/
public ImportResponseWrapper(HttpServletResponse response)
{
super(response);
}
/**
* @return a Writer designed to buffer the output.
*/
public PrintWriter getWriter()
{
if (isStreamUsed)
{
throw new IllegalStateException("Unexpected internal error during import: "
+ "Target servlet called getWriter(), then getOutputStream()");
}
isWriterUsed = true;
if (sw == null)
{
sw = new StringWriter();
}
return new PrintWriter(sw);
}
/**
* @return a ServletOutputStream designed to buffer the output.
*/
public ServletOutputStream getOutputStream()
{
if (isWriterUsed)
{
throw new IllegalStateException("Unexpected internal error during import: "
+ "Target servlet called getOutputStream(), then getWriter()");
}
isStreamUsed = true;
if (bos == null)
{
bos = new ByteArrayOutputStream();
}
ServletOutputStream sos = new ServletOutputStream()
{
public void write(int b) throws IOException
{
bos.write(b);
}
};
return sos;
}
/** Has no effect. */
public void setContentType(String x)
{
// ignore
}
/** Has no effect. */
public void setLocale(Locale x)
{
// ignore
}
/**
* Sets the status of the response
* @param status the status code
*/
public void setStatus(int status)
{
this.status = status;
}
/**
* @return the status of the response
*/
public int getStatus()
{
return status;
}
/**
* Retrieves the buffered output, using the containing tag's
* 'charEncoding' attribute, or the tag's default encoding,
* <b>if necessary</b>.
* @return the buffered output
* @throws UnsupportedEncodingException if the encoding is not supported
*/
public String getString() throws UnsupportedEncodingException
{
if (isWriterUsed)
{
return sw.toString();
}
else if (isStreamUsed)
{
return bos.toString(this.getCharacterEncoding());
}
else
{
return ""; // target didn't write anything
}
}
}
//*********************************************************************
// Public utility methods
/**
* Returns <tt>true</tt> if our current URL is absolute,
* <tt>false</tt> otherwise.
*
* @param url the url to check out
* @return true if the url is absolute
*/
public static boolean isAbsoluteUrl(String url) {
// a null URL is not absolute, by our definition
if (url == null)
{
return false;
}
// do a fast, simple check first
int colonPos;
if ((colonPos = url.indexOf(":")) == -1)
{
return false;
}
// if we DO have a colon, make sure that every character
// leading up to it is a valid scheme character
for (int i = 0; i < colonPos; i++)
{
if (VALID_SCHEME_CHARS.indexOf(url.charAt(i)) == -1)
{
return false;
}
}
// if so, we've got an absolute url
return true;
}
/**
* Strips a servlet session ID from <tt>url</tt>. The session ID
* is encoded as a URL "path parameter" beginning with "jsessionid=".
* We thus remove anything we find between ";jsessionid=" (inclusive)
* and either EOS or a subsequent ';' (exclusive).
*
* @param url the url to strip the session id from
* @return the stripped url
*/
public static String stripSession(String url)
{
StringBuffer u = new StringBuffer(url);
int sessionStart;
while ((sessionStart = u.toString().indexOf(";jsessionid=")) != -1)
{
int sessionEnd = u.toString().indexOf(";", sessionStart + 1);
if (sessionEnd == -1)
{
sessionEnd = u.toString().indexOf("?", sessionStart + 1);
}
if (sessionEnd == -1)
{
// still
sessionEnd = u.length();
}
u.delete(sessionStart, sessionEnd);
}
return u.toString();
}
/**
* Get the value associated with a content-type attribute.
* Syntax defined in RFC 2045, section 5.1.
*
* @param input the string containing the attributes
* @param name the name of the content-type attribute
* @return the value associated with a content-type attribute
*/
public static String getContentTypeAttribute(String input, String name)
{
int begin;
int end;
int index = input.toUpperCase().indexOf(name.toUpperCase());
if (index == -1)
{
return null;
}
index = index + name.length(); // positioned after the attribute name
index = input.indexOf('=', index); // positioned at the '='
if (index == -1)
{
return null;
}
index += 1; // positioned after the '='
input = input.substring(index).trim();
if (input.charAt(0) == '"')
{
// attribute value is a quoted string
begin = 1;
end = input.indexOf('"', begin);
if (end == -1)
{
return null;
}
}
else
{
begin = 0;
end = input.indexOf(';');
if (end == -1)
{
end = input.indexOf(' ');
}
if (end == -1)
{
end = input.length();
}
}
return input.substring(begin, end).trim();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -