Java源码示例:org.apache.olingo.commons.api.http.HttpHeader

示例1
@Test
public void validODataVersionAndMaxVersionHeader1() throws Exception {
  URL url = new URL(SERVICE_URI + "ESAllPrim?$format=json");

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.setRequestProperty(HttpHeader.ODATA_VERSION, "4.0");
  connection.setRequestProperty(HttpHeader.ODATA_MAX_VERSION, "4.01");
  connection.connect();

  assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());
  assertEquals("4.0", connection.getHeaderField(HttpHeader.ODATA_VERSION));
  assertEquals("application/json; odata.metadata=minimal", 
      connection.getHeaderField(HttpHeader.CONTENT_TYPE));

  final String content = IOUtils.toString(connection.getInputStream());
  assertNotNull(content);
}
 
示例2
public ODataResponse handle(final ODataRequest request, final boolean isChangeSet)
    throws BatchDeserializerException {
  ODataResponse response;

  if (isChangeSet) {
    rewriter.replaceReference(request);

    response = oDataHandler.process(request);

    rewriter.addMapping(request, response);
  } else {
    response = oDataHandler.process(request);
  }

  // Add content id to response
  final String contentId = request.getHeader(HttpHeader.CONTENT_ID);
  if (contentId != null) {
    response.setHeader(HttpHeader.CONTENT_ID, contentId);
  }

  return response;
}
 
示例3
public DebugTabBody(final ODataResponse response) {
  this.response = response;
  if (response != null) {
    final String contentTypeString = response.getHeader(HttpHeader.CONTENT_TYPE);
    if (contentTypeString != null) {
      if (contentTypeString.startsWith("application/json")) {
        responseContent = ResponseContent.JSON;
      } else if (contentTypeString.startsWith("image/")) {
        responseContent = ResponseContent.IMAGE;
      } else if (contentTypeString.contains("xml")) {
        responseContent = ResponseContent.XML;
      } else {
        responseContent = ResponseContent.TEXT;
      }
    } else {
      responseContent = ResponseContent.TEXT;
    }
  } else {
    responseContent = ResponseContent.TEXT;
  }
}
 
示例4
private void retrieveMonitorDetails(final ODataBatchResponse res) {
  Collection<String> headers = res.getHeader(HttpHeader.LOCATION);
  if (headers == null || headers.isEmpty()) {
    throw new AsyncRequestException("Invalid async request response. Monitor URL not found");
  } else {
    this.location = createLocation(headers.iterator().next());
  }

  headers = res.getHeader(HttpHeader.RETRY_AFTER);
  if (headers != null && !headers.isEmpty()) {
    this.retryAfter = parseReplyAfter(headers.iterator().next());
  }

  headers = res.getHeader(HttpHeader.PREFERENCE_APPLIED);
  if (headers != null && !headers.isEmpty()) {
    for (String header : headers) {
      if (header.equalsIgnoreCase(new ODataPreferences().respondAsync())) {
        preferenceApplied = true;
      }
    }
  }

  IOUtils.closeQuietly(res.getRawResponse());
}
 
示例5
@Test
public void biggerResponse() throws Exception {
  ODataResponse response = new ODataResponse();
  response.setStatusCode(HttpStatusCode.ACCEPTED.getStatusCode());
  response.setHeader(HttpHeader.CONTENT_TYPE, ContentType.APPLICATION_JSON.toContentTypeString());
  response.setHeader(HttpHeader.CONTENT_LENGTH, String.valueOf(0));

  String testData = testData(20000);
  response.setContent(IOUtils.toInputStream(testData));

  AsyncResponseSerializer serializer = new AsyncResponseSerializer();
  InputStream in = serializer.serialize(response);
  String result = IOUtils.toString(in);
  assertEquals("HTTP/1.1 202 Accepted" + CRLF
      + "Content-Type: application/json" + CRLF
      + "Content-Length: 0" + CRLF + CRLF
      + testData, result);
}
 
示例6
@Test
public void contentTypeCaseInsensitive() throws Exception {
  final String batch = "--" + BOUNDARY + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + MULTIPART_MIXED + "; boundary=" + CHANGESET_BOUNDARY + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      + MIME_HEADERS
      + HttpHeader.CONTENT_ID + ": 1" + CRLF
      + HttpHeader.CONTENT_LENGTH + ": 200" + CRLF
      + CRLF
      + HttpMethod.PATCH + " ESAllPrim(32767)" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_JSON + CRLF
      + CRLF
      + "{\"PropertyString\":\"new\"}" + CRLF
      + "--" + CHANGESET_BOUNDARY + "--" + CRLF
      + CRLF
      + "--" + BOUNDARY + "--";

  parse(batch);
}
 
示例7
@Test
public void multipleValuesInAcceptHeaderWithIncorrectParam() throws Exception {
  URL url = new URL(SERVICE_URI + "ESAllPrim");

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.setRequestProperty(HttpHeader.ACCEPT, "application/json,"
      + "application/json;q=0.1,application/json;q=<1");
  connection.connect();

  assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), connection.getResponseCode());

  final String content = IOUtils.toString(connection.getErrorStream());
  assertTrue(content.contains("The content-type range 'application/json;q=<1' is not "
      + "supported as value of the Accept header."));
}
 
示例8
@Test
public void testBaseTypeDerivedTypeCasting1() throws Exception {
  URL url = new URL(SERVICE_URI + "ESTwoPrim(111)/olingo.odata.test1.ETBase");

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.setRequestProperty(HttpHeader.ACCEPT, "application/json;odata.metadata=full");
  connection.connect();

  assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());
  final String content = IOUtils.toString(connection.getInputStream());

  assertTrue(content.contains("\"PropertyInt16\":111"));
  assertTrue(content.contains("\"PropertyString\":\"TEST A\""));
  assertTrue(content.contains("\"AdditionalPropertyString_5\":\"TEST A 0815\""));
  assertTrue(content.contains("\"@odata.type\":\"#olingo.odata.test1.ETBase\""));
}
 
示例9
@Test
public void queryInOperatorWithFunction() throws Exception {
  URL url = new URL(SERVICE_URI + "ESAllPrim?$filter=PropertyString%20in%20olingo.odata.test1.UFCRTCollString()");
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.setRequestProperty(HttpHeader.ACCEPT, "application/json;odata.metadata=minimal");
  connection.connect();

  assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());
  assertEquals(ContentType.JSON, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE)));

  final String content = IOUtils.toString(connection.getInputStream());
  assertTrue(content.contains("\"value\":[{\"PropertyInt16\":10,"
      + "\"PropertyString\":\"[email protected]\","
      + "\"PropertyBoolean\":false,\"PropertyByte\":0,\"PropertySByte\":0,"
      + "\"PropertyInt32\":0,\"PropertyInt64\":0,\"PropertySingle\":0.0,"
      + "\"PropertyDouble\":0.0,\"PropertyDecimal\":0,\"PropertyBinary\":\"\","
      + "\"PropertyDate\":\"1970-01-01\","
      + "\"PropertyDateTimeOffset\":\"2005-12-03T00:00:00Z\","
      + "\"PropertyDuration\":\"PT0S\","
      + "\"PropertyGuid\":\"76543201-23ab-cdef-0123-456789cccddd\","
      + "\"PropertyTimeOfDay\":\"00:01:01\"}]"));
}
 
示例10
public Map<String, String> getPreferences(){
  HashMap<String, String> map = new HashMap<>();
  List<String> headers = request.getHeaders(HttpHeader.PREFER);
  if (headers != null) {
    for (String header:headers) {
      int idx = header.indexOf('=');
      if (idx != -1) {
        String key = header.substring(0, idx);
        String value = header.substring(idx+1);
        if (value.startsWith("\"")) {
          value = value.substring(1);
        }
        if (value.endsWith("\"")) {
          value = value.substring(0, value.length()-1);
        }
        map.put(key, value);
      } else {
        map.put(header, "true");
      }
    }
  }
  return map;
}
 
示例11
@Override
public void readMediaEntity(final ODataRequest request, final ODataResponse response, final UriInfo uriInfo,
    final ContentType responseFormat) throws ODataApplicationException, ODataLibraryException {
  getEdmEntitySet(uriInfo); // including checks
  final Entity entity = readEntity(uriInfo);
  final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource());
  if (isMediaStreaming(edmEntitySet)) {
EntityMediaObject mediaEntity = new EntityMediaObject();
mediaEntity.setBytes(dataProvider.readMedia(entity));
   response.setODataContent(odata.createFixedFormatSerializer()
   		.mediaEntityStreamed(mediaEntity).getODataContent());
  } else {
  	response.setContent(odata.createFixedFormatSerializer().binary(dataProvider.readMedia(entity)));
  }
  response.setContent(odata.createFixedFormatSerializer().binary(dataProvider.readMedia(entity)));
  response.setStatusCode(HttpStatusCode.OK.getStatusCode());
  response.setHeader(HttpHeader.CONTENT_TYPE, entity.getMediaContentType());
  if (entity.getMediaETag() != null) {
    response.setHeader(HttpHeader.ETAG, entity.getMediaETag());
  }
}
 
示例12
@Override
public void createMediaEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
    ContentType requestFormat, ContentType responseFormat) throws ODataApplicationException, ODataLibraryException {
  
  final EdmEntitySet edmEntitySet = Util.getEdmEntitySet(uriInfo);
  final byte[] mediaContent = odata.createFixedFormatDeserializer().binary(request.getBody());
  
  final Entity entity = storage.createMediaEntity(edmEntitySet.getEntityType(), 
                                                  requestFormat.toContentTypeString(), 
                                                  mediaContent);
  
  final ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build();
  final EntitySerializerOptions opts = EntitySerializerOptions.with().contextURL(contextUrl).build();
  final SerializerResult serializerResult = odata.createSerializer(responseFormat).entity(serviceMetadata,
      edmEntitySet.getEntityType(), entity, opts);
  
  final String location = request.getRawBaseUri() + '/'
      + odata.createUriHelper().buildCanonicalURL(edmEntitySet, entity);
  response.setContent(serializerResult.getContent());
  response.setStatusCode(HttpStatusCode.CREATED.getStatusCode());
  response.setHeader(HttpHeader.LOCATION, location);
  response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
示例13
@Test
public void querySimple() throws Exception {
  URL url = new URL(SERVICE_URI + "ESAllPrim?$filter=PropertyString%20in%20("
      + "%27Second%20Resource%20-%20negative%20values%27,%27xyz%27)");
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.setRequestProperty(HttpHeader.ACCEPT, "application/json;odata.metadata=minimal");
  connection.connect();

  assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());
  assertEquals(ContentType.JSON, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE)));

  final String content = IOUtils.toString(connection.getInputStream());
  assertTrue(content.contains("\"value\":[{\"PropertyInt16\":-32768,"
      + "\"PropertyString\":\"Second Resource - negative values\","
      + "\"PropertyBoolean\":false,\"PropertyByte\":0,\"PropertySByte\":-128,"
      + "\"PropertyInt32\":-2147483648,\"PropertyInt64\":-9223372036854775808,"
      + "\"PropertySingle\":-1.79E8,\"PropertyDouble\":-179000.0,\"PropertyDecimal\":-34,"
      + "\"PropertyBinary\":\"ASNFZ4mrze8=\",\"PropertyDate\":\"2015-11-05\","
      + "\"PropertyDateTimeOffset\":\"2005-12-03T07:17:08Z\",\"PropertyDuration\":\"PT9S\","
      + "\"PropertyGuid\":\"76543201-23ab-cdef-0123-456789dddfff\","
      + "\"PropertyTimeOfDay\":\"23:49:14\"}]"));
}
 
示例14
@Test
public void multipleValues() {
  Header header = new Header(1);
  header.addHeader(HttpHeader.CONTENT_TYPE, ContentType.MULTIPART_MIXED.toContentTypeString(), 1);
  header.addHeader(HttpHeader.CONTENT_TYPE, ContentType.APPLICATION_ATOM_SVC.toContentTypeString(), 2);
  header.addHeader(HttpHeader.CONTENT_TYPE, ContentType.APPLICATION_ATOM_XML.toContentTypeString(), 3);

  final String fullHeaderString =
      ContentType.MULTIPART_MIXED + ", " + ContentType.APPLICATION_ATOM_SVC + ", "
          + ContentType.APPLICATION_ATOM_XML;

  assertEquals(fullHeaderString, header.getHeader(HttpHeader.CONTENT_TYPE));
  assertEquals(3, header.getHeaders(HttpHeader.CONTENT_TYPE).size());
  assertEquals(ContentType.MULTIPART_MIXED.toContentTypeString(),
      header.getHeaders(HttpHeader.CONTENT_TYPE).get(0));
  assertEquals(ContentType.APPLICATION_ATOM_SVC.toContentTypeString(),
      header.getHeaders(HttpHeader.CONTENT_TYPE).get(1));
  assertEquals(ContentType.APPLICATION_ATOM_XML.toContentTypeString(),
      header.getHeaders(HttpHeader.CONTENT_TYPE).get(2));
}
 
示例15
@Test
public void boundFunctionReturningDerievedType() throws Exception {
  URL url = new URL(SERVICE_URI + "ESBase(111)/olingo.odata.test1.ETTwoBase/"
      + "olingo.odata.test1.BFESBaseRTESTwoBase()");

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.setRequestProperty(HttpHeader.ACCEPT, "application/json");
  connection.connect();

  assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());
  final String expected =  "\"PropertyInt16\":111,"
      +  "\"PropertyString\":\"TEST A\","
      +  "\"AdditionalPropertyString_5\":\"TEST A 0815\","
      +  "\"AdditionalPropertyString_6\":\"TEST B 0815\"";
  String content = IOUtils.toString(connection.getInputStream());
  assertTrue(content.contains(expected));
  connection.disconnect();
}
 
示例16
public static int getContentLength(final Header headers) throws BatchDeserializerException {
  final HeaderField contentLengthField = headers.getHeaderField(HttpHeader.CONTENT_LENGTH);

  if (contentLengthField != null && contentLengthField.getValues().size() == 1) {
    try {
      final int contentLength = Integer.parseInt(contentLengthField.getValues().get(0));

      if (contentLength < 0) {
        throw new BatchDeserializerException("Invalid content length", MessageKeys.INVALID_CONTENT_LENGTH,
            Integer.toString(contentLengthField.getLineNumber()));
      }

      return contentLength;
    } catch (NumberFormatException e) {
      throw new BatchDeserializerException("Invalid content length", e, MessageKeys.INVALID_CONTENT_LENGTH,
          Integer.toString(contentLengthField.getLineNumber()));
    }
  }

  return -1;
}
 
示例17
@Test
public void nonNumericContentLength() throws Exception {
  final String batch = "--" + BOUNDARY + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + MULTIPART_MIXED + "; boundary=" + CHANGESET_BOUNDARY + CRLF
      + CRLF
      + "--" + CHANGESET_BOUNDARY + CRLF
      + MIME_HEADERS
      + HttpHeader.CONTENT_ID + ": 1" + CRLF
      + CRLF
      + HttpMethod.PATCH + " ESAllPrim(32767)" + HTTP_VERSION + CRLF
      + HttpHeader.CONTENT_TYPE + ": " + APPLICATION_JSON + CRLF
      + HttpHeader.CONTENT_LENGTH + ": 10abc" + CRLF
      + CRLF
      + "{\"PropertyString\":\"new\"}" + CRLF
      + "--" + CHANGESET_BOUNDARY + "--" + CRLF
      + CRLF
      + "--" + BOUNDARY + "--";

  parseInvalidBatchBody(batch, BatchDeserializerException.MessageKeys.INVALID_CONTENT_LENGTH);
}
 
示例18
@Test
public void noRequestContentType() throws Exception {
  EntityProcessor processor = mock(EntityProcessor.class);
  final ODataResponse response = dispatch(HttpMethod.POST, "ESAllPrim", null,
      HttpHeader.CONTENT_TYPE, null, processor);
  assertEquals(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode());
}
 
示例19
@Test
public void dataIsolation() throws Exception {
  String url = baseURL + "/People";
  HttpRequest request = new HttpGet(url);
  request.setHeader(HttpHeader.ODATA_ISOLATION, "snapshot");
  HttpResponse response = httpSend(request, 412);
  EntityUtils.consumeQuietly(response.getEntity());
}
 
示例20
@Override
public void readEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
    throws ODataApplicationException, SerializerException {

    // 1. retrieve the Entity Type
    List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
    // Note: only in our example we can assume that the first segment is the
    // EntitySet
    UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
    EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

    // 2. retrieve the data from backend
    List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
    Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);
    LOG.info("Reading entity {}", entity.getSelfLink());

    // 3. serialize
    EdmEntityType entityType = edmEntitySet.getEntityType();

    ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).suffix(ContextURL.Suffix.ENTITY).build();
    // expand and select currently not supported
    EntitySerializerOptions options = EntitySerializerOptions.with().contextURL(contextUrl).build();

    ODataSerializer serializer = this.odata.createSerializer(responseFormat);
    SerializerResult serializerResult = serializer.entity(serviceMetadata, entityType, entity, options);
    InputStream entityStream = serializerResult.getContent();

    // 4. configure the response object
    response.setContent(entityStream);
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
    response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
示例21
@Test
public void uriParserExceptionWithFormatQueryAtom() throws Exception {
  final ODataResponse response = dispatch(HttpMethod.GET, "ESAllPrims", "$format=atom", "", "", null);
  assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode());
  assertEquals("application/json;odata.metadata=minimal",
      response.getHeader(HttpHeader.CONTENT_TYPE));
}
 
示例22
@Test
public void invalidODataVersionHeader2() throws Exception {
  URL url = new URL(SERVICE_URI + "ESAllPrim");

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.setRequestProperty(HttpHeader.ODATA_VERSION, "5.0");
  connection.connect();

  assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), connection.getResponseCode());
  assertEquals("4.0", connection.getHeaderField(HttpHeader.ODATA_VERSION));

  final String content = IOUtils.toString(connection.getErrorStream());
  assertTrue(content.contains("OData version '5.0' is not supported."));
}
 
示例23
public void readEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
						throws ODataApplicationException, SerializerException {

	// 1. retrieve the Entity Type
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	// 2. retrieve the data from backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);

	// 3. serialize
	EdmEntityType entityType = edmEntitySet.getEntityType();

	ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).suffix(ContextURL.Suffix.ENTITY).build();
 	// expand and select currently not supported
	EntitySerializerOptions options = EntitySerializerOptions.with().contextURL(contextUrl).build();

	ODataSerializer serializer = this.odata.createSerializer(responseFormat);
	SerializerResult serializerResult = serializer.entity(serviceMetadata, entityType, entity, options);
	InputStream entityStream = serializerResult.getContent();

	//4. configure the response object
	response.setContent(entityStream);
	response.setStatusCode(HttpStatusCode.OK.getStatusCode());
	response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
示例24
@Test
public void updateReferenceAbsoluteURI() throws Exception {
  final URI uri = getClient().newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_KEY_NAV)
                                                   .appendKeySegment(1)
                                                   .appendNavigationSegment(NAV_PROPERTY_ET_KEY_NAV_ONE)
                                                   .appendRefSegment()
                                                   .build();
  
  final URI reference = getClient().newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_KEY_NAV)
                                                         .appendKeySegment(3)
                                                         .build();
  
  final ODataReferenceAddingResponse response = getClient().getCUDRequestFactory()
                                             .getReferenceSingleChangeRequest(new URI(SERVICE_URI), uri, reference)
                                             .execute();
  assertEquals(HttpStatusCode.NO_CONTENT.getStatusCode(), response.getStatusCode());
  final String cookie = response.getHeader(HttpHeader.SET_COOKIE).iterator().next();

  final URI getURI = getClient().newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_KEY_NAV)
                                                      .appendKeySegment(1)
                                                      .expand(NAV_PROPERTY_ET_KEY_NAV_ONE)
                                                      .build();
  final ODataEntityRequest<ClientEntity> requestGet = getEdmEnabledClient().getRetrieveRequestFactory()
      .getEntityRequest(getURI);
  requestGet.addCustomHeader(HttpHeader.COOKIE, cookie);
  final ODataRetrieveResponse<ClientEntity> responseGet = requestGet.execute();
  assertEquals(HttpStatusCode.OK.getStatusCode(), responseGet.getStatusCode());
  
  assertShortOrInt(3, responseGet.getBody().getNavigationLink(NAV_PROPERTY_ET_KEY_NAV_ONE)
                                       .asInlineEntity()
                                       .getEntity()
                                       .getProperty(PROPERTY_INT16)
                                       .getPrimitiveValue()
                                       .asPrimitive().toValue());
}
 
示例25
@Test
public void uriParserExceptionWithFormatJsonAcceptAtom() throws Exception {
  final ODataResponse response = dispatch(HttpMethod.GET, "ESAllPrims", "$format=json",
      HttpHeader.ACCEPT, ContentType.APPLICATION_ATOM_XML.toContentTypeString(), null);
  assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode());
  assertEquals("application/json;odata.metadata=minimal",
      response.getHeader(HttpHeader.CONTENT_TYPE));
}
 
示例26
@Test
public void invalidAcceptCharsetHeader1() throws Exception {
  URL url = new URL(SERVICE_URI + "ESAllPrim");

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.setRequestProperty(HttpHeader.ACCEPT_CHARSET, "US-ASCII");
  connection.connect();

  assertEquals(HttpStatusCode.NOT_ACCEPTABLE.getStatusCode(), connection.getResponseCode());
  
  final String content = IOUtils.toString(connection.getErrorStream());
  assertTrue(content.contains("The charset specified in Accept charset header "
      + "'US-ASCII' is not supported."));
}
 
示例27
@Test
public void testFilterWithFormEncodingOrderChange() throws Exception {
  URL url = new URL(SERVICE_URI + 
      "ESAllPrim?odata-accept-forms-encoding=true&$filter=PropertyInt16+eq+1");

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.GET.name());
  connection.setRequestProperty(HttpHeader.ACCEPT, "application/json;odata.metadata=full");
  connection.connect();

  assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());
  final String content = IOUtils.toString(connection.getInputStream());

  assertNotNull(content);
}
 
示例28
@Test
public void getBatchRequestWithRelativeUris() {
  ODataBatchRequest request = getClient().getBatchRequestFactory().getBatchRequest(SERVICE_URI);
  setCookieHeader(request);
  BatchManager payload = request.payloadManager();
  payload.addRequest(createGetRequest("ESAllPrim", 32767, true));

  // Fetch result
  final ODataBatchResponse response = payload.getResponse();
  saveCookieHeader(response);

  assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());
  assertEquals("OK", response.getStatusMessage());

  final Iterator<ODataBatchResponseItem> iter = response.getBody();
  assertTrue(iter.hasNext());

  ODataBatchResponseItem item = iter.next();
  assertFalse(item.isChangeset());

  ODataResponse oDataResponse = item.next();
  assertNotNull(oDataResponse);
  assertEquals(HttpStatusCode.OK.getStatusCode(), oDataResponse.getStatusCode());
  assertEquals(1, oDataResponse.getHeader(HttpHeader.ODATA_VERSION).size());
  assertEquals("4.0", oDataResponse.getHeader(HttpHeader.ODATA_VERSION).iterator().next());
  assertEquals(1, oDataResponse.getHeader(HttpHeader.CONTENT_LENGTH).size());
  assertEquals(isJson() ? "605" : "2223", oDataResponse.getHeader(HttpHeader.CONTENT_LENGTH).iterator().next());
  assertContentType(oDataResponse.getContentType());
}
 
示例29
@Test
public void testHeadMethodOnMetadataDocument() throws Exception {
  URL url = new URL(SERVICE_URI + "$metadata");

  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setRequestMethod(HttpMethod.HEAD.name());
  connection.setRequestProperty(HttpHeader.ACCEPT, "application/xml");
  connection.connect();

  assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());
  assertNull(connection.getHeaderField(HttpHeader.CONTENT_TYPE));
  assertEquals("", IOUtils.toString(connection.getInputStream()));
  connection.disconnect();
}
 
示例30
private void retrieveMonitorDetails(final HttpResponse res) {
  Header[] headers = res.getHeaders(HttpHeader.LOCATION);
  if (ArrayUtils.isNotEmpty(headers)) {
    this.location = createLocation(headers[0].getValue());
  } else {
    throw new AsyncRequestException(
        "Invalid async request response. Monitor URL '" + headers[0].getValue() + "'");
  }

  headers = res.getHeaders(HttpHeader.RETRY_AFTER);
  if (ArrayUtils.isNotEmpty(headers)) {
    this.retryAfter = parseReplyAfter(headers[0].getValue());
  }

  headers = res.getHeaders(HttpHeader.PREFERENCE_APPLIED);
  if (ArrayUtils.isNotEmpty(headers)) {
    for (Header header : headers) {
      if (header.getValue().equalsIgnoreCase(new ODataPreferences().respondAsync())) {
        preferenceApplied = true;
      }
    }
  }
  try {
    EntityUtils.consume(res.getEntity());
  } catch (IOException ex) {
    Logger.getLogger(AsyncRequestWrapperImpl.class.getName()).log(Level.SEVERE, null, ex);
  }
}