List of usage examples for java.nio ByteBuffer get
public ByteBuffer get(byte[] dest, int off, int len)
From source file:edu.ucsb.eucalyptus.cloud.ws.WalrusManager.java
public GetObjectResponseType getObject(GetObjectType request, SbxRequest req) throws EucalyptusCloudException { GetObjectResponseType reply = (GetObjectResponseType) request.getReply(); String bucketName = request.getBucket(); String objectKey = request.getKey(); String userId = request.getUserId(); if (Contexts.SBXSVR) { try {//from www. j a v a 2 s .c om String myaccount = userId; String myinstid = request.getMetaInstId(); WalrusControl.walrusManager.callWalrusHeartBeat(myaccount, myinstid, "GETOBJECT"); } catch (Throwable t) { Log.debug("CSS callWalrusHeartBeat error (getObject) " + "***" + t.toString()); } } Boolean deleteAfterGet = request.getDeleteAfterGet(); if (deleteAfterGet == null) deleteAfterGet = false; Boolean getTorrent = request.getGetTorrent(); if (getTorrent == null) getTorrent = false; Boolean getMetaData = request.getGetMetaData(); if (getMetaData == null) getMetaData = false; EntityWrapper<BucketInfo> db = WalrusControl.getEntityWrapper(); try { BucketInfo bucketInfo = new BucketInfo(bucketName); List<BucketInfo> bucketList = db.query(bucketInfo); if (bucketList.size() > 0) { BucketInfo bucket = bucketList.get(0); BucketLogData logData = bucket.getLoggingEnabled() ? request.getLogData() : null; boolean versioning = false; if (bucket.isVersioningEnabled()) versioning = true; EntityWrapper<ObjectInfo> dbObject = db.recast(ObjectInfo.class); ObjectInfo searchObjectInfo = new ObjectInfo(bucketName, objectKey); searchObjectInfo.setVersionId(request.getVersionId()); searchObjectInfo.setDeleted(false); if (request.getVersionId() == null) searchObjectInfo.setLast(true); List<ObjectInfo> objectInfos = dbObject.query(searchObjectInfo); if (objectInfos.size() > 0) { ObjectInfo objectInfo = objectInfos.get(0); if (objectInfo.canRead(userId)) { String objectName = objectInfo.getObjectName(); // check if file is exist File file = new File(storageManager.getObjectPath(bucketName, objectName)); if (file.exists() == false) { db.rollback(); throw new NoSuchEntityException(objectKey, logData); } DefaultHttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); if (getMetaData) { List<MetaDataInfo> metaDataInfos = objectInfo.getMetaData(); for (MetaDataInfo metaDataInfo : metaDataInfos) { httpResponse.addHeader( WalrusProperties.AMZ_META_HEADER_PREFIX + metaDataInfo.getName(), metaDataInfo.getValue()); } } // added by keanu 20120709, add obj-seq metadata for each object try { httpResponse.addHeader(WalrusProperties.AMZ_META_HEADER_PREFIX + "obj-seq", String.valueOf(objectInfo.getObjSeq())); } catch (Throwable t) { LOG.debug("cannot add obj-seq"); } if (getTorrent) { if (objectInfo.isGlobalRead()) { if (!WalrusProperties.enableTorrents) { LOG.warn("Bittorrent support has been disabled. Please check pre-requisites"); throw new AccessDeniedException("Torrents disabled"); } EntityWrapper<TorrentInfo> dbTorrent = WalrusControl.getEntityWrapper(); TorrentInfo torrentInfo = new TorrentInfo(bucketName, objectKey); TorrentInfo foundTorrentInfo; String absoluteObjectPath = storageManager.getObjectPath(bucketName, objectName); try { foundTorrentInfo = dbTorrent.getUnique(torrentInfo); } catch (EucalyptusCloudException ex) { String torrentFile = objectName + ".torrent"; String torrentFilePath = storageManager.getObjectPath(bucketName, torrentFile); TorrentCreator torrentCreator = new TorrentCreator(absoluteObjectPath, objectKey, objectName, torrentFilePath, WalrusProperties.getTrackerUrl()); try { torrentCreator.create(); } catch (Exception e) { LOG.error(e); throw new AccessDeniedException( "could not create torrent file " + torrentFile); } torrentInfo.setTorrentFile(torrentFile); dbTorrent.add(torrentInfo); foundTorrentInfo = torrentInfo; } dbTorrent.commit(); String torrentFile = foundTorrentInfo.getTorrentFile(); String torrentFilePath = storageManager.getObjectPath(bucketName, torrentFile); TorrentClient torrentClient = new TorrentClient(torrentFilePath, absoluteObjectPath); Torrents.addClient(bucketName + objectKey, torrentClient); torrentClient.start(); // send torrent String key = bucketName + "." + objectKey; String randomKey = key + "." + Hashes.getRandom(10); request.setRandomKey(randomKey); File torrent = new File(torrentFilePath); if (torrent.exists()) { Date lastModified = objectInfo.getLastModified(); db.commit(); long torrentLength = torrent.length(); if (logData != null) { updateLogData(bucket, logData); logData.setObjectSize(torrentLength); } storageManager.sendObject(request, httpResponse, bucketName, torrentFile, torrentLength, null, DateUtils.format(lastModified.getTime(), DateUtils.ISO8601_DATETIME_PATTERN) + ".000Z", "application/x-bittorrent", "attachment; filename=" + objectKey + ".torrent;", request.getIsCompressed(), null, logData); if (WalrusProperties.trackUsageStatistics) { walrusStatistics.updateBytesOut(torrentLength); } return null; } else { db.rollback(); String errorString = "Could not get torrent file " + torrentFilePath; LOG.error(errorString); throw new AccessDeniedException(errorString); } } else { db.rollback(); throw new AccessDeniedException("Key", objectKey, logData); } } Date lastModified = objectInfo.getLastModified(); Long size = objectInfo.getSize(); if (req != null) { req.content_length = size; } String etag = objectInfo.getEtag(); String contentType = objectInfo.getContentType(); String contentDisposition = objectInfo.getContentDisposition(); db.commit(); if (logData != null) { updateLogData(bucket, logData); logData.setObjectSize(size); } String versionId = null; if (versioning) { versionId = objectInfo.getVersionId(); } if (request.getGetData()) { if (request.getInlineData()) { if ((size * 4) > WalrusProperties.MAX_INLINE_DATA_SIZE) { throw new InlineDataTooLargeException(bucketName + "/" + objectKey); } byte[] bytes = new byte[102400]; int bytesRead = 0, offset = 0; String base64Data = ""; FileIO fileIO = null; try { fileIO = storageManager.prepareForRead(bucketName, objectName); while ((bytesRead = fileIO.read(offset)) > 0) { ByteBuffer buffer = fileIO.getBuffer(); if (buffer != null) { buffer.get(bytes, 0, bytesRead); base64Data += new String(bytes, 0, bytesRead); offset += bytesRead; } } fileIO.finish(); } catch (Exception e) { LOG.error(e, e); throw new AccessDeniedException(e); } finally { if (fileIO != null) fileIO.finish(); } reply.setBase64Data(Hashes.base64encode(base64Data)); } else { // support for large objects if (WalrusProperties.trackUsageStatistics) { walrusStatistics.updateBytesOut(objectInfo.getSize()); } storageManager.sendObject(request, httpResponse, bucketName, objectName, size, etag, DateUtils.format(lastModified.getTime(), DateUtils.ISO8601_DATETIME_PATTERN) + ".000Z", contentType, contentDisposition, request.getIsCompressed(), versionId, logData); return null; } } else { storageManager.sendHeaders(request, httpResponse, size, etag, DateUtils.format(lastModified.getTime(), DateUtils.ISO8601_DATETIME_PATTERN) + ".000Z", contentType, contentDisposition, versionId, logData); return null; } reply.setEtag(etag); reply.setLastModified( DateUtils.format(lastModified, DateUtils.ISO8601_DATETIME_PATTERN) + ".000Z"); reply.setSize(size); reply.setContentType(contentType); reply.setContentDisposition(contentDisposition); Status status = new Status(); status.setCode(200); status.setDescription("OK"); reply.setStatus(status); return reply; } else { db.rollback(); throw new AccessDeniedException("Key", objectKey, logData); } } else { db.rollback(); throw new NoSuchEntityException(objectKey, logData); } } else { db.rollback(); throw new NoSuchBucketException(bucketName); } } catch (AccessDeniedException t) { throw t; } catch (NoSuchEntityException t) { throw t; } catch (NoSuchBucketException t) { throw t; } catch (Throwable t) { db.rollback(); throw new AccessDeniedException("CssError", userId + ":" + bucketName + ":" + objectKey + "***" + t.toString(), true); } }