use of jcifs.smb.SmbFileInputStream in project yacy_grid_mcp by yacy.
the class MultiProtocolURL method get.
public byte[] get(final ClientIdentification.Agent agent, final String username, final String pass) throws IOException {
if (isFile())
return read(new FileInputStream(getFSFile()));
if (isSMB())
return read(new SmbFileInputStream(getSmbFile()));
if (isFTP()) {
FTPStorageFactory client = new FTPStorageFactory(this.host, this.port < 0 ? 21 : this.port, username, pass, false);
Asset<byte[]> asset = client.getStorage().load(this.path);
return asset.getPayload();
}
if (isHTTP() || isHTTPS()) {
// TODO: add agent, user and pass
return ClientConnection.load(this.toString());
}
return null;
}
use of jcifs.smb.SmbFileInputStream in project fess-crawler by codelibs.
the class SmbClient method copy.
private void copy(final SmbFile src, final File dest) {
if (dest.exists() && !dest.canWrite()) {
return;
}
try (BufferedInputStream in = new BufferedInputStream(new SmbFileInputStream(src));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest))) {
final byte[] buf = new byte[1024];
int length;
while (-1 < (length = in.read(buf))) {
out.write(buf, 0, length);
out.flush();
}
} catch (final IOException e) {
throw new IORuntimeException(e);
}
}
use of jcifs.smb.SmbFileInputStream in project yacy_grid_mcp by yacy.
the class MultiProtocolURL method getInputStream.
public InputStream getInputStream(final ClientIdentification.Agent agent, final String username, final String pass) throws IOException {
if (isFile())
return new BufferedInputStream(new FileInputStream(getFSFile()));
if (isSMB())
return new BufferedInputStream(new SmbFileInputStream(getSmbFile()));
if (isFTP()) {
FTPStorageFactory client = new FTPStorageFactory(this.host, this.port < 0 ? 21 : this.port, username, pass, false);
Asset<byte[]> asset = client.getStorage().load(this.path);
return new ByteArrayInputStream(asset.getPayload());
}
if (isHTTP() || isHTTPS()) {
// TODO: add agent, user and pass
return new ByteArrayInputStream(ClientConnection.load(this.toString()));
}
return null;
}
use of jcifs.smb.SmbFileInputStream in project fess-crawler by codelibs.
the class SmbClient method getResponseData.
protected ResponseData getResponseData(final String uri, final boolean includeContent) {
final ResponseData responseData = new ResponseData();
responseData.setMethod(Constants.GET_METHOD);
final String filePath = preprocessUri(uri);
responseData.setUrl(filePath);
SmbFile file = null;
final SmbAuthentication smbAuthentication = smbAuthenticationHolder.get(filePath);
if (logger.isDebugEnabled()) {
logger.debug("Creating SmbFile: " + filePath);
}
try {
if (smbAuthentication == null) {
file = new SmbFile(filePath);
} else {
file = new SmbFile(filePath, smbAuthentication.getAuthentication());
}
} catch (final MalformedURLException e) {
logger.warn("Could not parse url: " + filePath, e);
}
if (logger.isDebugEnabled()) {
logger.debug("Processing SmbFile: " + filePath);
}
try {
if (file == null) {
responseData.setHttpStatusCode(Constants.NOT_FOUND_STATUS_CODE);
responseData.setCharSet(charset);
responseData.setContentLength(0);
} else if (file.isFile()) {
if (logger.isDebugEnabled()) {
logger.debug("Checking SmbFile Size: " + filePath);
}
responseData.setContentLength(file.length());
checkMaxContentLength(responseData);
responseData.setHttpStatusCode(Constants.OK_STATUS_CODE);
responseData.setCharSet(geCharSet(file));
responseData.setLastModified(new Date(file.lastModified()));
responseData.addMetaData(SMB_CREATE_TIME, new Date(file.createTime()));
try {
if (logger.isDebugEnabled()) {
logger.debug("Parsing SmbFile Owner: " + filePath);
}
final SID ownerUser = file.getOwnerUser();
if (ownerUser != null) {
final String[] ownerAttributes = { ownerUser.getAccountName(), ownerUser.getDomainName() };
responseData.addMetaData(SMB_OWNER_ATTRIBUTES, ownerAttributes);
}
} catch (final IOException e) {
logger.warn("Cannot get owner of the file: " + filePath);
}
if (logger.isDebugEnabled()) {
logger.debug("Parsing SmbFile ACL: " + filePath);
}
processAccessControlEntries(responseData, file);
final Map<String, List<String>> headerFieldMap = file.getHeaderFields();
if (headerFieldMap != null) {
for (final Map.Entry<String, List<String>> entry : headerFieldMap.entrySet()) {
responseData.addMetaData(entry.getKey(), entry.getValue());
}
}
if (file.canRead()) {
final MimeTypeHelper mimeTypeHelper = crawlerContainer.getComponent("mimeTypeHelper");
if (includeContent) {
if (logger.isDebugEnabled()) {
logger.debug("Parsing SmbFile Content: " + filePath);
}
if (file.getContentLength() < maxCachedContentSize) {
try (InputStream contentStream = new BufferedInputStream(new SmbFileInputStream(file))) {
responseData.setResponseBody(InputStreamUtil.getBytes(contentStream));
} catch (final Exception e) {
logger.warn("I/O Exception.", e);
responseData.setHttpStatusCode(Constants.SERVER_ERROR_STATUS_CODE);
}
} else {
File outputFile = null;
try {
outputFile = File.createTempFile("crawler-SmbClient-", ".out");
copy(file, outputFile);
responseData.setResponseBody(outputFile, true);
} catch (final Exception e) {
logger.warn("I/O Exception.", e);
responseData.setHttpStatusCode(Constants.SERVER_ERROR_STATUS_CODE);
if (outputFile != null && !outputFile.delete()) {
logger.warn("Could not delete " + outputFile.getAbsolutePath());
}
}
}
if (logger.isDebugEnabled()) {
logger.debug("Parsing SmbFile MIME Type: " + filePath);
}
try (final InputStream is = responseData.getResponseBody()) {
responseData.setMimeType(mimeTypeHelper.getContentType(is, file.getName()));
} catch (final Exception e) {
responseData.setMimeType(mimeTypeHelper.getContentType(null, file.getName()));
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("Parsing SmbFile MIME Type: " + filePath);
}
try (final InputStream is = new SmbFileInputStream(file)) {
responseData.setMimeType(mimeTypeHelper.getContentType(is, file.getName()));
} catch (final Exception e) {
responseData.setMimeType(mimeTypeHelper.getContentType(null, file.getName()));
}
}
if (contentLengthHelper != null) {
final long maxLength = contentLengthHelper.getMaxLength(responseData.getMimeType());
if (responseData.getContentLength() > maxLength) {
throw new MaxLengthExceededException("The content length (" + responseData.getContentLength() + " byte) is over " + maxLength + " byte. The url is " + filePath);
}
}
} else {
// Forbidden
responseData.setHttpStatusCode(Constants.FORBIDDEN_STATUS_CODE);
responseData.setMimeType(APPLICATION_OCTET_STREAM);
}
} else if (file.isDirectory()) {
if (logger.isDebugEnabled()) {
logger.debug("Parsing SmbFile Directory: " + filePath);
}
final Set<RequestData> requestDataSet = new HashSet<>(100);
if (includeContent) {
final SmbFile[] files = file.listFiles();
if (files != null) {
for (final SmbFile f : files) {
final String chileUri = f.toString();
requestDataSet.add(RequestDataBuilder.newRequestData().get().url(chileUri).build());
}
}
}
throw new ChildUrlsException(requestDataSet, this.getClass().getName() + "#getResponseData");
} else {
responseData.setHttpStatusCode(Constants.NOT_FOUND_STATUS_CODE);
responseData.setCharSet(charset);
responseData.setContentLength(0);
}
} catch (final CrawlerSystemException e) {
CloseableUtil.closeQuietly(responseData);
throw e;
} catch (final SmbException e) {
CloseableUtil.closeQuietly(responseData);
throw new CrawlingAccessException("Could not access " + uri, e);
}
return responseData;
}
Aggregations