Package com.sun.jersey.api.client.WebResource

Examples of com.sun.jersey.api.client.WebResource.Builder


    formData.add("code", code);
    formData.add("redirect_uri", env.getProperty("redirect_uri"));

    String auth = "Basic ".concat(new String(Base64.encodeBase64(settings.getOauthKey().concat(":")
            .concat(settings.getOauthSecret()).getBytes())));
    Builder builder = client.resource(settings.getAccessTokenEndPoint()).header(AUTHORIZATION, auth)
            .type(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
    OutBoundHeaders headers = getHeadersCopy(builder);
    ClientResponse clientResponse = builder.post(ClientResponse.class, formData);
    String json = IOUtils.toString(clientResponse.getEntityInputStream());
    HashMap map = mapper.readValue(json, HashMap.class);
    settings.setStep("step3");
    settings.setAccessToken((String) map.get("access_token"));
    modelMap.put(SETTINGS, settings);
View Full Code Here


  }

  @RequestMapping(value = "test", method = RequestMethod.POST, params = "step3")
  public String step3(ModelMap modelMap, @ModelAttribute("settings")
  ClientSettings settings, HttpServletRequest request, HttpServletResponse response) throws IOException {
    Builder builder = client.resource(settings.getRequestURL())
            .header(AUTHORIZATION, "bearer ".concat(settings.getAccessToken()))
            .type(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
    OutBoundHeaders headers = getHeadersCopy(builder);
    ClientResponse clientResponse = builder.get(ClientResponse.class);
    String json = IOUtils.toString(clientResponse.getEntityInputStream());
    settings.setStep("step3");
    modelMap.put(SETTINGS, settings);
    modelMap.put("requestInfo", "Method: GET".concat(BR).concat("URL: ").concat(settings.getRequestURL()).concat(BR)
            .concat("Headers: ").concat(headers.toString()));
View Full Code Here

                b.append(escapeString(key)).append("=").append(escapeString(value));
            }
        }
        String querystring = b.toString();

        Builder builder = client.resource(host + path + querystring).accept("application/json");
        for (String key : headerParams.keySet()) {
            builder.header(key, headerParams.get(key));
        }

        for (String key : defaultHeaderMap.keySet()) {
            if (!headerParams.containsKey(key)) {
                builder.header(key, defaultHeaderMap.get(key));
            }
        }
        if (workspaceId != null) {
            builder.header("Lumify-Workspace-Id", workspaceId);
        }
        if (jSessionId != null) {
            builder.header("Cookie", "JSESSIONID=" + jSessionId);
        }

        ClientResponse response = null;

        response = (ClientResponse) builder.get(ClientResponse.class);

        if (response.getClientResponseStatus() == ClientResponse.Status.NO_CONTENT) {
            return null;
        } else if (response.getClientResponseStatus().getFamily() == Family.SUCCESSFUL) {
            return response.getEntityInputStream();
View Full Code Here

                b.append(escapeString(key)).append("=").append(escapeString(value));
            }
        }
        String querystring = b.toString();

        Builder builder = client.resource(host + path + querystring).accept("application/json");
        for (String key : headerParams.keySet()) {
            builder.header(key, headerParams.get(key));
        }

        for (String key : defaultHeaderMap.keySet()) {
            if (!headerParams.containsKey(key)) {
                builder.header(key, defaultHeaderMap.get(key));
            }
        }
        if (workspaceId != null) {
            builder.header("Lumify-Workspace-Id", workspaceId);
        }
        if (jSessionId != null) {
            builder.header("Cookie", "JSESSIONID=" + jSessionId);
        }

        ClientResponse response = null;

        if ("GET".equals(method)) {
            response = (ClientResponse) builder.get(ClientResponse.class);
        } else if ("POST".equals(method)) {
            builder.header("Lumify-CSRF-Token", this.csrfToken);
            if (body == null) {
                response = builder.post(ClientResponse.class, serialize(body));
            } else if (body instanceof FormDataMultiPart) {
                response = builder.type(contentType).post(ClientResponse.class, body);
            } else {
                response = builder.type(contentType).post(ClientResponse.class, serialize(body));
            }
        } else if ("PUT".equals(method)) {
            builder.header("Lumify-CSRF-Token", this.csrfToken);
            if (body == null) {
                response = builder.put(ClientResponse.class, serialize(body));
            } else {
                if ("application/x-www-form-urlencoded".equals(contentType)) {
                    StringBuilder formParamBuilder = new StringBuilder();

                    // encode the form params
                    for (String key : formParams.keySet()) {
                        String value = formParams.get(key);
                        if (value != null && !"".equals(value.trim())) {
                            if (formParamBuilder.length() > 0) {
                                formParamBuilder.append("&");
                            }
                            try {
                                formParamBuilder.append(URLEncoder.encode(key, "utf8")).append("=").append(URLEncoder.encode(value, "utf8"));
                            } catch (Exception e) {
                                // move on to next
                            }
                        }
                    }
                    response = builder.type(contentType).put(ClientResponse.class, formParamBuilder.toString());
                } else
                    response = builder.type(contentType).put(ClientResponse.class, serialize(body));
            }
        } else if ("DELETE".equals(method)) {
            builder.header("Lumify-CSRF-Token", this.csrfToken);
            if (body == null) {
                response = builder.delete(ClientResponse.class, serialize(body));
            } else {
                response = builder.type(contentType).delete(ClientResponse.class, serialize(body));
            }
        } else {
            throw new ApiException(500, "unknown method type " + method);
        }
        if (response.getClientResponseStatus() == ClientResponse.Status.NO_CONTENT) {
View Full Code Here

        }
        WebResource aResource = apiClient.resource(resourceURL);


        //set the required HTTP headers
        Builder builder = aResource.type("application/json");
        for(String key : headerMap.keySet()){
            builder.header(key, headerMap.get(key));
        }
        if(headerParams != null){
            for(String key : headerParams.keySet()){
                builder.header(key, headerParams.get(key));
            }
        }

        ClientResponse clientResponse = null;
        if(method.equals(GET)) {
          clientResponse =  builder.get(ClientResponse.class);
        }else if (method.equals(POST)) {
          clientResponse =  builder.post(ClientResponse.class, serialize(postData));
        }else if (method.equals(PUT)) {
          clientResponse =  builder.put(ClientResponse.class, serialize(postData));
        }else if (method.equals(DELETE)) {
          clientResponse =  builder.delete(ClientResponse.class);
        }
       
        //process the response
        if(clientResponse.getClientResponseStatus() == ClientResponse.Status.OK) {
          String response = clientResponse.getEntity(String.class);
View Full Code Here

        WebResource wr = r.path(path).queryParam("dataformat", encoding);
        if (data == null) {
            wr = wr.queryParam("null", "true");
        }

        Builder builder = wr.accept(accept)
            .type(MediaType.APPLICATION_OCTET_STREAM);

        ClientResponse cr;
        if (data == null) {
            cr = builder.put(ClientResponse.class);
        } else {
            // this shouldn't be necessary (wrapping data with string)
            // but without it there are problems on the server - ie it
            // hangs for 30 seconds and doesn't get the data.
            // TODO investigate
            cr = builder.put(ClientResponse.class, new String(data));
        }
        assertEquals(expectedStatus, cr.getClientResponseStatus());

        if (expectedStat == null) {
            return;
View Full Code Here

        String name = "roottest-create";
        byte[] data = "foo".getBytes();

        WebResource wr = r.path(path).queryParam("dataformat", "utf8")
            .queryParam("name", name);
        Builder builder = wr.accept(MediaType.APPLICATION_JSON);

        ClientResponse cr;
        cr = builder.post(ClientResponse.class, data);
        assertEquals(ClientResponse.Status.CREATED, cr.getClientResponseStatus());

        ZPath zpath = cr.getEntity(ZPath.class);
        assertEquals(new ZPath(path + name), zpath);
        assertEquals(r.path(path).toString(), zpath.uri);
View Full Code Here

        }
        if (sequence) {
            wr = wr.queryParam("sequence", "true");
        }

        Builder builder = wr.accept(accept);

        ClientResponse cr;
        if (data == null) {
            cr = builder.post(ClientResponse.class);
        } else {
            cr = builder.post(ClientResponse.class, data);
        }
        assertEquals(expectedStatus, cr.getClientResponseStatus());

        if (expectedPath == null) {
            return;
View Full Code Here

            .append(escapeString(value));
      }
    }
    String querystring = b.toString();

    Builder builder = client.resource(host + path + querystring).accept("application/json");
   
    // add basic header if not present
    String user = null;
    String password = null;
    if (headerParams.containsKey("BASIC"))
    {
      user = headerParams.get("BASIC").split(":")[0];
      password = headerParams.get("BASIC").split(":")[1];
      headerParams.remove("BASIC");
      client.addFilter(new HTTPBasicAuthFilter(user, password));
    }
    else if (!headerParams.containsKey("Authorization")) // add oauth header if not present
    {
      headerParams.put("Authorization", "Bearer " + findSystemDefaultAccessToken());
    }
   
    for (String key : headerParams.keySet()) {
      builder.header(key, headerParams.get(key));
    }

    for (String key : defaultHeaderMap.keySet()) {
      if (!headerParams.containsKey(key)) {
        builder.header(key, defaultHeaderMap.get(key));
      }
    }
    ClientResponse response = null;

    if ("GET".equals(method)) {
      response = (ClientResponse) builder.get(ClientResponse.class);
    } else if ("POST".equals(method)) {
      if (body == null)
        response = builder.post(ClientResponse.class, serialize(body));
      else
        if ("application/x-www-form-urlencoded".equals(contentType)) {
          StringBuilder formParamBuilder = new StringBuilder();

          // encode the form params
          for (String key : formParams.keySet()) {
            String value = formParams.get(key);
            if (value != null && !"".equals(value.trim())) {
              if (formParamBuilder.length() > 0) {
                formParamBuilder.append("&");
              }
              try {
                formParamBuilder
                    .append(URLEncoder.encode(key, "utf8"))
                    .append("=")
                    .append(URLEncoder
                        .encode(value, "utf8"));
              } catch (Exception e) {
                // move on to next
              }
            }
          }
          response = builder.type(contentType).post(
              ClientResponse.class, formParamBuilder.toString());
        } else {
          response = builder.type(contentType).post(ClientResponse.class,
            serialize(body));
        }
    } else if ("PUT".equals(method)) {
      if (body == null)
        response = builder.put(ClientResponse.class, serialize(body));
      else {
        if ("application/x-www-form-urlencoded".equals(contentType)) {
          StringBuilder formParamBuilder = new StringBuilder();

          // encode the form params
          for (String key : formParams.keySet()) {
            String value = formParams.get(key);
            if (value != null && !"".equals(value.trim())) {
              if (formParamBuilder.length() > 0) {
                formParamBuilder.append("&");
              }
              try {
                formParamBuilder
                    .append(URLEncoder.encode(key, "utf8"))
                    .append("=")
                    .append(URLEncoder
                        .encode(value, "utf8"));
              } catch (Exception e) {
                // move on to next
              }
            }
          }
          response = builder.type(contentType).put(
              ClientResponse.class, formParamBuilder.toString());
        } else
          response = builder.type(contentType).put(
              ClientResponse.class, serialize(body));
      }
    } else if ("DELETE".equals(method)) {
      if (body == null)
        response = builder
            .delete(ClientResponse.class, serialize(body));
      else
        response = builder.type(contentType).delete(
            ClientResponse.class, serialize(body));
    } else {
      throw new ApiException(500, "unknown method type " + method);
    }
    if (response.getClientResponseStatus() == ClientResponse.Status.NO_CONTENT) {
View Full Code Here

    WebResource webResource = client.resource(restEndpointUrl + String.format("/containers/%s/start", containerId));

    try {
      LOGGER.trace("POST: {}", webResource);
      Builder builder = webResource.accept(MediaType.TEXT_PLAIN);
      if (hostConfig != null) {
        builder.type(MediaType.APPLICATION_JSON).post(hostConfig);
      } else {
        builder.post((HostConfig) null);
      }
    } catch (UniformInterfaceException exception) {
      if (exception.getResponse().getStatus() == 404) {
        throw new NotFoundException(String.format("No such container %s", containerId));
      } else if (exception.getResponse().getStatus() == 204) {
View Full Code Here

TOP

Related Classes of com.sun.jersey.api.client.WebResource.Builder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.