本文整理汇总了Java中org.apache.olingo.odata2.api.commons.HttpStatusCodes类的典型用法代码示例。如果您正苦于以下问题:Java HttpStatusCodes类的具体用法?Java HttpStatusCodes怎么用?Java HttpStatusCodes使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HttpStatusCodes类属于org.apache.olingo.odata2.api.commons包,在下文中一共展示了HttpStatusCodes类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeEntity
import org.apache.olingo.odata2.api.commons.HttpStatusCodes; //导入依赖的package包/类
private void writeEntity(String absoluteUri, String content, String contentType, String httpMethod)
throws IOException, URISyntaxException {
print(httpMethod + " request on uri: " + absoluteUri + ":\n " + content + "\n");
//
HttpURLConnection connection = initializeConnection(absoluteUri, contentType, httpMethod);
byte[] buffer = content.getBytes("UTF-8");
connection.getOutputStream().write(buffer);
// if a entity is created (via POST request) the response body contains the new created entity
HttpStatusCodes statusCode = HttpStatusCodes.fromStatusCode(connection.getResponseCode());
if(statusCode == HttpStatusCodes.CREATED) {
// get the content as InputStream and de-serialize it into an ODataEntry object
InputStream responseContent = connection.getInputStream();
logRawContent(httpMethod + " response:\n ", responseContent, "\n");
} else if(statusCode == HttpStatusCodes.NO_CONTENT) {
print("No content.");
} else {
checkStatus(connection);
}
//
connection.disconnect();
}
示例2: validateFieldsBeforeSave
import org.apache.olingo.odata2.api.commons.HttpStatusCodes; //导入依赖的package包/类
private void validateFieldsBeforeSave() throws ODataRuntimeApplicationException {
// reject null or empty Name field
if (supplierName == null || supplierName.length() == 0) {
throw new ODataRuntimeApplicationException(
"Invalid value for supplier name field",
Locale.ROOT,
HttpStatusCodes.BAD_REQUEST
);
}
// reject null or empty phone number field
if (phoneNumber == null || phoneNumber.length() == 0) {
throw new ODataRuntimeApplicationException(
"Invalid value for phone number field",
Locale.ROOT,
HttpStatusCodes.BAD_REQUEST
);
}
}
示例3: executeChangeSet
import org.apache.olingo.odata2.api.commons.HttpStatusCodes; //导入依赖的package包/类
@Override
public BatchResponsePart executeChangeSet(final BatchHandler handler, final List<ODataRequest> requests)
throws ODataException {
List<ODataResponse> responses = new ArrayList<>();
for (ODataRequest request : requests) {
ODataResponse response = handler.handleRequest(request);
if (response.getStatus().getStatusCode() >= HttpStatusCodes.BAD_REQUEST.getStatusCode()) {
// Rollback
List<ODataResponse> errorResponses = new ArrayList<>(1);
errorResponses.add(response);
return BatchResponsePart.responses(errorResponses).changeSet(false).build();
}
responses.add(response);
}
return BatchResponsePart.responses(responses).changeSet(true).build();
}
示例4: entitySetRooms
import org.apache.olingo.odata2.api.commons.HttpStatusCodes; //导入依赖的package包/类
@Test
@SuppressWarnings(value = "unchecked")
public void entitySetRooms() throws Exception {
HttpResponse response =
callUri("Rooms", HttpHeaders.ACCEPT, HttpContentType.APPLICATION_JSON, HttpStatusCodes.OK);
checkMediaType(response, HttpContentType.APPLICATION_JSON);
String body = getBody(response);
LinkedTreeMap<?, ?> map = getLinkedTreeMap(body);
List<LinkedTreeMap<String, String>> results = (List) map.get("results");
assertFalse(results.isEmpty());
LinkedTreeMap<String, String> firstRoom = null;
for (LinkedTreeMap<String, String> result: results) {
if(result.get("Name").equals("Small green room")) {
firstRoom = result;
}
}
assertNotNull(firstRoom);
assertEquals("Small green room", firstRoom.get("Name"));
assertEquals(20.0, firstRoom.get("Seats"));
assertEquals(42.0, firstRoom.get("Version"));
}
示例5: entitySetRoomsSkip
import org.apache.olingo.odata2.api.commons.HttpStatusCodes; //导入依赖的package包/类
@Test
@SuppressWarnings(value = "unchecked")
public void entitySetRoomsSkip() throws Exception {
HttpResponse response =
callUri("Rooms?$skip=1", HttpHeaders.ACCEPT, HttpContentType.APPLICATION_JSON, HttpStatusCodes.OK);
checkMediaType(response, HttpContentType.APPLICATION_JSON);
String body = getBody(response);
LinkedTreeMap<?, ?> map = getLinkedTreeMap(body);
List<LinkedTreeMap<String, String>> results = (List) map.get("results");
assertFalse(results.isEmpty());
//
assertTrue(roomsCount() != results.size());
LinkedTreeMap<String, String> roomToCheck = null;
for (LinkedTreeMap<String, String> result: results) {
if(result.get("Name").equals("Small dark green room")) {
roomToCheck = result;
}
}
assertNotNull(roomToCheck);
assertEquals("Small dark green room", roomToCheck.get("Name"));
assertEquals(30.0, roomToCheck.get("Seats"));
assertEquals(2.0, roomToCheck.get("Version"));
}
示例6: executeValidated
import org.apache.olingo.odata2.api.commons.HttpStatusCodes; //导入依赖的package包/类
public HttpResponse executeValidated(HttpStatusCodes expectedStatusCode) throws IOException {
HttpResponse response = this.execute();
assertNotNull(response);
assertEquals(expectedStatusCode.getStatusCode(), response.getStatusLine().getStatusCode());
if (expectedStatusCode == HttpStatusCodes.OK) {
assertNotNull(response.getEntity());
assertNotNull(response.getEntity().getContent());
} else if (expectedStatusCode == HttpStatusCodes.CREATED) {
assertNotNull(response.getEntity());
assertNotNull(response.getEntity().getContent());
assertNotNull(response.getFirstHeader(HttpHeaders.LOCATION));
} else if (expectedStatusCode == HttpStatusCodes.NO_CONTENT) {
assertTrue(response.getEntity() == null || response.getEntity().getContent() == null);
}
return response;
}
示例7: requestRoomId
import org.apache.olingo.odata2.api.commons.HttpStatusCodes; //导入依赖的package包/类
@SuppressWarnings(value = "unchecked")
private String requestRoomId() throws Exception {
HttpResponse response =
callUri("Rooms", HttpHeaders.ACCEPT, HttpContentType.APPLICATION_JSON, HttpStatusCodes.OK);
checkMediaType(response, HttpContentType.APPLICATION_JSON);
String body = getBody(response);
LinkedTreeMap<?, ?> map = getLinkedTreeMap(body);
List<LinkedTreeMap<String, String>> results = (List) map.get("results");
for (LinkedTreeMap<String, String> result : results) {
if(result.get("Name").equals("Small green room")) {
return result.get("Id");
}
}
return null;
}
示例8: createEntryRoomWithInlineEmptyFeedArray
import org.apache.olingo.odata2.api.commons.HttpStatusCodes; //导入依赖的package包/类
@Test
public void createEntryRoomWithInlineEmptyFeedArray() throws Exception {
String content = "{\"d\":{\"__metadata\":{\"id\":\"" + getEndpoint() + "Rooms('1')\","
+ "\"uri\":\"" + getEndpoint() + "Rooms('1')\",\"type\":\"RefScenario.Room\","
+ "\"etag\":\"W/\\\"3\\\"\"},"
+ "\"Id\":\"1\",\"Name\":\"Room 104\",\"Seats\":4,\"Version\":2,"
+ "\"nr_Employees\":[],"
+ "\"nr_Building\":{\"__deferred\":{\"uri\":\"" + getEndpoint() + "Rooms('1')/nr_Building\"}}}}";
HttpResponse response =
postUri("Rooms", content, HttpContentType.APPLICATION_JSON, HttpHeaders.ACCEPT,
HttpContentType.APPLICATION_JSON, HttpStatusCodes.CREATED);
checkMediaType(response, HttpContentType.APPLICATION_JSON);
checkEtag(response, "W/\"2\"");
String body = getBody(response);
LinkedTreeMap<?, ?> map = getLinkedTreeMap(body);
assertEquals("104", map.get("Id"));
assertEquals("Room 104", map.get("Name"));
@SuppressWarnings("unchecked")
LinkedTreeMap<String, String> metadataMap = (LinkedTreeMap<String, String>) map.get("__metadata");
assertNotNull(metadataMap);
assertEquals(getEndpoint() + "Rooms('104')", metadataMap.get("id"));
assertEquals("RefScenario.Room", metadataMap.get("type"));
assertEquals(getEndpoint() + "Rooms('104')", metadataMap.get("uri"));
}
示例9: testSplit0
import org.apache.olingo.odata2.api.commons.HttpStatusCodes; //导入依赖的package包/类
@Test
public void testSplit0() throws IOException, ODataException {
server.setPathSplit(0);
startServer();
final HttpGet get = new HttpGet(URI.create(server.getEndpoint().toString() + "/$metadata"));
final HttpResponse response = httpClient.execute(get);
assertEquals(HttpStatusCodes.OK.getStatusCode(), response.getStatusLine().getStatusCode());
final ODataContext ctx = service.getProcessor().getContext();
assertNotNull(ctx);
assertTrue(ctx.getPathInfo().getPrecedingSegments().isEmpty());
assertEquals("$metadata", ctx.getPathInfo().getODataSegments().get(0).getPath());
}
示例10: createWithLargeProperty
import org.apache.olingo.odata2.api.commons.HttpStatusCodes; //导入依赖的package包/类
@Test
public void createWithLargeProperty() throws Exception {
final String largeTeamName = StringHelper.generateData(888888);
// Create an entry for a type that has no media resource.
final String requestBody = getBody(callUri("Teams('1')"))
.replace("'1'", "'9'")
.replace("Id>1", "Id>9")
.replace("Team 1", largeTeamName)
.replaceAll("<link.+?/>", "");
HttpResponse response =
postUri("Teams()", requestBody, HttpContentType.APPLICATION_ATOM_XML_ENTRY, HttpStatusCodes.CREATED);
checkMediaType(response, HttpContentType.APPLICATION_ATOM_XML_UTF8 + ";type=entry");
assertEquals(getEndpoint() + "Teams('4')", response.getFirstHeader(HttpHeaders.LOCATION).getValue());
assertNull(response.getFirstHeader(HttpHeaders.ETAG));
assertXpathEvaluatesTo(largeTeamName, "/atom:entry/atom:content/m:properties/d:Name", getBody(response));
}
示例11: createEntryRoomWithLink
import org.apache.olingo.odata2.api.commons.HttpStatusCodes; //导入依赖的package包/类
@Test
public void createEntryRoomWithLink() throws Exception {
String content = "{\"d\":{\"__metadata\":{\"id\":\"" + getEndpoint() + "Rooms('1')\","
+ "\"uri\":\"" + getEndpoint() + "Rooms('1')\",\"type\":\"RefScenario.Room\","
+ "\"etag\":\"W/\\\"3\\\"\"},"
+ "\"Id\":\"1\",\"Name\":\"Room 104\","
+ "\"nr_Employees\":{\"__deferred\":{\"uri\":\"" + getEndpoint() + "Rooms('1')/nr_Employees\"}},"
+ "\"nr_Building\":{\"__deferred\":{\"uri\":\"" + getEndpoint() + "Rooms('1')/nr_Building\"}}}}";
assertNotNull(content);
HttpResponse response =
postUri("Rooms", content, HttpContentType.APPLICATION_JSON, HttpHeaders.ACCEPT,
HttpContentType.APPLICATION_JSON, HttpStatusCodes.CREATED);
checkMediaType(response, HttpContentType.APPLICATION_JSON);
String body = getBody(response);
LinkedTreeMap<?, ?> map = getLinkedTreeMap(body);
assertEquals("104", map.get("Id"));
assertEquals("Room 104", map.get("Name"));
@SuppressWarnings("unchecked")
LinkedTreeMap<String, Object> employeesMap = (LinkedTreeMap<String, Object>) map.get("nr_Employees");
assertNotNull(employeesMap);
@SuppressWarnings("unchecked")
LinkedTreeMap<String, String> deferredMap = (LinkedTreeMap<String, String>) employeesMap.get("__deferred");
assertNotNull(deferredMap);
assertEquals(getEndpoint() + "Rooms('104')/nr_Employees", deferredMap.get("uri"));
}
示例12: writeErrorDocument
import org.apache.olingo.odata2.api.commons.HttpStatusCodes; //导入依赖的package包/类
/**
* <p>Serializes an error message according to the OData standard.</p>
* <p>In case an error occurs, it is logged.
* An exception is not thrown because this method is used in exception handling.</p>
* @param status the {@link HttpStatusCodes status code} associated with this error
* @param errorCode a String that serves as a substatus to the HTTP response code
* @param message a human-readable message describing the error
* @param locale the {@link Locale} that should be used to format the error message
* @param innerError the inner error for this message; if it is null or an empty String
* no inner error tag is shown inside the response structure
* @return an {@link ODataResponse} containing the serialized error message
*/
@Override
public ODataResponse writeErrorDocument(final HttpStatusCodes status, final String errorCode, final String message,
final Locale locale, final String innerError) {
CircleStreamBuffer buffer = new CircleStreamBuffer();
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET));
new JsonErrorDocumentProducer().writeErrorDocument(writer, errorCode, message, locale, innerError);
writer.flush();
buffer.closeWrite();
return ODataResponse.status(status)
.entity(buffer.getInputStream())
.header(ODataHttpHeaders.DATASERVICEVERSION, ODataServiceVersion.V10)
.build();
} catch (Exception e) {
buffer.close();
throw new ODataRuntimeException(e);
}
}
示例13: enhanceContextWithMessageException
import org.apache.olingo.odata2.api.commons.HttpStatusCodes; //导入依赖的package包/类
private void enhanceContextWithMessageException(final ODataMessageException toHandleException) {
errorContext.setErrorCode(toHandleException.getErrorCode());
MessageReference messageReference = toHandleException.getMessageReference();
Message localizedMessage = messageReference == null ? null : extractEntity(messageReference);
if (localizedMessage != null) {
errorContext.setMessage(localizedMessage.getText());
errorContext.setLocale(localizedMessage.getLocale());
}
if (toHandleException instanceof ODataHttpException) {
errorContext.setHttpStatus(((ODataHttpException) toHandleException).getHttpStatus());
} else if (toHandleException instanceof EntityProviderException) {
if(toHandleException instanceof EntityProviderProducerException){
/*
* As per OLINGO-763 serializer exceptions are produced by the server and must therefore result
* in a 500 internal server error
*/
errorContext.setHttpStatus(HttpStatusCodes.INTERNAL_SERVER_ERROR);
}else{
errorContext.setHttpStatus(HttpStatusCodes.BAD_REQUEST);
}
} else if (toHandleException instanceof BatchException) {
errorContext.setHttpStatus(HttpStatusCodes.BAD_REQUEST);
}
}
示例14: testBaseUriWithMatrixParameter
import org.apache.olingo.odata2.api.commons.HttpStatusCodes; //导入依赖的package包/类
@Test
public void testBaseUriWithMatrixParameter() throws IOException, ODataException,
URISyntaxException {
server.setPathSplit(3);
startServer();
final String endpoint = server.getEndpoint().toString();
final HttpGet get = new HttpGet(URI.create(endpoint + "aaa/bbb;n=2,3;m=1/ccc/"));
final HttpResponse response = httpClient.execute(get);
assertEquals(HttpStatusCodes.OK.getStatusCode(), response.getStatusLine().getStatusCode());
final ODataContext ctx = service.getProcessor().getContext();
assertNotNull(ctx);
validateServiceRoot(ctx.getPathInfo().getServiceRoot().toASCIIString(),
endpoint + "aaa/bbb;", "/ccc/", "n=2,3", "m=1");
}
示例15: test404HttpNotFound
import org.apache.olingo.odata2.api.commons.HttpStatusCodes; //导入依赖的package包/类
@Test
public void test404HttpNotFound() throws Exception {
when(processor.readEntity(any(GetEntityUriInfo.class), any(String.class))).thenThrow(
new ODataNotFoundException(ODataNotFoundException.ENTITY));
final HttpResponse response = executeGetRequest("Managers('199')");
assertEquals(HttpStatusCodes.NOT_FOUND.getStatusCode(), response.getStatusLine().getStatusCode());
final String content = StringHelper.inputStreamToString(response.getEntity().getContent());
Map<String, String> prefixMap = new HashMap<String, String>();
prefixMap.put("a", Edm.NAMESPACE_M_2007_08);
XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(prefixMap));
assertXpathExists("/a:error/a:code", content);
assertXpathValuesEqual("\"" + MessageService.getMessage(Locale.ENGLISH, ODataNotFoundException.ENTITY).getText()
+ "\"", "/a:error/a:message", content);
}