Package com.google.appengine.api.urlfetch

Examples of com.google.appengine.api.urlfetch.HTTPHeader


        // add the headers to the request
        if (options.containsKey("headers")) {
            @SuppressWarnings("unchecked") Map<String, String> headers = (Map<String, String>) options.get("headers");
            for (Entry<String, String> e : headers.entrySet()) {
                request.addHeader(new HTTPHeader(e.getKey(), e.getValue()));
            }
        }

        // add the payload
        if (options.containsKey("payload")) {
View Full Code Here


    protected void writeRequestHeaders(GHttpEndpoint endpoint, Exchange exchange, HTTPRequest request) {
        HeaderFilterStrategy strategy = endpoint.getHeaderFilterStrategy();
        for (String headerName : exchange.getIn().getHeaders().keySet()) {
            String headerValue = exchange.getIn().getHeader(headerName, String.class);
            if (strategy != null && !strategy.applyFilterToCamelHeaders(headerName, headerValue, exchange)) {
                request.addHeader(new HTTPHeader(headerName, headerValue));
            }
        }
    }
View Full Code Here

  java.net.URL url = new java.net.URL(strUrl);

  HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST, FetchOptions.Builder.withDeadline(TIMEOUT_SECONDS).allowTruncate());

  String boundary = makeBoundary();
  request.setHeader(new HTTPHeader("Content-Type", "multipart/form-data; boundary=" + boundary));
  ByteArrayOutputStream baos = new ByteArrayOutputStream();

  // form-data
  for (Map.Entry<String, String> formData : params.entrySet()) {
    write(baos, "--" + boundary + "\r\n");
View Full Code Here

public class HttpRequestTest extends URLFetchTestBase {

    @Test
    public void testGetters() throws Exception {
        HTTPRequest request = new HTTPRequest(getFetchUrl(), HTTPMethod.PATCH, FetchOptions.Builder.withDefaults());
        request.addHeader(new HTTPHeader("foo", "bar"));
        request.setPayload("qwerty".getBytes());

        Assert.assertEquals(getFetchUrl(), request.getURL());
        Assert.assertEquals(HTTPMethod.PATCH, request.getMethod());
        Assert.assertNotNull(request.getFetchOptions());
        Assert.assertNotNull(request.getHeaders());
        Assert.assertEquals(1, request.getHeaders().size());
        assertEquals(new HTTPHeader("foo", "bar"), request.getHeaders().get(0));
        Assert.assertArrayEquals("qwerty".getBytes(), request.getPayload());
    }
View Full Code Here

        URLFetchService service = URLFetchServiceFactory.getURLFetchService();

        URL url = getFetchUrl();

        HTTPRequest req = new HTTPRequest(url, HTTPMethod.POST);
        req.setHeader(new HTTPHeader("Content-Type", "application/octet-stream"));
        req.setPayload("Tralala".getBytes(UTF_8));

        HTTPResponse response = service.fetch(req);
        String content = new String(response.getContent());
        Assert.assertEquals("Hopsasa", content);
View Full Code Here

        URLFetchService service = URLFetchServiceFactory.getURLFetchService();

        URL url = getFetchUrl();

        HTTPRequest req = new HTTPRequest(url, HTTPMethod.POST);
        req.setHeader(new HTTPHeader("Content-Type", "application/octet-stream"));
        req.setPayload("Headers!".getBytes(UTF_8));

        HTTPResponse response = service.fetch(req);

        boolean found = false;
View Full Code Here

   @Test
   void testConvertWithHeaders() throws IOException {
      HTTPResponse gaeResponse = createMock(HTTPResponse.class);
      expect(gaeResponse.getResponseCode()).andReturn(200);
      List<HTTPHeader> headers = Lists.newArrayList();
      headers.add(new HTTPHeader(HttpHeaders.CONTENT_TYPE, "text/xml"));
      expect(gaeResponse.getHeaders()).andReturn(headers);
      expect(gaeResponse.getContent()).andReturn(null).atLeastOnce();
      replay(gaeResponse);
      HttpResponse response = req.apply(gaeResponse);
      assertEquals(response.getStatusCode(), 200);
View Full Code Here

   @Test
   void testConvertWithContent() throws IOException {
      HTTPResponse gaeResponse = createMock(HTTPResponse.class);
      expect(gaeResponse.getResponseCode()).andReturn(200);
      List<HTTPHeader> headers = Lists.newArrayList();
      headers.add(new HTTPHeader(HttpHeaders.CONTENT_TYPE, "text/xml"));
      expect(gaeResponse.getHeaders()).andReturn(headers);
      expect(gaeResponse.getContent()).andReturn("hello".getBytes()).atLeastOnce();
      replay(gaeResponse);
      HttpResponse response = req.apply(gaeResponse);
      assertEquals(response.getStatusCode(), 200);
View Full Code Here

      HTTPRequest gaeRequest = new HTTPRequest(url, HTTPMethod.valueOf(request.getMethod().toString()), options);

      for (Entry<String, String> entry : request.getHeaders().entries()) {
         String header = entry.getKey();
         if (!prohibitedHeaders.contains(header))
            gaeRequest.addHeader(new HTTPHeader(header, entry.getValue()));
      }
      gaeRequest.addHeader(new HTTPHeader(HttpHeaders.USER_AGENT, USER_AGENT));
      /**
       * byte [] content is replayable and the only content type supportable by GAE. As such, we
       * convert the original request content to a byte array.
       */
      if (request.getPayload() != null) {
         InputStream input = request.getPayload().getInput();
         try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            request.getPayload().writeTo(out);
            byte[] array = out.toByteArray();
            if (!request.getPayload().isRepeatable()) {
               Payload oldPayload = request.getPayload();
               request.setPayload(array);
               HttpUtils.copy(oldPayload.getContentMetadata(), request.getPayload().getContentMetadata());
            }
            gaeRequest.setPayload(array);
            if (array.length > 0) {
               gaeRequest.setHeader(new HTTPHeader("Expect", "100-continue"));
            }
         } catch (IOException e) {
            Throwables.propagate(e);
         } finally {
            Closeables.closeQuietly(input);
         }

         for (Entry<String, String> header : contentMetadataCodec.toHeaders(
               request.getPayload().getContentMetadata()).entries()) {
            if (!prohibitedHeaders.contains(header.getKey()))
               gaeRequest.setHeader(new HTTPHeader(header.getKey(), header.getValue()));
         }
      }
      return gaeRequest;
   }
View Full Code Here

      HTTPRequest gaeRequest = new HTTPRequest(url, HTTPMethod.valueOf(request.getMethod().toString()), options);

      for (Entry<String, String> entry : request.getHeaders().entries()) {
         String header = entry.getKey();
         if (!prohibitedHeaders.contains(header))
            gaeRequest.addHeader(new HTTPHeader(header, entry.getValue()));
      }
      gaeRequest.addHeader(new HTTPHeader(HttpHeaders.USER_AGENT, USER_AGENT));
      /**
       * byte [] content is replayable and the only content type supportable by GAE. As such, we
       * convert the original request content to a byte array.
       */
      if (request.getPayload() != null) {
         InputStream input = request.getPayload().getInput();
         try {
            byte[] array = ByteStreams.toByteArray(input);
            if (!request.getPayload().isRepeatable()) {
               Payload oldPayload = request.getPayload();
               request.setPayload(array);
               HttpUtils.copy(oldPayload.getContentMetadata(), request.getPayload().getContentMetadata());
            }
            gaeRequest.setPayload(array);
            if (array.length > 0) {
               gaeRequest.setHeader(new HTTPHeader("Expect", "100-continue"));
            }
         } catch (IOException e) {
            Throwables.propagate(e);
         } finally {
            Closeables2.closeQuietly(input);
         }

         for (Entry<String, String> header : contentMetadataCodec.toHeaders(
               request.getPayload().getContentMetadata()).entries()) {
            if (!prohibitedHeaders.contains(header.getKey()))
               gaeRequest.setHeader(new HTTPHeader(header.getKey(), header.getValue()));
         }
      }
      return gaeRequest;
   }
View Full Code Here

TOP

Related Classes of com.google.appengine.api.urlfetch.HTTPHeader

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.