下面列出了javax.ws.rs.core.MultivaluedHashMap#add() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected Response challenge(AuthenticationFlowContext context, String error) {
String useAjax = getConfigProperty(context, USE_AXJAX_CONFIG_PROPERTY, "true");
String loginHint = context.getHttpRequest().getUri().getQueryParameters().getFirst(OIDCLoginProtocol.LOGIN_HINT_PARAM);
LoginFormsProvider usernameLoginForm = createSelectUserForm(context, error)
.setAttribute("useAjax", "true".equals(useAjax));
if (loginHint != null) {
MultivaluedHashMap<String, String> formData = new MultivaluedHashMap<>();
formData.add(AuthenticationManager.FORM_USERNAME, loginHint);
usernameLoginForm.setAttribute("login", new LoginBean(formData));
}
return usernameLoginForm
.createForm("select-user-form.ftl");
}
private ODataContextImpl getMockedContextWithLocale(final String requestUri,
final String serviceRoot) throws ODataException,
URISyntaxException {
ODataContextImpl context = Mockito.mock(ODataContextImpl.class);
PathInfoImpl pathInfo = new PathInfoImpl();
pathInfo.setRequestUri(new URI(requestUri));
pathInfo.setServiceRoot(new URI(serviceRoot));
when(context.getPathInfo()).thenReturn(pathInfo);
MultivaluedHashMap<String,String> headers = new MultivaluedHashMap<String, String>();
headers.add("Accept-Language","de-DE, de;q=0.7");
when(context.getRequestHeaders()).thenReturn(headers);
List<Locale> locales = new ArrayList<Locale>();
locales.add(Locale.GERMANY);
when(context.getAcceptableLanguages()).thenReturn(locales);
return context;
}
@Override
public MultivaluedMap<String, String> getRequestHeaders() {
MultivaluedHashMap<String, String> newHeaders =
new MultivaluedHashMap<>();
for (Map.Entry<String, String> headerEntry : nettyHttpHeaders.entries()) {
if (headerEntry != null) {
newHeaders.add(headerEntry.getKey(), headerEntry.getValue());
}
}
return newHeaders;
}
/**
* Performs a POST using the specified url and form data.
*
* @param uri the uri to post to
* @param formData the data to post
* @return the client response of the post
*/
public Response post(URI uri, Map<String, String> formData) {
// convert the form data
final MultivaluedHashMap<String, String> entity = new MultivaluedHashMap();
for (String key : formData.keySet()) {
entity.add(key, formData.get(key));
}
// get the resource
Invocation.Builder builder = client.target(uri).request().accept(MediaType.APPLICATION_JSON);
// get the resource
return builder.post(Entity.form(entity));
}
protected Invocation prepareResource(final String key, final List<String> text, final String sourceLanguage, final String destLanguage) {
Invocation.Builder builder = client.target(URL).request(MediaType.APPLICATION_JSON);
final MultivaluedHashMap entity = new MultivaluedHashMap();
entity.put("text", text);
entity.add("key", key);
if ((StringUtils.isBlank(sourceLanguage))) {
entity.add("lang", destLanguage);
} else {
entity.add("lang", sourceLanguage + "-" + destLanguage);
}
return builder.buildPost(Entity.form(entity));
}
/**
* Attempts to create a token with the specified username and password.
*
* @param url the url
* @param username the username
* @param password the password
* @return response
* @throws Exception ex
*/
public Response testCreateToken(String url, String username, String password) throws Exception {
// convert the form data
MultivaluedHashMap<String, String> entity = new MultivaluedHashMap();
entity.add("username", username);
entity.add("password", password);
// get the resource
Invocation.Builder resourceBuilder = addProxiedEntities(client.target(url).request().accept(MediaType.TEXT_PLAIN));
// perform the request
return resourceBuilder.post(Entity.form(entity));
}
/**
* Issues a registration request for this NiFi instance for the instance at the baseApiUri.
*
* @param baseApiUri uri to register with
* @return response
*/
public Response issueRegistrationRequest(String baseApiUri) {
final URI uri = URI.create(String.format("%s/controller/users", baseApiUri));
// set up the query params
MultivaluedHashMap entity = new MultivaluedHashMap();
entity.add("justification", "A Remote instance of NiFi has attempted to create a reference to this NiFi. This action must be approved first.");
// get the resource
return client.target(uri).request().post(Entity.form(entity));
}
/**
* Test of getList method, of class MyResource.
*/
@Test @InSequence(1)
public void testPostAndGet() {
MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
map.add("name", "Penny");
map.add("age", "1");
target.request().post(Entity.form(map));
map.clear();
map.add("name", "Leonard");
map.add("age", "2");
target.request().post(Entity.form(map));
map.clear();
map.add("name", "Sheldon");
map.add("age", "3");
target.request().post(Entity.form(map));
Employee[] list = target.request().get(Employee[].class);
assertEquals(3, list.length);
assertEquals("Penny", list[0].getName());
assertEquals(1, list[0].getAge());
assertEquals("Leonard", list[1].getName());
assertEquals(2, list[1].getAge());
assertEquals("Sheldon", list[2].getName());
assertEquals(3, list[2].getAge());
}
/**
* Test of putToList method, of class MyResource.
*/
@Test @InSequence(3)
public void testPut() {
MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
map.add("name", "Howard");
map.add("age", "4");
target.request().post(Entity.form(map));
Employee[] list = target.request().get(Employee[].class);
assertEquals(4, list.length);
assertEquals("Howard", list[3].getName());
assertEquals(4, list[3].getAge());
}