private String generateForm(MultiValueMap<String, String> form) {
   StringBuilder builder = new StringBuilder();
   try {
     for (Iterator<String> names = form.keySet().iterator(); names.hasNext(); ) {
       String name = names.next();
       for (Iterator<String> values = form.get(name).iterator(); values.hasNext(); ) {
         String value = values.next();
         builder.append(URLEncoder.encode(name, "UTF-8"));
         if (value != null) {
           builder.append('=');
           builder.append(URLEncoder.encode(value, "UTF-8"));
           if (values.hasNext()) {
             builder.append('&');
           }
         }
       }
       if (names.hasNext()) {
         builder.append('&');
       }
     }
   } catch (UnsupportedEncodingException ex) {
     throw new IllegalStateException(ex);
   }
   return builder.toString();
 }
Example #2
0
 public URI build() {
   StringBuilder builder = new StringBuilder(url);
   try {
     if (!params.isEmpty()) {
       builder.append("?");
       boolean first = true;
       for (String key : params.keySet()) {
         if (!first) {
           builder.append("&");
         } else {
           first = false;
         }
         for (String value : params.get(key)) {
           builder.append(key + "=" + UriUtils.encodeQueryParam(value, "UTF-8"));
         }
       }
     }
     return new URI(builder.toString());
   } catch (UnsupportedEncodingException ex) {
     // should not happen, UTF-8 is always supported
     throw new IllegalStateException(ex);
   } catch (URISyntaxException ex) {
     throw new IllegalArgumentException(
         "Could not create URI from [" + builder + "]: " + ex, ex);
   }
 }
 private Header[] convertHeaders(MultiValueMap<String, String> headers) {
   List<Header> list = new ArrayList<>();
   for (String name : headers.keySet()) {
     for (String value : headers.get(name)) {
       list.add(new BasicHeader(name, value));
     }
   }
   return list.toArray(new BasicHeader[0]);
 }
  /**
   * Decode the given matrix variables unless {@link #setUrlDecode(boolean)} is set to {@code true}
   * in which case it is assumed the URL path from which the variables were extracted is already
   * decoded through a call to {@link #getLookupPathForRequest(ServerWebExchange)}.
   *
   * @param exchange current exchange
   * @param vars URI variables extracted from the URL path
   * @return the same Map or a new Map instance
   */
  public MultiValueMap<String, String> decodeMatrixVariables(
      ServerWebExchange exchange, MultiValueMap<String, String> vars) {

    if (this.urlDecode) {
      return vars;
    }
    MultiValueMap<String, String> decodedVars = new LinkedMultiValueMap<>(vars.size());
    for (String key : vars.keySet()) {
      for (String value : vars.get(key)) {
        decodedVars.add(key, decode(exchange, value));
      }
    }
    return decodedVars;
  }
 /** Return a {@link HandlerMapping} with mapped {@link HttpRequestHandler}s. */
 public AbstractHandlerMapping getHandlerMapping() {
   Map<String, Object> urlMap = new LinkedHashMap<>();
   for (ServletWebSocketHandlerRegistration registration : this.registrations) {
     MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
     for (HttpRequestHandler httpHandler : mappings.keySet()) {
       for (String pattern : mappings.get(httpHandler)) {
         urlMap.put(pattern, httpHandler);
       }
     }
   }
   WebSocketHandlerMapping hm = new WebSocketHandlerMapping();
   hm.setUrlMap(urlMap);
   hm.setOrder(this.order);
   if (this.urlPathHelper != null) {
     hm.setUrlPathHelper(this.urlPathHelper);
   }
   return hm;
 }
 @Test
 @SuppressWarnings("unchecked")
 public void getRequestOk() throws Exception {
   MockHttpServletRequest request = new MockHttpServletRequest();
   request.setMethod("GET");
   request.setParameter("foo", "bar");
   MockHttpServletResponse response = new MockHttpServletResponse();
   defaultAdapter.handleRequest(request, response);
   assertEquals(HttpServletResponse.SC_OK, response.getStatus());
   Message<?> message = requests.receive(0);
   assertNotNull(message);
   Object payload = message.getPayload();
   assertTrue(payload instanceof MultiValueMap);
   MultiValueMap<String, String> map = (MultiValueMap<String, String>) payload;
   assertEquals(1, map.size());
   assertEquals("foo", map.keySet().iterator().next());
   assertEquals(1, map.get("foo").size());
   assertEquals("bar", map.getFirst("foo"));
   assertNotNull(TestUtils.getPropertyValue(defaultAdapter, "errorChannel"));
 }