Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
048a8224
Commit
048a8224
authored
Jun 24, 2017
by
Greg Messner
Browse files
Mods to properly downloading blobs and files.
parent
03473020
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/AbstractApi.java
View file @
048a8224
...
@@ -64,6 +64,25 @@ public abstract class AbstractApi implements Constants {
...
@@ -64,6 +64,25 @@ public abstract class AbstractApi implements Constants {
}
}
}
}
/**
* Perform an HTTP GET call with the specified query parameters and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param queryParams multivalue map of request parameters
* @param accepts if non-empty will set the Accepts header to this value
* @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint
* @throws GitLabApiException if any exception occurs during execution
*/
protected
Response
getWithAccepts
(
Response
.
Status
expectedStatus
,
MultivaluedMap
<
String
,
String
>
queryParams
,
String
accepts
,
Object
...
pathArgs
)
throws
GitLabApiException
{
try
{
return
validate
(
getApiClient
().
getWithAccepts
(
queryParams
,
accepts
,
pathArgs
),
expectedStatus
);
}
catch
(
Exception
e
)
{
throw
handle
(
e
);
}
}
/**
/**
* Perform an HTTP GET call with the specified query parameters and URL, returning
* Perform an HTTP GET call with the specified query parameters and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
* a ClientResponse instance with the data returned from the endpoint.
...
...
src/main/java/org/gitlab4j/api/GitLabApiClient.java
View file @
048a8224
...
@@ -285,6 +285,34 @@ public class GitLabApiClient {
...
@@ -285,6 +285,34 @@ public class GitLabApiClient {
return
(
invocation
(
url
,
queryParams
).
get
());
return
(
invocation
(
url
,
queryParams
).
get
());
}
}
/**
* Perform an HTTP GET call with the specified query parameters and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param queryParams multivalue map of request parameters
* @param accepts if non-empty will set the Accepts header to this value
* @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL
*/
protected
Response
getWithAccepts
(
MultivaluedMap
<
String
,
String
>
queryParams
,
String
accepts
,
Object
...
pathArgs
)
throws
IOException
{
URL
url
=
getApiUrl
(
pathArgs
);
return
(
getWithAccepts
(
queryParams
,
url
,
accepts
));
}
/**
* Perform an HTTP GET call with the specified query parameters and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param queryParams multivalue map of request parameters
* @param url the fully formed path to the GitLab API endpoint
* @param accepts if non-empty will set the Accepts header to this value
* @return a ClientResponse instance with the data returned from the endpoint
*/
protected
Response
getWithAccepts
(
MultivaluedMap
<
String
,
String
>
queryParams
,
URL
url
,
String
accepts
)
{
return
(
invocation
(
url
,
queryParams
,
accepts
).
get
());
}
/**
/**
* Perform an HTTP POST call with the specified form data and path objects, returning
* Perform an HTTP POST call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
* a ClientResponse instance with the data returned from the endpoint.
...
@@ -392,6 +420,10 @@ public class GitLabApiClient {
...
@@ -392,6 +420,10 @@ public class GitLabApiClient {
}
}
protected
Invocation
.
Builder
invocation
(
URL
url
,
MultivaluedMap
<
String
,
String
>
queryParams
)
{
protected
Invocation
.
Builder
invocation
(
URL
url
,
MultivaluedMap
<
String
,
String
>
queryParams
)
{
return
(
invocation
(
url
,
queryParams
,
MediaType
.
APPLICATION_JSON
));
}
protected
Invocation
.
Builder
invocation
(
URL
url
,
MultivaluedMap
<
String
,
String
>
queryParams
,
String
accept
)
{
if
(
apiClient
==
null
)
{
if
(
apiClient
==
null
)
{
apiClient
=
ClientBuilder
.
newBuilder
().
withConfig
(
clientConfig
).
sslContext
(
getSslContext
()).
hostnameVerifier
(
new
AcceptAllHostnameVerifier
()).
build
();
apiClient
=
ClientBuilder
.
newBuilder
().
withConfig
(
clientConfig
).
sslContext
(
getSslContext
()).
hostnameVerifier
(
new
AcceptAllHostnameVerifier
()).
build
();
...
@@ -404,7 +436,10 @@ public class GitLabApiClient {
...
@@ -404,7 +436,10 @@ public class GitLabApiClient {
}
}
}
}
return
(
target
.
request
().
header
(
PRIVATE_TOKEN_HEADER
,
privateToken
).
accept
(
MediaType
.
APPLICATION_JSON
));
if
(
accept
==
null
||
accept
.
trim
().
length
()
==
0
)
return
(
target
.
request
().
header
(
PRIVATE_TOKEN_HEADER
,
privateToken
));
else
return
(
target
.
request
().
header
(
PRIVATE_TOKEN_HEADER
,
privateToken
).
accept
(
accept
));
}
}
protected
class
AcceptAllHostnameVerifier
implements
HostnameVerifier
{
protected
class
AcceptAllHostnameVerifier
implements
HostnameVerifier
{
...
...
src/main/java/org/gitlab4j/api/RepositoryApi.java
View file @
048a8224
...
@@ -9,6 +9,7 @@ import java.util.List;
...
@@ -9,6 +9,7 @@ import java.util.List;
import
javax.ws.rs.core.Form
;
import
javax.ws.rs.core.Form
;
import
javax.ws.rs.core.GenericType
;
import
javax.ws.rs.core.GenericType
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.Response
;
import
javax.ws.rs.core.Response
;
import
org.gitlab4j.api.GitLabApi.ApiVersion
;
import
org.gitlab4j.api.GitLabApi.ApiVersion
;
...
@@ -388,23 +389,6 @@ public class RepositoryApi extends AbstractApi {
...
@@ -388,23 +389,6 @@ public class RepositoryApi extends AbstractApi {
return
(
new
Pager
<
TreeItem
>(
this
,
TreeItem
.
class
,
itemsPerPage
,
formData
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"tree"
));
return
(
new
Pager
<
TreeItem
>(
this
,
TreeItem
.
class
,
itemsPerPage
,
formData
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"tree"
));
}
}
/**
* Get the raw file contents for a file by commit sha and path.
*
* GET /projects/:id/repository/blobs/:sha
*
* @param projectId the ID of the project
* @param commitOrBranchName the commit or branch name to get the file contents for
* @param filepath the path of the file to get
* @return a string with the file content for the specified file
* @throws GitLabApiException if any exception occurs
*/
public
InputStream
getRawFileContent
(
Integer
projectId
,
String
commitOrBranchName
,
String
filepath
)
throws
GitLabApiException
{
Form
formData
=
new
GitLabApiForm
().
withParam
(
"filepath"
,
filepath
,
true
);
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"blobs"
,
commitOrBranchName
);
return
(
response
.
readEntity
(
InputStream
.
class
));
}
/**
/**
* Get the raw file contents for a blob by blob SHA.
* Get the raw file contents for a blob by blob SHA.
*
*
...
@@ -416,7 +400,8 @@ public class RepositoryApi extends AbstractApi {
...
@@ -416,7 +400,8 @@ public class RepositoryApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
*/
*/
public
InputStream
getRawBlobContent
(
Integer
projectId
,
String
sha
)
throws
GitLabApiException
{
public
InputStream
getRawBlobContent
(
Integer
projectId
,
String
sha
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"raw_blobs"
,
sha
);
Response
response
=
getWithAccepts
(
Response
.
Status
.
OK
,
null
,
MediaType
.
MEDIA_TYPE_WILDCARD
,
"projects"
,
projectId
,
"repository"
,
"blobs"
,
sha
,
"raw"
);
return
(
response
.
readEntity
(
InputStream
.
class
));
return
(
response
.
readEntity
(
InputStream
.
class
));
}
}
...
@@ -433,7 +418,8 @@ public class RepositoryApi extends AbstractApi {
...
@@ -433,7 +418,8 @@ public class RepositoryApi extends AbstractApi {
*/
*/
public
InputStream
getRepositoryArchive
(
Integer
projectId
,
String
sha
)
throws
GitLabApiException
{
public
InputStream
getRepositoryArchive
(
Integer
projectId
,
String
sha
)
throws
GitLabApiException
{
Form
formData
=
new
GitLabApiForm
().
withParam
(
"sha"
,
sha
);
Form
formData
=
new
GitLabApiForm
().
withParam
(
"sha"
,
sha
);
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"archive"
);
Response
response
=
getWithAccepts
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
MediaType
.
MEDIA_TYPE_WILDCARD
,
"projects"
,
projectId
,
"repository"
,
"archive"
);
return
(
response
.
readEntity
(
InputStream
.
class
));
return
(
response
.
readEntity
(
InputStream
.
class
));
}
}
...
@@ -452,7 +438,8 @@ public class RepositoryApi extends AbstractApi {
...
@@ -452,7 +438,8 @@ public class RepositoryApi extends AbstractApi {
public
File
getRepositoryArchive
(
Integer
projectId
,
String
sha
,
File
directory
)
throws
GitLabApiException
{
public
File
getRepositoryArchive
(
Integer
projectId
,
String
sha
,
File
directory
)
throws
GitLabApiException
{
Form
formData
=
new
GitLabApiForm
().
withParam
(
"sha"
,
sha
);
Form
formData
=
new
GitLabApiForm
().
withParam
(
"sha"
,
sha
);
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"archive"
);
Response
response
=
getWithAccepts
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
MediaType
.
MEDIA_TYPE_WILDCARD
,
"projects"
,
projectId
,
"repository"
,
"archive"
);
try
{
try
{
...
...
src/main/java/org/gitlab4j/api/RepositoryFileApi.java
View file @
048a8224
package
org.gitlab4j.api
;
package
org.gitlab4j.api
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.file.Files
;
import
java.nio.file.StandardCopyOption
;
import
javax.ws.rs.core.Form
;
import
javax.ws.rs.core.Form
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.Response
;
import
javax.ws.rs.core.Response
;
import
org.gitlab4j.api.GitLabApi.ApiVersion
;
import
org.gitlab4j.api.GitLabApi.ApiVersion
;
...
@@ -137,6 +144,59 @@ public class RepositoryFileApi extends AbstractApi {
...
@@ -137,6 +144,59 @@ public class RepositoryFileApi extends AbstractApi {
delete
(
expectedStatus
,
form
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"files"
);
delete
(
expectedStatus
,
form
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"files"
);
}
}
/**
* Get the raw file for the file by commit sha and path. Thye file will be saved to the specified directory.
* If the file already exists in the directory it will be overwritten.
*
* GET /projects/:id/repository/archive
*
* @param projectId the ID of the project
* @param commitOrBranchName the commit or branch name to get the file for
* @param filepath the path of the file to get
* @return a File instance pointing to the download of the specified file
* @throws GitLabApiException if any exception occurs
*/
public
File
getRawFile
(
Integer
projectId
,
String
commitOrBranchName
,
String
filepath
,
File
directory
)
throws
GitLabApiException
{
Form
formData
=
new
GitLabApiForm
().
withParam
(
"ref"
,
commitOrBranchName
,
true
);
Response
response
=
getWithAccepts
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
MediaType
.
MEDIA_TYPE_WILDCARD
,
"projects"
,
projectId
,
"repository"
,
"files"
,
filepath
,
"raw"
);
try
{
if
(
directory
==
null
)
directory
=
new
File
(
System
.
getProperty
(
"java.io.tmpdir"
));
String
filename
=
new
File
(
filepath
).
getName
();
File
file
=
new
File
(
directory
,
filename
);
InputStream
in
=
response
.
readEntity
(
InputStream
.
class
);
Files
.
copy
(
in
,
file
.
toPath
(),
StandardCopyOption
.
REPLACE_EXISTING
);
return
(
file
);
}
catch
(
IOException
ioe
)
{
throw
new
GitLabApiException
(
ioe
);
}
}
/**
* Get the raw file contents for a file by commit sha and path.
*
* GET /projects/:id/repository/blobs/:sha
*
* @param projectId the ID of the project
* @param commitOrBranchName the commit or branch name to get the file contents for
* @param filepath the path of the file to get
* @return a string with the file content for the specified file
* @throws GitLabApiException if any exception occurs
*/
public
InputStream
getRawFile
(
Integer
projectId
,
String
commitOrBranchName
,
String
filepath
)
throws
GitLabApiException
{
Form
formData
=
new
GitLabApiForm
().
withParam
(
"ref"
,
commitOrBranchName
,
true
);
Response
response
=
getWithAccepts
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
MediaType
.
MEDIA_TYPE_WILDCARD
,
"projects"
,
projectId
,
"repository"
,
"files"
,
filepath
,
"raw"
);
return
(
response
.
readEntity
(
InputStream
.
class
));
}
private
Form
file2form
(
RepositoryFile
file
,
String
branchName
,
String
commitMessage
)
{
private
Form
file2form
(
RepositoryFile
file
,
String
branchName
,
String
commitMessage
)
{
Form
form
=
new
Form
();
Form
form
=
new
Form
();
addFormParam
(
form
,
"file_path"
,
file
.
getFilePath
(),
true
);
addFormParam
(
form
,
"file_path"
,
file
.
getFilePath
(),
true
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment