下面列出了javax.ws.rs.core.Form#asMap() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected String getTokenFromFormData(Message message) {
String method = (String)message.get(Message.HTTP_REQUEST_METHOD);
String type = (String)message.get(Message.CONTENT_TYPE);
if (type != null && MediaType.APPLICATION_FORM_URLENCODED.startsWith(type)
&& method != null && (method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT))) {
try {
FormEncodingProvider<Form> provider = new FormEncodingProvider<>(true);
Form form = FormUtils.readForm(provider, message);
MultivaluedMap<String, String> formData = form.asMap();
String token = formData.getFirst(OAuthConstants.ACCESS_TOKEN);
if (token != null) {
FormUtils.restoreForm(provider, form, message);
return token;
}
} catch (Exception ex) {
// the exception will be thrown below
}
}
AuthorizationUtils.throwAuthorizationFailure(supportedSchemes, realm);
return null;
}
public OOBAuthorizationResponse readFrom(
Class<OOBAuthorizationResponse> clazz, Type genericType, Annotation[] annotations, MediaType mt,
MultivaluedMap<String, String> headers, InputStream is) throws IOException {
Form form = formProvider.readFrom(Form.class, Form.class, annotations, mt, headers, is);
MultivaluedMap<String, String> data = form.asMap();
OOBAuthorizationResponse resp = new OOBAuthorizationResponse();
resp.setRequestToken(data.getFirst(OAuth.OAUTH_TOKEN));
resp.setVerifier(data.getFirst(OAuth.OAUTH_VERIFIER));
resp.setState(data.getFirst(OAuthConstants.X_OAUTH_STATE));
return resp;
}
@Override
public void filter(ContainerRequestContext context) {
Message message = JAXRSUtils.getCurrentMessage();
Form form = readFormData(message);
MultivaluedMap<String, String> formData = form.asMap();
String assertionType = formData.getFirst(Constants.CLIENT_AUTH_ASSERTION_TYPE);
String decodedAssertionType = assertionType != null ? HttpUtils.urlDecode(assertionType) : null;
if (decodedAssertionType == null || !Constants.CLIENT_AUTH_SAML2_BEARER.equals(decodedAssertionType)) {
throw ExceptionUtils.toNotAuthorizedException(null, null);
}
String assertion = formData.getFirst(Constants.CLIENT_AUTH_ASSERTION_PARAM);
Element token = readToken(message, assertion);
String clientId = formData.getFirst(OAuthConstants.CLIENT_ID);
validateToken(message, token, clientId);
formData.remove(OAuthConstants.CLIENT_ID);
formData.remove(Constants.CLIENT_AUTH_ASSERTION_PARAM);
formData.remove(Constants.CLIENT_AUTH_ASSERTION_TYPE);
// restore input stream
try {
FormUtils.restoreForm(provider, form, message);
} catch (Exception ex) {
throw ExceptionUtils.toNotAuthorizedException(null, null);
}
}
@Override
public void filter(ContainerRequestContext context) {
Message message = JAXRSUtils.getCurrentMessage();
Form form = readFormData(message);
MultivaluedMap<String, String> formData = form.asMap();
String assertion = formData.getFirst(SAML_ELEMENT);
handleToken(message, assertion);
// redirect if needed
String samlRequestURI = formData.getFirst(SAML_RELAY_STATE);
if (samlRequestURI != null) {
// RelayState may actually represent a reference to a transient local state
// containing the actual REQUEST URI client was using before being redirected
// back to IDP - at the moment assume it's URI
UriInfoImpl ui = new UriInfoImpl(message);
if (!samlRequestURI.startsWith(ui.getBaseUri().toString())) {
context.abortWith(Response.status(302).location(URI.create(samlRequestURI)).build());
return;
}
}
formData.remove(SAML_ELEMENT);
formData.remove(SAML_RELAY_STATE);
// restore input stream
try {
FormUtils.restoreForm(provider, form, message);
} catch (Exception ex) {
throwFault(ex.getMessage(), ex);
}
}
@Test
public void testReadFromForm() throws Exception {
FormEncodingProvider<Form> ferp = new FormEncodingProvider<>();
InputStream is = getClass().getResourceAsStream("singleValPostBody.txt");
Form form = ferp.readFrom(Form.class, null,
new Annotation[]{}, MediaType.APPLICATION_FORM_URLENCODED_TYPE, null, is);
MultivaluedMap<String, String> mvMap = form.asMap();
assertEquals("Wrong entry for foo", "bar", mvMap.getFirst("foo"));
assertEquals("Wrong entry for boo", "far", mvMap.getFirst("boo"));
}
/**
* Get a Pager of repository commits in a project.
* <p>
* GET /projects/:id/repository/commits
*
* @param projectId the project ID to get the list of commits for
* @param ref the name of a repository branch or tag or if not given the default branch
* @param since only commits after or on this date will be returned
* @param until only commits before or on this date will be returned
* @param itemsPerPage the number of Commit instances that will be fetched per page
* @return a Pager containing the commits for the specified project ID
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public Pager<Commit> getCommits(int projectId, String ref, Date since, Date until, int itemsPerPage) throws GitLabApiException {
Form formData = new GitLabApiForm()
.withParam("ref_name", ref)
.withParam("since", ISO8601.toString(since, false))
.withParam("until", ISO8601.toString(until, false));
return (new Pager<Commit>(this, Commit.class, itemsPerPage, formData.asMap(), "projects", projectId, "repository", "commits"));
}
/**
* Get a Pager of repository files and directories in a project.
* <p>
* GET /projects/:id/repository/tree
* <p>
* id (required) - The ID of a project
* path (optional) - The path inside repository. Used to get contend of subdirectories
* ref_name (optional) - The name of a repository branch or tag or if not given the default branch
* recursive (optional) - Boolean value used to get a recursive tree (false by default)
*
* @param projectId the ID of the project to get the files for
* @param filePath the path inside repository, used to get content of subdirectories
* @param refName the name of a repository branch or tag or if not given the default branch
* @param recursive flag to get a recursive tree or not
* @param itemsPerPage the number of Project instances that will be fetched per page
* @return a tree with the directories and files of a project
* @throws GitLabApiException if any exception occurs
*/
public Pager<TreeItem> getTree(Integer projectId, String filePath, String refName, Boolean recursive, int itemsPerPage) throws GitLabApiException {
Form formData = new GitLabApiForm()
.withParam("id", projectId, true)
.withParam("path", filePath, false)
.withParam("ref_name", refName, false)
.withParam("recursive", recursive, false);
return (new Pager<TreeItem>(this, TreeItem.class, itemsPerPage, formData.asMap(), "projects", projectId, "repository", "tree"));
}
/**
* Get a list of repository tags from a project, sorted by name in reverse alphabetical order.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/tags</code></pre>
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param orderBy return tags ordered by name or updated fields. Default is updated
* @param sortOrder return tags sorted in asc or desc order. Default is desc
* @param search return list of tags matching the search criteria
* @param itemsPerPage the number of Project instances that will be fetched per page
* @return the Pager of tags for the specified project ID
* @throws GitLabApiException if any exception occurs
* @since GitLab 11.8
*/
public Pager<Tag> getTags(Object projectIdOrPath, TagOrderBy orderBy, SortOrder sortOrder, String search, int itemsPerPage) throws GitLabApiException {
Form formData = new GitLabApiForm()
.withParam("order_by", orderBy)
.withParam("sort", sortOrder)
.withParam("search", search);
return (new Pager<Tag>(this, Tag.class, itemsPerPage, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags"));
}
/**
* Get a Pager of the group audit events viewable by Maintainer or an Owner of the group.
*
* <pre><code>GET /groups/:id/audit_events</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
* @param created_after Group audit events created on or after the given time.
* @param created_before Group audit events created on or before the given time.
* @param itemsPerPage the number of Audit Event instances that will be fetched per page
* @return a Pager of group Audit events
* @throws GitLabApiException if any exception occurs
*/
public Pager<AuditEvent> getAuditEvents(Object groupIdOrPath, Date created_after, Date created_before, int itemsPerPage) throws GitLabApiException {
Form form = new GitLabApiForm()
.withParam("created_before", ISO8601.toString(created_after, false))
.withParam("created_after", ISO8601.toString(created_before, false));
return (new Pager<AuditEvent>(this, AuditEvent.class, itemsPerPage, form.asMap(),
"groups", getGroupIdOrPath(groupIdOrPath), "audit_events"));
}
/**
* Get a Pager of visible direct subgroups in this group.
*
* <pre><code>GitLab Endpoint: GET /groups/:id/subgroups</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required
* @param skipGroups skip the group IDs passed
* @param allAvailable show all the groups you have access to (defaults to false for authenticated users)
* @param search return the list of authorized groups matching the search criteria
* @param orderBy order groups by NAME or PATH. Default is NAME
* @param sortOrder order groups in ASC or DESC order. Default is ASC
* @param statistics include group statistics (admins only)
* @param owned limit to groups owned by the current user
* @param itemsPerPage the number of Group instances that will be fetched per page
* @return a Pager containing matching Group instances
* @throws GitLabApiException if any exception occurs
* @since GitLab 10.3.0
*/
public Pager<Group> getSubGroups(Object groupIdOrPath, List<Integer> skipGroups, Boolean allAvailable, String search,
GroupOrderBy orderBy, SortOrder sortOrder, Boolean statistics, Boolean owned, int itemsPerPage) throws GitLabApiException {
Form formData = new GitLabApiForm()
.withParam("skip_groups", skipGroups)
.withParam("all_available", allAvailable)
.withParam("search", search)
.withParam("order_by", orderBy)
.withParam("sort_order", sortOrder)
.withParam("statistics", statistics)
.withParam("owned", owned);
return (new Pager<Group>(this, Group.class, itemsPerPage, formData.asMap(), "groups", getGroupIdOrPath(groupIdOrPath), "subgroups"));
}
/**
* Get a Pager of the specified repository commits in a project
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/commits</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param ref the name of a repository branch or tag or if not given the default branch
* @param since only commits after or on this date will be returned
* @param until only commits before or on this date will be returned
* @param path the path to file of a project
* @param all retrieve every commit from the repository
* @param withStats stats about each commit will be added to the response
* @param firstParent follow only the first parent commit upon seeing a merge commit
* @param itemsPerPage the number of Commit instances that will be fetched per page
* @return a Pager containing the commits for the specified project ID
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public Pager<Commit> getCommits(Object projectIdOrPath, String ref, Date since, Date until,
String path, Boolean all, Boolean withStats, Boolean firstParent, int itemsPerPage) throws GitLabApiException {
Form formData = new GitLabApiForm()
.withParam("ref_name", ref)
.withParam("since", ISO8601.toString(since, false))
.withParam("until", ISO8601.toString(until, false))
.withParam("path", (path == null ? null : urlEncode(path)))
.withParam("all", all)
.withParam("with_stats", withStats)
.withParam("first_parent", firstParent);
return (new Pager<Commit>(this, Commit.class, itemsPerPage, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits"));
}
/**
* Get a Pager of repository files and directories in a project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/tree</code></pre>
*
* id (required) - The ID of a project
* path (optional) - The path inside repository. Used to get contend of subdirectories
* ref_name (optional) - The name of a repository branch or tag or if not given the default branch
* recursive (optional) - Boolean value used to get a recursive tree (false by default)
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param filePath the path inside repository, used to get content of subdirectories
* @param refName the name of a repository branch or tag or if not given the default branch
* @param recursive flag to get a recursive tree or not
* @param itemsPerPage the number of Project instances that will be fetched per page
* @return a tree with the directories and files of a project
* @throws GitLabApiException if any exception occurs
*/
public Pager<TreeItem> getTree(Object projectIdOrPath, String filePath, String refName, Boolean recursive, int itemsPerPage) throws GitLabApiException {
Form formData = new GitLabApiForm()
.withParam("id", getProjectIdOrPath(projectIdOrPath), true)
.withParam("path", filePath, false)
.withParam(isApiVersion(ApiVersion.V3) ? "ref_name" : "ref", (refName != null ? urlEncode(refName) : null), false)
.withParam("recursive", recursive, false);
return (new Pager<TreeItem>(this, TreeItem.class, itemsPerPage, formData.asMap(), "projects",
getProjectIdOrPath(projectIdOrPath), "repository", "tree"));
}
/**
* Get all merge requests for the specified project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/merge_requests</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param state the state parameter can be used to get only merge requests with a given state (opened, closed, or merged) or all of them (all).
* @param itemsPerPage the number of MergeRequest instances that will be fetched per page
* @return all merge requests for the specified project
* @throws GitLabApiException if any exception occurs
*/
public Pager<MergeRequest> getMergeRequests(Object projectIdOrPath, MergeRequestState state, int itemsPerPage) throws GitLabApiException {
Form formData = new GitLabApiForm()
.withParam("state", state);
return (new Pager<MergeRequest>(this, MergeRequest.class, itemsPerPage, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "merge_requests"));
}
/**
* Get a Pager of projects accessible by the authenticated user that match the provided search string.
* <p>
* GET /projects?search=search
*
* @param search the project name search criteria
* @param itemsPerPage the number of Project instances that will be fetched per page
* @return a Pager of projects accessible by the authenticated user that match the provided search string
* @throws GitLabApiException if any exception occurs
*/
public Pager<Project> getProjects(String search, int itemsPerPage) throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("search", search);
return (new Pager<Project>(this, Project.class, itemsPerPage, formData.asMap(), "projects"));
}
/**
* Get all groups that match your string in their name or path.
*
* @param search the group name or path search criteria
* @param itemsPerPage the number of Group instances that will be fetched per page
* @return a List containing matching Group instances
* @throws GitLabApiException if any exception occurs
*/
public Pager<Group> getGroups(String search, int itemsPerPage) throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("search", search);
return (new Pager<Group>(this, Group.class, itemsPerPage, formData.asMap(), "groups"));
}
/**
* Get all merge requests for the specified project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/merge_requests</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param state the state parameter can be used to get only merge requests with a given state (opened, closed, or merged) or all of them (all).
* @param itemsPerPage the number of MergeRequest instances that will be fetched per page
* @return all merge requests for the specified project
* @throws GitLabApiException if any exception occurs
*/
public Pager<MergeRequest> getMergeRequests(Object projectIdOrPath, MergeRequestState state, int itemsPerPage) throws GitLabApiException {
Form formData = new GitLabApiForm()
.withParam("state", state);
return (new Pager<MergeRequest>(this, MergeRequest.class, itemsPerPage, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "merge_requests"));
}
/**
* Get a Pager of projects that the authenticated user is a member of.
*
* <pre><code>GitLab Endpoint: GET /projects?membership=true</code></pre>
*
* @param itemsPerPage the number of Project instances that will be fetched per page
* @return a Pager o Project instances that the authenticated user is a member of
* @throws GitLabApiException if any exception occurs
*/
public Pager<Project> getMemberProjects(int itemsPerPage) throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("membership", true);
return (new Pager<Project>(this, Project.class, itemsPerPage, formData.asMap(), "projects"));
}
/**
* Get a Pager of projects owned by the authenticated user.
*
* <pre><code>GitLab Endpoint: GET /projects?owned=true</code></pre>
*
* @param itemsPerPage the number of Project instances that will be fetched per page
* @return a list of projects owned by the authenticated user
* @throws GitLabApiException if any exception occurs
*/
public Pager<Project> getOwnedProjects(int itemsPerPage) throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("owned", true);
return (new Pager<Project>(this, Project.class, itemsPerPage, formData.asMap(), "projects"));
}
/**
* Get a Pager of references (from branches or tags) a commit is pushed to.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/commits/:sha/refs?type=:refType</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param sha a commit hash or name of a branch or tag
* @param refType the scope of commits. Possible values branch, tag, all. Default is all.
* @param itemsPerPage the number of Commit instances that will be fetched per page
* @return a Pager of references (from branches or tags) a commit is pushed to
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
* @since Gitlab 10.6
*/
public Pager<CommitRef> getCommitRefs(Object projectIdOrPath, String sha, CommitRef.RefType refType, int itemsPerPage) throws GitLabApiException {
Form form = new GitLabApiForm().withParam("type", refType);
return (new Pager<CommitRef>(this, CommitRef.class, itemsPerPage, form.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits", urlEncode(sha), "refs"));
}
/**
* Get all groups that match your string in their name or path.
*
* @param search the group name or path search criteria
* @param itemsPerPage the number of Group instances that will be fetched per page
* @return a Pager containing matching Group instances
* @throws GitLabApiException if any exception occurs
*/
public Pager<Group> getGroups(String search, int itemsPerPage) throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("search", search);
return (new Pager<Group>(this, Group.class, itemsPerPage, formData.asMap(), "groups"));
}