📄 jcrurlconnection.java
字号:
contentType = guessContentTypeFromName(getItem().getName()); if (contentType == null) { contentType = (property.getType() == PropertyType.BINARY) ? APPLICATION_OCTET : TEXT_PLAIN; } } log.debug("connect: Using atom '" + property.getPath() + "' with content type '" + contentType + "' for " + String.valueOf(contentLength) + " bytes"); // set the fields setProperty(property); setContentType(contentType); setContentEncoding(contentEncoding); setContentLength(contentLength); setLastModified(lastModified); // mark connection open connected = true; } catch (RepositoryException re) { throw failure("connect", re.toString(), re); } } } /** * Returns an input stream that reads from this open connection. * <p> * <b>NOTES:</b> * <ul> * <li>Each call to this method returns a new <code>InputStream</code>. * <li>Do not forget to close the return stream when not used anymore for * the system to be able to free resources. * </ul> * <p> * Calling this method implicitly calls {@link #connect()} to ensure the * connection is open. * * @throws IOException if an error occurrs opening the connection through * {@link #connect()} or creating the <code>InputStream</code> on the * repository <code>Property</code>. * * @see #connect() */ public InputStream getInputStream() throws IOException { try { return getProperty().getStream(); } catch (RepositoryException re) { throw failure("getInputStream", re.toString(), re); } } /** * Gets the named header field. This implementation only supports the * Content-Type, Content-Encoding, Content-Length and Last-Modified header * fields. All other names return <code>null</code>. * <p> * Calling this method implicitly calls {@link #connect()} to ensure the * connection is open. * * @param s The name of the header field value to return. * * @return The corresponding value or <code>null</code> if not one of the * supported fields or the named field's value cannot be retrieved * from the data source. * * @see #connect() */ public String getHeaderField(String s) { try { connect(); if (CONTENT_LENGTH.equalsIgnoreCase(s)) { return String.valueOf(contentLength); } else if (CONTENT_TYPE.equalsIgnoreCase(s)) { return contentType; } else if (LAST_MODIFIED.equalsIgnoreCase(s)) { return String.valueOf(lastModified); } else if (CONTENT_ENCODING.equalsIgnoreCase(s)) { return contentEncoding; } } catch (IOException ioe) { log.info("getHeaderField: Problem connecting: " + ioe.toString()); log.debug("dump", ioe); } return null; } /** * Get the header field with the given index. As with * {@link #getHeaderField(String)} only Content-Length, Content-Type, * Content-Encoding, and Last-Modified are supported. All indexes other * than 0, 1, 2 or 3 will return <code>null</code>. * <p> * Calling this method implicitly calls {@link #connect()} to ensure the * connection is open. * * @param i The index of the header field value to return. * * @return The corresponding value or <code>null</code> if not one of the * supported fields or the known field's value cannot be retrieved * from the data source. * * @see #connect() */ public String getHeaderField(int i) { try { connect(); if (i == 0) { return String.valueOf(contentLength); } else if (i == 1) { return contentType; } else if (i == 2) { return String.valueOf(lastModified); } else if (i == 3) { return contentEncoding; } } catch (IOException ioe) { log.info("getHeaderField: Problem connecting: " + ioe.toString()); log.debug("dump", ioe); } return null; } /** * Get the name of the header field with the given index. As with * {@link #getHeaderField(String)} only Content-Length, Content-Type, * Content-Encoding and Last-Modified are supported. All indexes other than * 0, 1, 2 or 3 will return <code>null</code>. * <p> * Calling this method implicitly calls {@link #connect()} to ensure the * connection is open. * * @param i The index of the header field name to return. * @return The corresponding name or <code>null</code> if not one of the * supported fields. * * @see #connect() */ public String getHeaderFieldKey(int i) { try { connect(); if (i == 0) { return CONTENT_LENGTH; } else if (i == 1) { return CONTENT_TYPE; } else if (i == 2) { return LAST_MODIFIED; } else if (i == 3) { return CONTENT_ENCODING; } } catch (IOException ioe) { log .info("getHeaderFieldKey: Problem connecting: " + ioe.toString()); log.debug("dump", ioe); } return null; } /** * Returns an unmodifiable map of all header fields. Each entry is indexed * with a string key naming the field. The entry's value is an unmodifiable * list of the string values of the respective header field. * <p> * Calling this method implicitly calls {@link #connect()} to ensure the * connection is open. * * @return An unmodifiable map of header fields and their values. The map * will be empty if an error occurrs connecting through * {@link #connect()}. * * @see #connect() */ public Map getHeaderFields() { Map fieldMap = new HashMap(); try { connect(); fieldMap.put(CONTENT_LENGTH, toList(String.valueOf(contentLength))); fieldMap.put(CONTENT_TYPE, toList(contentType)); fieldMap.put(LAST_MODIFIED, toList(String.valueOf(lastModified))); // only include if not null)) if (contentEncoding != null) { fieldMap.put(CONTENT_ENCODING, toList(contentEncoding)); } } catch (IOException ioe) { log.info("getHeaderFields: Problem connecting: " + ioe.toString()); log.debug("dump", ioe); } return Collections.unmodifiableMap(fieldMap); } /** * Returns the content type of the data as a string. This is just a * perfomance convenience overwrite of the base class implementation. * <p> * Calling this method implicitly calls {@link #connect()} to ensure the * connection is open. * * @return The content length of the data or <code>null</code> if the * content type cannot be derived from the data source. * * @see #connect() */ public String getContentType() { try { connect(); return contentType; } catch (IOException ioe) { log.info("getContentType: Problem connecting: " + ioe.toString()); log.debug("dump", ioe); } return null; } /** * Returns the content encoding of the data as a string. This is just a * perfomance convenience overwrite of the base class implementation. * <p> * Calling this method implicitly calls {@link #connect()} to ensure the * connection is open. * * @return The content encoding of the data or <code>null</code> if the * content encoding cannot be derived from the data source. * * @see #connect() */ public String getContentEncoding() { try { connect(); return contentEncoding; } catch (IOException ioe) { log.info("getContentEncoding: Problem connecting: " + ioe.toString()); log.debug("dump", ioe); } return null; } /** * Returns the content length of the data as an number. This is just a * perfomance convenience overwrite of the base class implementation. * <p> * Calling this method implicitly calls {@link #connect()} to ensure the * connection is open. * * @return The content length of the data or -1 if the content length cannot * be derived from the data source. * * @see #connect() */ public int getContentLength() { try { connect(); return contentLength; } catch (IOException ioe) { log.info("getContentLength: Problem connecting: " + ioe.toString()); log.debug("dump", ioe); } return -1; } /** * Returns the value of the <code>last-modified</code> header field. The * result is the number of milliseconds since January 1, 1970 GMT. * <p> * Calling this method implicitly calls {@link #connect()} to ensure the * connection is open. * * @return the date the resource referenced by this * <code>URLConnection</code> was last modified, or -1 if not * known. * * @see #connect() */ public long getLastModified() { try { connect(); return lastModified; } catch (IOException ioe) { log.info("getLastModified: Problem connecting: " + ioe.toString()); log.debug("dump", ioe); } return -1; } //---------- implementation helpers ---------------------------------------- /** * Returns the URL handler of the URL of this connection. */ protected JCRURLHandler getHandler() { return handler; } /** * Returns the {@link FileParts} object which contains the decomposed file * part of this connection's URL. */ FileParts getFileParts() { if (fileParts == null) { fileParts = new FileParts(getURL().getFile()); } return fileParts; } /** * @param contentEncoding The contentEncoding to set. */ protected void setContentEncoding(String contentEncoding) { this.contentEncoding = contentEncoding; } /** * @param contentLength The contentLength to set. */ protected void setContentLength(int contentLength) { this.contentLength = contentLength; } /** * @param contentType The contentType to set. */ protected void setContentType(String contentType) { this.contentType = contentType; } /** * @param lastModified The lastModified to set. */ protected void setLastModified(long lastModified) { this.lastModified = lastModified; } /** * @param property The property to set. */ protected void setProperty(Property property) { this.property = property; } //---------- internal ----------------------------------------------------- /** * Logs the message and returns an IOException to be thrown by the caller. * The log message contains the caller name, the external URL form and the * message while the IOException is only based on the external URL form and * the message given. * * @param method The method in which the error occurred. This is used for * logging. * @param message The message to log and set in the exception * @param cause The cause of failure. May be <code>null</code>. * * @return The IOException the caller may throw. */ protected IOException failure(String method, String message, Throwable cause) { log.info(method + ": URL: " + url.toExternalForm() + ", Reason: " + message); if (cause != null) { log.debug("dump", cause); } IOException ioe = new IOException(url.toExternalForm() + ": " + message); ioe.initCause(cause); return ioe; } /** * Returns an unmodifiable list containing just the given string value. */ private List toList(String value) { String[] values = { value }; List valueList = Arrays.asList(values); return Collections.unmodifiableList(valueList); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -