private static String urlEncode(String url, boolean keepPathSlash) { String encoded; try { encoded = URLEncoder.encode(url, UTF_8.name()); } catch (UnsupportedEncodingException e) { throw new RuntimeException(LfsServerText.get().unsupportedUtf8, e); } if (keepPathSlash) { encoded = encoded.replace("%2F", "/"); //$NON-NLS-1$ //$NON-NLS-2$ } return encoded; }
private static String urlEncode(String url, boolean keepPathSlash) { String encoded; try { encoded = URLEncoder.encode(url, StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { throw new RuntimeException(LfsServerText.get().unsupportedUtf8, e); } if (keepPathSlash) { encoded = encoded.replace("%2F", "/"); //$NON-NLS-1$ //$NON-NLS-2$ } return encoded; }
private static byte[] sign(String stringData, byte[] key) { try { byte[] data = stringData.getBytes("UTF-8"); //$NON-NLS-1$ Mac mac = Mac.getInstance(HMACSHA256); mac.init(new SecretKeySpec(key, HMACSHA256)); return mac.doFinal(data); } catch (Exception e) { throw new RuntimeException(MessageFormat.format( LfsServerText.get().failedToCalcSignature, e.getMessage()), e); } }
private static byte[] sign(String stringData, byte[] key) { try { byte[] data = stringData.getBytes(UTF_8); Mac mac = Mac.getInstance(HMACSHA256); mac.init(new SecretKeySpec(key, HMACSHA256)); return mac.doFinal(data); } catch (Exception e) { throw new RuntimeException(MessageFormat.format( LfsServerText.get().failedToCalcSignature, e.getMessage()), e); } }
private URL getObjectUrl(AnyLongObjectId oid) { try { return new URL(String.format("https://s3-%s.amazonaws.com/%s/%s", //$NON-NLS-1$ s3Config.getRegion(), s3Config.getBucket(), getPath(oid))); } catch (MalformedURLException e) { throw new IllegalArgumentException(MessageFormat.format( LfsServerText.get().unparsableEndpoint, e.getMessage())); } }
private URL getObjectUrl(AnyLongObjectId oid) { try { return new URL(String.format("https://s3-%s.amazonaws.com/%s/%s", //$NON-NLS-1$ s3Config.getRegion(), s3Config.getBucket(), getPath(oid))); } catch (MalformedURLException e) { throw new IllegalArgumentException(MessageFormat.format( LfsServerText.get().unparsableEndpoint, e.getMessage())); } }
static TransferHandler forOperation(String operation, LargeFileRepository repository, List<LfsObject> objects) { switch (operation) { case UPLOAD: return new Upload(repository, objects); case DOWNLOAD: return new Download(repository, objects); case VERIFY: default: throw new UnsupportedOperationException(MessageFormat.format( LfsServerText.get().unsupportedOperation, operation)); } }
static TransferHandler forOperation(String operation, LargeFileRepository repository, List<LfsObject> objects) { switch (operation) { case TransferHandler.UPLOAD: return new Upload(repository, objects); case TransferHandler.DOWNLOAD: return new Download(repository, objects); case TransferHandler.VERIFY: default: throw new UnsupportedOperationException(MessageFormat.format( LfsServerText.get().unsupportedOperation, operation)); } }
private void validateConfig(S3Config config) { assertNotEmpty(LfsServerText.get().undefinedS3AccessKey, config.getAccessKey()); assertNotEmpty(LfsServerText.get().undefinedS3Bucket, config.getBucket()); assertNotEmpty(LfsServerText.get().undefinedS3Region, config.getRegion()); assertNotEmpty(LfsServerText.get().undefinedS3SecretKey, config.getSecretKey()); assertNotEmpty(LfsServerText.get().undefinedS3StorageClass, config.getStorageClass()); }
private void validateConfig(S3Config config) { assertNotEmpty(LfsServerText.get().undefinedS3AccessKey, config.getAccessKey()); assertNotEmpty(LfsServerText.get().undefinedS3Bucket, config.getBucket()); assertNotEmpty(LfsServerText.get().undefinedS3Region, config.getRegion()); assertNotEmpty(LfsServerText.get().undefinedS3SecretKey, config.getSecretKey()); assertNotEmpty(LfsServerText.get().undefinedS3StorageClass, config.getStorageClass()); }
private AnyLongObjectId getObjectToTransfer(HttpServletRequest req, HttpServletResponse rsp) throws IOException { String info = req.getPathInfo(); int length = 1 + Constants.LONG_OBJECT_ID_STRING_LENGTH; if (info.length() != length) { sendError(rsp, HttpStatus.SC_UNPROCESSABLE_ENTITY, MessageFormat .format(LfsServerText.get().invalidPathInfo, info)); return null; } try { return LongObjectId.fromString(info.substring(1, length)); } catch (InvalidLongObjectIdException e) { sendError(rsp, HttpStatus.SC_UNPROCESSABLE_ENTITY, e.getMessage()); return null; } }
private void verifyHash() { AnyLongObjectId contentHash = LongObjectId .fromRaw(out.getMessageDigest().digest()); if (!contentHash.equals(id)) { abort(); throw new CorruptLongObjectException(id, contentHash, MessageFormat.format(LfsServerText.get().corruptLongObject, contentHash, id)); } }
/** * Retrieve object id from request * * @param req * servlet request * @param rsp * servlet response * @return object id, or <code>null</code> if the object id could not be * retrieved * @throws java.io.IOException * if an I/O error occurs * @since 4.6 */ protected AnyLongObjectId getObjectToTransfer(HttpServletRequest req, HttpServletResponse rsp) throws IOException { String info = req.getPathInfo(); int length = 1 + Constants.LONG_OBJECT_ID_STRING_LENGTH; if (info.length() != length) { sendError(rsp, HttpStatus.SC_UNPROCESSABLE_ENTITY, MessageFormat .format(LfsServerText.get().invalidPathInfo, info)); return null; } try { return LongObjectId.fromString(info.substring(1, length)); } catch (InvalidLongObjectIdException e) { sendError(rsp, HttpStatus.SC_UNPROCESSABLE_ENTITY, e.getMessage()); return null; } }
private void addObjectInfo(Response.Body body, LfsObject o) throws IOException { Response.ObjectInfo info = new Response.ObjectInfo(); body.objects.add(info); info.oid = o.oid; info.size = o.size; LongObjectId oid = LongObjectId.fromString(o.oid); if (repository.getSize(oid) >= 0) { info.actions = new HashMap<>(); info.actions.put(DOWNLOAD, repository.getDownloadAction(oid)); } else { info.error = new Response.Error(); info.error.code = SC_NOT_FOUND; info.error.message = MessageFormat.format( LfsServerText.get().objectNotFound, oid.getName()); } } }
private void addObjectInfo(Response.Body body, LfsObject o) throws IOException { Response.ObjectInfo info = new Response.ObjectInfo(); body.objects.add(info); info.oid = o.oid; info.size = o.size; LongObjectId oid = LongObjectId.fromString(o.oid); if (repository.getSize(oid) >= 0) { info.actions = new HashMap<>(); info.actions.put(DOWNLOAD, repository.getDownloadAction(oid)); } else { info.error = new Response.Error(); info.error.code = SC_NOT_FOUND; info.error.message = MessageFormat.format( LfsServerText.get().objectNotFound, oid.getName()); } } }
/** * Extracts the {@link LongObjectId} from the request. Finishes the request, in case the {@link LongObjectId} cannot * be extracted with an appropriate error. * * @throws IOException Thrown if the response could not be completed in an error case. */ private AnyLongObjectId getObjectToTransfer(HttpServletRequest request, HttpServletResponse response) throws IOException { String path = request.getPathInfo(); String objectIdFromPath = objectIdFromPath(path); if (objectIdFromPath == null) { //ObjectId is not retrievable from URL sendErrorAndLog(response, HttpStatus.SC_UNPROCESSABLE_ENTITY, MessageFormat.format(LfsServerText.get().invalidPathInfo, path)); return null; } else { try { return LongObjectId.fromString(objectIdFromPath); } catch (InvalidLongObjectIdException e) { sendErrorAndLog(response, HttpStatus.SC_UNPROCESSABLE_ENTITY, e); return null; } } }
/** * Provides a blob to download. * <p> * Actual implementation is based on <code>org.eclipse.jgit.lfs.server.fs.ObjectDownloadListener</code> and adjusted * to non-async as we're currently on servlet-2.5. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { AnyLongObjectId objectId = getObjectToTransfer(request, response); if (objectId == null) { logInvalidObjectId(request.getRequestURI()); } else { final String objectIdName = objectId.getName(); logger.trace("---- providing download for LFS-Oid: {}", objectIdName); Blob savedBlob = blobStore.get(objectIdName); if (isBlobPresent(savedBlob)) { logger.trace("----- Object {}: providing {} bytes", objectIdName, savedBlob.getSize()); writeBlobIntoResponse(savedBlob, response); } else { sendErrorAndLog(response, HttpStatus.SC_NOT_FOUND, MessageFormat.format(LfsServerText.get().objectNotFound, objectIdName)); } } }
/** * {@inheritDoc} * * Handle object downloads */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException { AnyLongObjectId obj = getObjectToTransfer(req, rsp); if (obj != null) { if (repository.getSize(obj) == -1) { sendError(rsp, HttpStatus.SC_NOT_FOUND, MessageFormat .format(LfsServerText.get().objectNotFound, obj.getName())); return; } AsyncContext context = req.startAsync(); context.setTimeout(timeout); rsp.getOutputStream() .setWriteListener(new ObjectDownloadListener(repository, context, rsp, obj)); } }
/** * Handles object downloads * * @param req * servlet request * @param rsp * servlet response * @throws ServletException * if a servlet-specific error occurs * @throws IOException * if an I/O error occurs */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException { AnyLongObjectId obj = getObjectToTransfer(req, rsp); if (obj != null) { if (repository.getSize(obj) == -1) { sendError(rsp, HttpStatus.SC_NOT_FOUND, MessageFormat .format(LfsServerText.get().objectNotFound, obj.getName())); return; } AsyncContext context = req.startAsync(); context.setTimeout(timeout); rsp.getOutputStream() .setWriteListener(new ObjectDownloadListener(repository, context, rsp, obj)); } }