/**
   * Tests conditional ranges requests.
   *
   * @throws Exception
   */
  public void testConditionalRanges() throws Exception {
    Client client = new Client(Protocol.HTTP);

    // Test partial Get.
    Request request = new Request(Method.GET, "http://localhost:" + TEST_PORT + "/testGet");
    Response response = client.handle(request);
    Tag entityTag = response.getEntity().getTag();

    request.setRanges(Arrays.asList(new Range(1, Range.SIZE_MAX)));
    request.getConditions().setRangeTag(entityTag);
    response = client.handle(request);
    assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
    assertEquals("234567890", response.getEntity().getText());
    assertEquals(10, response.getEntity().getSize());
    assertEquals(9, response.getEntity().getAvailableSize());
    assertEquals(1, response.getEntity().getRange().getIndex());
    assertEquals(9, response.getEntity().getRange().getSize());

    entityTag = new Tag(entityTag.getName() + "-test");
    request.setRanges(Arrays.asList(new Range(1, Range.SIZE_MAX)));
    request.getConditions().setRangeTag(entityTag);
    response = client.handle(request);
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("1234567890", response.getEntity().getText());
    client.stop();
  }
  public Response sendRequest(Request request) throws IOException {
    injectCustomSecurityMechanism(request);

    if (log.isLoggable(Level.FINE)) {
      RestClientLogHelper.logHttpRequest(log, Level.FINE, request);
    }

    Client client = initClient();
    Response response = client.handle(request);

    if (log.isLoggable(Level.FINE)) {
      RestClientLogHelper.logHttpResponse(log, Level.FINE, response);
    }

    if (response.getStatus().isSuccess()) {
      return response;
    } else if (response.getStatus().equals(Status.CLIENT_ERROR_UNAUTHORIZED)) {
      // retry request with additional authentication
      Request retryRequest = createChallengeResponse(response);
      return sendRequest(retryRequest);
    }

    throw new RepositoryException(
        "Encountered error while retrieving http response (HttpStatus: "
            + response.getStatus()
            + ", Body: "
            + response.getEntity().getText()
            + ")");
  }
  /**
   * Tests partial Get requests.
   *
   * @throws IOException
   * @throws NoSuchAlgorithmException
   */
  @Test
  public void testGet() throws IOException, NoSuchAlgorithmException {
    Client client = new Client(Protocol.HTTP);

    // Test partial Get.
    Request request = new Request(Method.PUT, "http://localhost:" + TEST_PORT + "/");
    StringRepresentation rep = new StringRepresentation("0123456789");
    try {
      DigesterRepresentation digester = new DigesterRepresentation(rep);
      // Such representation computes the digest while
      // consuming the wrapped representation.
      digester.exhaust();
      // Set the digest with the computed one
      digester.setDigest(digester.computeDigest());
      request.setEntity(digester);

      Response response = client.handle(request);

      assertEquals(Status.SUCCESS_OK, response.getStatus());
      digester = new DigesterRepresentation(response.getEntity());
      digester.exhaust();
      assertTrue(digester.checkDigest());

      client.stop();
    } catch (Exception e) {
      fail(e.getMessage());
    }
  }
  /**
   * @param args
   * @throws Exception
   */
  public static void main(String[] args) throws Exception {
    String propertyFilePath = args.length > 0 ? args[0] : "jeevlet.properties";

    // Get configuration
    int listenPort =
        Integer.parseInt(ConfigUtils.getProps(propertyFilePath).getProperty(Config.LISTEN_PORT));
    String hostDomain = ConfigUtils.getProps(propertyFilePath).getProperty(Config.HOST_DOMAIN);

    // Prepare the request
    String url = "http://" + hostDomain + ":" + listenPort + "/";
    Request request = new Request(Method.GET, url);
    request.setEntity(Config.STOP_GEONETWORK, MediaType.TEXT_PLAIN);

    // Handle it using an HTTP client connector
    Client client = new Client(Protocol.HTTP);

    try {
      client.start();
      Response response = client.handle(request);
    } catch (Exception e) {
      System.err.println("ERROR: Not running!");
      e.printStackTrace();
    } finally {
      client.stop();
      System.out.println("GeoNetwork shutdown ...");
      System.exit(0);
    }
  }
Esempio n. 5
0
 @Override
 public void handle(Request request, Response response) {
   // Update URI of the incoming request
   request.setResourceRef("http://localhost:8182/");
   // Create a basic HTTP client
   Client client = new Client(Protocol.HTTP);
   // Let the client handle the request and response
   client.handle(request, response);
 }
Esempio n. 6
0
 public void handle(Request request, Response response) {
   if (request.getProtocol().equals(Protocol.HTTP)) {
     List<Protocol> protocols = Arrays.asList(Protocol.HTTP);
     String helperClass = HttpClientHelper.class.getName();
     Client client = new Client(null, protocols, helperClass);
     client.handle(request, response);
   } else {
     new Client(request.getProtocol()).handle(request, response);
   }
 };
  /**
   * Tests ranges.
   *
   * @throws Exception
   */
  public void testRanges() throws Exception {
    Client client = new Client(Protocol.HTTP);
    Request request;
    Response response;

    // Test "range" header.
    request = new Request(Method.GET, "http://localhost:" + TEST_PORT + "/test?range=0-500");
    request.setRanges(Arrays.asList(new Range(0, 500)));
    response = client.handle(request);
    assertTrue(response.getStatus().isSuccess());
    response.getEntity().exhaust();

    request = new Request(Method.GET, "http://localhost:" + TEST_PORT + "/test?range=-500");
    request.setRanges(Arrays.asList(new Range(Range.INDEX_LAST, 500)));
    response = client.handle(request);
    assertTrue(response.getStatus().isSuccess());
    response.getEntity().exhaust();

    request = new Request(Method.GET, "http://localhost:" + TEST_PORT + "/test?range=500-");
    request.setRanges(Arrays.asList(new Range(500, Range.SIZE_MAX)));
    response = client.handle(request);
    assertTrue(response.getStatus().isSuccess());
    response.getEntity().exhaust();

    request = new Request(Method.GET, "http://localhost:" + TEST_PORT + "/test?range=500-1000");
    request.setRanges(Arrays.asList(new Range(500, 500)));

    response = client.handle(request);
    assertTrue(response.getStatus().isSuccess());
    response.getEntity().exhaust();

    // Multiple ranges are not supported yet.
    // request = new Request(Method.GET, "http://localhost:" + TEST_PORT
    // + "/test?range=500-1000&range=500-");
    // request.setRanges(Arrays.asList(new Range(500, 500), new Range(500,
    // Range.SIZE_MAX)));
    // assertTrue(client.handle(request).getStatus().isSuccess());

    // client.stop();

  }
  public void writeRawData(String path, byte[] data) throws IOException {
    while (path.startsWith("/")) {
      path = path.substring(1);
    }

    Request rr = createRequest(Method.PUT, path);

    ByteArrayRepresentation entity = new ByteArrayRepresentation(MediaType.APPLICATION_XML, data);

    rr.setEntity(entity);

    Response response = client.handle(rr);

    if (!response.getStatus().isSuccess()) {
      throw new IOException("The response was not successful: " + response.getStatus());
    }
  }
  @Test
  public void testPutTriple() throws Exception {
    final Client client = new Client(Protocol.HTTP);
    final Reference reference = new Reference("http://localhost/clue/triples");
    reference.setHostPort(port);

    final Request request = new Request(Method.PUT, reference);
    request.setChallengeResponse(getChallengeResponse());

    final Weapon weapon = WeaponEnum.CANDLESTICK.getWeapon();
    final Suspect suspect = SuspectEnum.PEACOCK.getSuspect();
    final Room room = RoomEnum.BILLIARDROOM.getRoom();

    request.setEntity(new JacksonRepresentation<Triple>(new Triple(room, suspect, weapon)));
    System.out.println(request.getEntityAsText());
    client.handle(request);

    assertEquals(323, getRemainingCount());
  }
  public byte[] readRawData(String path) throws IOException {
    while (path.startsWith("/")) {
      path = path.substring(1);
    }

    Request rr = createRequest(Method.GET, path);

    Response response = client.handle(rr);

    if (response.getStatus().isSuccess() && response.isEntityAvailable()) {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();

      response.getEntity().write(baos);

      return baos.toByteArray();
    } else if (response.getStatus().equals(Status.CLIENT_ERROR_NOT_FOUND)) {
      return null;
    } else {
      throw new IOException("The response was not successful: " + response.getStatus());
    }
  }
  /**
   * Tests partial Put requests.
   *
   * @throws Exception
   */
  public void testPut() throws Exception {
    if (!SystemUtils.isWindows()) {
      Request request;
      Response response;

      BioUtils.delete(testDir, true);
      Client client = new Client(new Context(), Protocol.HTTP);
      client.getContext().getParameters().add("tracing", "true");

      // PUT on a file that does not exist
      request = new Request(Method.PUT, "http://localhost:" + TEST_PORT + "/testPut/essai.txt");
      request.setEntity(new StringRepresentation("1234567890"));
      request.setRanges(Arrays.asList(new Range(0, 10)));
      response = client.handle(request);
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      response = client.handle(new Request(Method.GET, request.getResourceRef()));
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      assertEquals("1234567890", response.getEntity().getText());

      // Partial PUT on a file, the provided representation overflowed the
      // existing file
      request = new Request(Method.PUT, "http://localhost:" + TEST_PORT + "/testPut/essai.txt");
      request.setEntity(new StringRepresentation("0000000000"));
      request.setRanges(Arrays.asList(new Range(1, 10)));
      response = client.handle(request);
      System.out.println(response.getStatus() + " / " + response.getStatus().getThrowable());
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      response = client.handle(new Request(Method.GET, request.getResourceRef()));
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      assertEquals("10000000000", response.getEntity().getText());

      // Partial PUT on a file that does not exists, the provided range
      // does not start at the 0 index.
      request = new Request(Method.PUT, "http://localhost:" + TEST_PORT + "/testPut/essai2.txt");
      request.setEntity(new StringRepresentation("0000000000"));
      request.setRanges(Arrays.asList(new Range(1, 10)));
      response = client.handle(request);
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      request.setMethod(Method.GET);
      response = client.handle(request);
      assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
      assertEquals("0000000000", response.getEntity().getText());

      // Partial PUT on a file, simple range
      request = new Request(Method.PUT, "http://localhost:" + TEST_PORT + "/testPut/essai.txt");
      request.setEntity(new StringRepresentation("22"));
      request.setRanges(Arrays.asList(new Range(2, 2)));
      response = client.handle(request);
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      response = client.handle(new Request(Method.GET, request.getResourceRef()));
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      assertEquals("10220000000", response.getEntity().getText());

      // Partial PUT on a file, the provided representation will be padded
      // at the very end of the file.
      request = new Request(Method.PUT, "http://localhost:" + TEST_PORT + "/testPut/essai.txt");
      request.setEntity(new StringRepresentation("888"));
      request.setRanges(Arrays.asList(new Range(8, Range.SIZE_MAX)));
      response = client.handle(request);
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      response = client.handle(new Request(Method.GET, request.getResourceRef()));
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      assertEquals("10220000888", response.getEntity().getText());

      // Partial PUT on a file that does not exist, the range does not
      // specify the range size.
      request = new Request(Method.PUT, "http://localhost:" + TEST_PORT + "/testPut/essai3.txt");
      request.setEntity(new StringRepresentation("888"));
      request.setRanges(Arrays.asList(new Range(8, Range.SIZE_MAX)));
      response = client.handle(request);
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      request.setMethod(Method.GET);
      response = client.handle(request);
      assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
      assertEquals("888", response.getEntity().getText());

      // Partial PUT on a file, the provided representation will be padded
      // just before the end of the file.
      request = new Request(Method.PUT, "http://localhost:" + TEST_PORT + "/testPut/essai.txt");
      request.setEntity(new StringRepresentation("99"));
      request.setRanges(Arrays.asList(new Range(8, Range.SIZE_MAX)));
      response = client.handle(request);
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      response = client.handle(new Request(Method.GET, request.getResourceRef()));
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      assertEquals("10220000998", response.getEntity().getText());

      request = new Request(Method.GET, "http://localhost:" + TEST_PORT + "/testPut/essai.txt");
      request.setRanges(Arrays.asList(new Range(3, Range.SIZE_MAX)));
      response = client.handle(request);
      assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
      assertEquals("20000998", response.getEntity().getText());

      BioUtils.delete(testDir, true);
      client.stop();
    }
  }
  /**
   * Tests partial Get requests.
   *
   * @throws Exception
   */
  public void testGet() throws Exception {
    Client client = new Client(Protocol.HTTP);

    // Test partial Get.
    Request request = new Request(Method.GET, "http://localhost:" + TEST_PORT + "/testGet");
    Response response;

    response = client.handle(request);
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("1234567890", response.getEntity().getText());
    assertEquals(10, response.getEntity().getSize());
    assertEquals(10, response.getEntity().getAvailableSize());

    request = new Request(Method.GET, "http://localhost:" + TEST_PORT + "/testGet");
    request.setRanges(Arrays.asList(new Range(0, 10)));
    response = client.handle(request);
    assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
    assertEquals("1234567890", response.getEntity().getText());
    assertEquals(10, response.getEntity().getSize());
    assertEquals(10, response.getEntity().getAvailableSize());
    assertEquals(0, response.getEntity().getRange().getIndex());
    assertEquals(10, response.getEntity().getRange().getSize());

    request.setRanges(Arrays.asList(new Range(Range.INDEX_FIRST, 2)));
    response = client.handle(request);
    assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
    assertEquals("12", response.getEntity().getText());
    assertEquals(10, response.getEntity().getSize());
    assertEquals(2, response.getEntity().getAvailableSize());
    assertEquals(0, response.getEntity().getRange().getIndex());
    assertEquals(2, response.getEntity().getRange().getSize());

    request.setRanges(Arrays.asList(new Range(2, 2)));
    response = client.handle(request);
    assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
    assertEquals("34", response.getEntity().getText());
    assertEquals(10, response.getEntity().getSize());
    assertEquals(2, response.getEntity().getAvailableSize());
    assertEquals(2, response.getEntity().getRange().getIndex());
    assertEquals(2, response.getEntity().getRange().getSize());

    request.setRanges(Arrays.asList(new Range(2, 7)));
    response = client.handle(request);
    assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
    assertEquals("3456789", response.getEntity().getText());
    assertEquals(10, response.getEntity().getSize());
    assertEquals(7, response.getEntity().getAvailableSize());
    assertEquals(2, response.getEntity().getRange().getIndex());
    assertEquals(7, response.getEntity().getRange().getSize());

    request.setRanges(Arrays.asList(new Range(Range.INDEX_LAST, 7)));
    response = client.handle(request);
    assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
    assertEquals("4567890", response.getEntity().getText());
    assertEquals(10, response.getEntity().getSize());
    assertEquals(7, response.getEntity().getAvailableSize());
    assertEquals(3, response.getEntity().getRange().getIndex());
    assertEquals(7, response.getEntity().getRange().getSize());

    request.setRanges(Arrays.asList(new Range(2, Range.SIZE_MAX)));
    response = client.handle(request);
    assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
    assertEquals("34567890", response.getEntity().getText());
    assertEquals(10, response.getEntity().getSize());
    assertEquals(8, response.getEntity().getAvailableSize());
    assertEquals(2, response.getEntity().getRange().getIndex());
    assertEquals(8, response.getEntity().getRange().getSize());

    request.setRanges(Arrays.asList(new Range(2, 1000)));
    response = client.handle(request);
    assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
    assertEquals("34567890", response.getEntity().getText());
    assertEquals(10, response.getEntity().getSize());
    assertEquals(8, response.getEntity().getAvailableSize());
    assertEquals(2, response.getEntity().getRange().getIndex());
    assertEquals(8, response.getEntity().getRange().getSize());

    client.stop();
  }
Esempio n. 13
0
  /** Returns a full representation for a given variant. */
  @Override
  public Representation represent(Variant variant) throws ResourceException {

    String endPoint;

    if (version == 1) {
      jsonRequestString =
          "{getMedicationFacts:{criteria:{queryId:\"20100411011010\",\"senderId\":\"Adapter Assembly Service\",\"interactionId\":\"QUPC_IN043100UV\",\"triggerEventCode\":\"QUPC_TE043100UV0\",\"patientId\":\""
              + patientId
              + "\",\"careProvisionCode\":\"HISTMEDLIST\",\"careRecordStartTimePeriod\":\""
              + fromDate
              + "\",\"careRecordEndTimePeriod\":\""
              + toDate
              + "\"}}}";

      endPoint = this.getProperty("JSONPojoFactServiceEndpoint");

    } else {
      jsonRequestString =
          "{\"getMedicationFacts\":{\"criteria\":{\"queryId\":\"20100411011010\",\"senderId\":\"Adapter Assembly Service\",\"interactionId\":\"QUPC_IN043100UV\",\"triggerEventCode\":\"QUPC_TE043100UV01\","
              + "\"careRecordPayload\":{\"patientId\":\""
              + patientId
              + "\",\"careProvisionCode\":\"HISTMEDLIST\",\"careRecordStartTimePeriod\":\""
              + fromDate
              + "\",\"careRecordEndTimePeriod\":\""
              + toDate
              + "\"}}}}";

      endPoint = this.getProperty("JSONPojoFactServiceEndpointV2");
    }

    try {
      Client client = new Client(Protocol.HTTP);
      client.setConnectTimeout(10);

      Request request =
          new Request(
              Method.POST, new Reference(endPoint), new StringRepresentation(jsonRequestString));
      Response response = client.handle(request);

      if (response.getStatus().isSuccess()) {
        String rep = response.getEntity().getText();
        JSONObject jo = new JSONObject(rep);
        Object omf = jo.get("medicationFact");
        if (!(omf instanceof JSONArray)) {
          JSONArray joa = new JSONArray();
          joa.put(omf);
          jo.remove("medicationFact");
          jo.put("medicationFact", joa);
          rep = jo.toString();
        }
        Representation representation = new StringRepresentation(rep, MediaType.APPLICATION_JSON);

        return representation;
      } else {
        throw new ResourceException(
            Status.SERVER_ERROR_INTERNAL,
            "Error during call to JSONPojoFactServiceEndpoint, Status = "
                + response.getStatus().getDescription());
      }
    } catch (ResourceException e) {
      throw e;
    } catch (Exception e) {
      Logger.getLogger(AllergiesResource.class.getName()).log(Level.SEVERE, null, e);
      throw new ResourceException(
          Status.SERVER_ERROR_INTERNAL, "Error calling JSONPojoFactServiceEndpoint", e);
    }
  }