Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011 GitHub Inc. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: Kevin Sawicki (GitHub Inc.) - initial API and implementation /
 
 package org.eclipse.egit.github.core.service;
 
 import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_DOWNLOADS;
 import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_REPOS;
 import static org.eclipse.egit.github.core.client.PagedRequest.PAGE_FIRST;
 import static org.eclipse.egit.github.core.client.PagedRequest.PAGE_SIZE;
 
 
 import java.io.File;
 import java.util.List;
 
Service for accessing, creating, and deleting repositories downloads.

See also:
GitHub downloads API documentation
 
 public class DownloadService extends GitHubService {

UPLOAD_KEY
 
 	public static final String UPLOAD_KEY = "key"//$NON-NLS-1$
 
UPLOAD_ACL
 
 	public static final String UPLOAD_ACL = "acl"//$NON-NLS-1$
 
UPLOAD_SUCCESS_ACTION_STATUS
 
 	public static final String UPLOAD_SUCCESS_ACTION_STATUS = "success_action_status"//$NON-NLS-1$
 
UPLOAD_FILENAME
 
 	public static final String UPLOAD_FILENAME = "Filename"//$NON-NLS-1$
 
UPLOAD_AWS_ACCESS_KEY_ID
 
 	public static final String UPLOAD_AWS_ACCESS_KEY_ID = "AWSAccessKeyId"//$NON-NLS-1$
 
UPLOAD_POLICY
 
 	public static final String UPLOAD_POLICY = "Policy"//$NON-NLS-1$
 
UPLOAD_SIGNATURE
 
 	public static final String UPLOAD_SIGNATURE = "Signature"//$NON-NLS-1$
 
UPLOAD_FILE
 
 	public static final String UPLOAD_FILE = "file"//$NON-NLS-1$
 
 	private static class SizedInputStreamBody extends InputStreamBody {
 
 		private final long size;

Parameters:
in
size
		public SizedInputStreamBody(InputStream inlong size) {
			super(innull);
			this. = size;
		}
		public long getContentLength() {
			return ;
		}
	}

Create download service
	public DownloadService() {
		super();
	}

Create download service

Parameters:
client
	public DownloadService(GitHubClient client) {
		super(client);
	}

Get download metadata for given repository and id

Parameters:
repository
id
Returns:
download
Throws:
java.io.IOException
	public Download getDownload(IRepositoryIdProvider repositoryint id)
			throws IOException {
		final String repoId = getId(repository);
		uri.append('/').append(repoId);
		uri.append('/').append(id);
		request.setUri(uri);
		request.setType(Download.class);
		return (Download.get(request).getBody();
	}

Create paged downloads request

Parameters:
repository
start
size
Returns:
request
			IRepositoryIdProvider repositoryint startint size) {
		final String repoId = getId(repository);
		uri.append('/').append(repoId);
		PagedRequest<Downloadrequest = createPagedRequest(startsize);
		request.setType(new TypeToken<List<Download>>() {
		}.getType());
		request.setUri(uri);
		return request;
	}

Get metadata for all downloads for given repository

Parameters:
repository
Returns:
non-null but possibly empty list of download metadata
Throws:
java.io.IOException
			throws IOException {
		return getAll(pageDownloads(repository));
	}

Page metadata for downloads for given repository

Parameters:
repository
Returns:
iterator over pages of downloads
		return pageDownloads(repository);
	}

Page downloads for given repository

Parameters:
repository
size
Returns:
iterator over pages of downloads
			IRepositoryIdProvider repositoryint size) {
		return pageDownloads(repositorysize);
	}

Page downloads for given repository

Parameters:
repository
start
size
Returns:
iterator over pages of downloads
			IRepositoryIdProvider repositoryint startint size) {
				startsize);
		return createPageIterator(request);
	}

Delete download with given id from given repository

Parameters:
repository
id
Throws:
java.io.IOException
	public void deleteDownload(IRepositoryIdProvider repositoryint id)
			throws IOException {
		final String repoId = getId(repository);
		uri.append('/').append(repoId);
		uri.append('/').append(id);
	}

Create a new resource for download associated with the given repository

Parameters:
repository
download
Returns:
download resource
Throws:
java.io.IOException
			Download downloadthrows IOException {
		final String repoId = getId(repository);
		uri.append('/').append(repoId);
		return .post(uri.toString(), downloadDownloadResource.class);
	}

Create client to use to upload a resource to

Returns:
non-null http client
		return client;
	}

Upload a resource to be available as the download described by the given resource.

Parameters:
resource
content
size
Throws:
java.io.IOException
	public void uploadResource(DownloadResource resourceInputStream content,
			long sizethrows IOException {
		if (resource == null)
					"Download resource cannot be null"); //$NON-NLS-1$
		if (content == null)
					"Content input stream cannot be null"); //$NON-NLS-1$
		HttpPost post = new HttpPost(resource.getS3Url());
		MultipartEntity entity = new MultipartEntity();
		entity.addPart(new StringBody(resource.getPath()));
		entity.addPart(new StringBody(resource.getAcl()));
		entity.addPart(new StringBody(resource.getName()));
				new StringBody(resource.getAccesskeyid()));
		entity.addPart(new StringBody(resource.getPolicy()));
				new StringBody(resource.getSignature()));
				new StringBody(resource.getMimeType()));
		entity.addPart(new SizedInputStreamBody(contentsize));
		post.setEntity(entity);
		HttpResponse response = client.execute(post);
		int status = response.getStatusLine().getStatusCode();
		if (status != .)
			throw new IOException("Unexpected response status of " + status); //$NON-NLS-1$
	}

Create download and set the content to be the content of given input stream. This is a convenience method that performs a createResource(org.eclipse.egit.github.core.IRepositoryIdProvider,org.eclipse.egit.github.core.Download) followed by a uploadResource(org.eclipse.egit.github.core.DownloadResource,java.io.InputStream,long) with the results.

Parameters:
repository
download metadata about the download
content raw content of the download
size size of content in the input stream
Throws:
java.io.IOException
	public void createDownload(IRepositoryIdProvider repository,
			Download downloadInputStream contentlong size)
			throws IOException {
		DownloadResource resource = createResource(repositorydownload);
		uploadResource(resourcecontentsize);
	}

Create download from content of given file.

	public void createDownload(IRepositoryIdProvider repository,
			Download downloadFile filethrows IOException {
		if (file == null)
			throw new IllegalArgumentException("File cannot be null"); //$NON-NLS-1$
		createDownload(repositorydownloadnew FileInputStream(file),
				file.length());
	}
New to GrepCode? Check out our FAQ X