[OLINGO-659] Minor code clean up
This commit is contained in:
parent
1b200437b4
commit
f187ad3513
|
@ -87,7 +87,7 @@ public class BatchBodyPart implements BatchPart {
|
||||||
private List<List<Line>> splitChangeSet(List<Line> remainingMessage) throws BatchDeserializerException {
|
private List<List<Line>> splitChangeSet(List<Line> remainingMessage) throws BatchDeserializerException {
|
||||||
|
|
||||||
final HeaderField contentTypeField = headers.getHeaderField(HttpHeader.CONTENT_TYPE);
|
final HeaderField contentTypeField = headers.getHeaderField(HttpHeader.CONTENT_TYPE);
|
||||||
final String changeSetBoundary = BatchParserCommon.getBoundary(contentTypeField.getValueNotNull(),
|
final String changeSetBoundary = BatchParserCommon.getBoundary(contentTypeField.getValue(),
|
||||||
contentTypeField.getLineNumber());
|
contentTypeField.getLineNumber());
|
||||||
validateChangeSetBoundary(changeSetBoundary, headers);
|
validateChangeSetBoundary(changeSetBoundary, headers);
|
||||||
|
|
||||||
|
|
|
@ -46,9 +46,9 @@ public class BatchParserCommon {
|
||||||
public static String getBoundary(final String contentType, final int line) throws BatchDeserializerException {
|
public static String getBoundary(final String contentType, final int line) throws BatchDeserializerException {
|
||||||
final ContentType type = getContentType(contentType, ContentType.MULTIPART_MIXED, line);
|
final ContentType type = getContentType(contentType, ContentType.MULTIPART_MIXED, line);
|
||||||
final Map<String, String> parameters = type.getParameters();
|
final Map<String, String> parameters = type.getParameters();
|
||||||
for (final String parameterName : parameters.keySet()) {
|
for (final Map.Entry<String, String> entries : parameters.entrySet()) {
|
||||||
if (BOUNDARY.equalsIgnoreCase(parameterName)) {
|
if (BOUNDARY.equalsIgnoreCase(entries.getKey())) {
|
||||||
final String boundary = parameters.get(parameterName).trim();
|
final String boundary = entries.getValue().trim();
|
||||||
if (boundary.matches(PATTERN_BOUNDARY)) {
|
if (boundary.matches(PATTERN_BOUNDARY)) {
|
||||||
return trimQuotes(boundary);
|
return trimQuotes(boundary);
|
||||||
} else {
|
} else {
|
||||||
|
@ -63,7 +63,7 @@ public class BatchParserCommon {
|
||||||
|
|
||||||
public static ContentType getContentType(final String contentType, final ContentType expected, final int line)
|
public static ContentType getContentType(final String contentType, final ContentType expected, final int line)
|
||||||
throws BatchDeserializerException {
|
throws BatchDeserializerException {
|
||||||
ContentType type = null;
|
ContentType type;
|
||||||
try {
|
try {
|
||||||
type = ContentType.create(contentType);
|
type = ContentType.create(contentType);
|
||||||
} catch (final IllegalArgumentException e) {
|
} catch (final IllegalArgumentException e) {
|
||||||
|
@ -83,11 +83,6 @@ public class BatchParserCommon {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String removeEndingSlash(final String content) {
|
|
||||||
String newContent = content.trim();
|
|
||||||
return newContent.endsWith("/") ? newContent.substring(0, newContent.length() - 1) : newContent;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String trimQuotes(final String boundary) {
|
private static String trimQuotes(final String boundary) {
|
||||||
if (boundary != null && boundary.length() >= 2 && boundary.startsWith("\"") && boundary.endsWith("\"")) {
|
if (boundary != null && boundary.length() >= 2 && boundary.startsWith("\"") && boundary.endsWith("\"")) {
|
||||||
return boundary.substring(1, boundary.length() - 1);
|
return boundary.substring(1, boundary.length() - 1);
|
||||||
|
|
|
@ -73,12 +73,6 @@ public class Header implements Iterable<HeaderField> {
|
||||||
return (headerField == null) ? null : headerField.getValue();
|
return (headerField == null) ? null : headerField.getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHeaderNotNull(final String name) {
|
|
||||||
final HeaderField headerField = getHeaderField(name);
|
|
||||||
|
|
||||||
return (headerField == null) ? "" : headerField.getValueNotNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getHeaders(final String name) {
|
public List<String> getHeaders(final String name) {
|
||||||
final HeaderField headerField = getHeaderField(name);
|
final HeaderField headerField = getHeaderField(name);
|
||||||
|
|
||||||
|
@ -96,9 +90,9 @@ public class Header implements Iterable<HeaderField> {
|
||||||
public Map<String, String> toSingleMap() {
|
public Map<String, String> toSingleMap() {
|
||||||
final Map<String, String> singleMap = new HashMap<String, String>();
|
final Map<String, String> singleMap = new HashMap<String, String>();
|
||||||
|
|
||||||
for (final String key : headers.keySet()) {
|
for (final Map.Entry<String, HeaderField> entries : headers.entrySet()) {
|
||||||
HeaderField field = headers.get(key);
|
HeaderField field = entries.getValue();
|
||||||
singleMap.put(field.getFieldName(), getHeader(key));
|
singleMap.put(field.getFieldName(), getHeader(entries.getKey()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return singleMap;
|
return singleMap;
|
||||||
|
@ -107,8 +101,8 @@ public class Header implements Iterable<HeaderField> {
|
||||||
public Map<String, List<String>> toMultiMap() {
|
public Map<String, List<String>> toMultiMap() {
|
||||||
final Map<String, List<String>> singleMap = new HashMap<String, List<String>>();
|
final Map<String, List<String>> singleMap = new HashMap<String, List<String>>();
|
||||||
|
|
||||||
for (final String key : headers.keySet()) {
|
for (final Map.Entry<String, HeaderField> entries : headers.entrySet()) {
|
||||||
HeaderField field = headers.get(key);
|
HeaderField field = entries.getValue();
|
||||||
singleMap.put(field.getFieldName(), field.getValues());
|
singleMap.put(field.getFieldName(), field.getValues());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,8 +124,8 @@ public class Header implements Iterable<HeaderField> {
|
||||||
public Header clone() {
|
public Header clone() {
|
||||||
final Header newInstance = new Header(lineNumber);
|
final Header newInstance = new Header(lineNumber);
|
||||||
|
|
||||||
for (final String key : headers.keySet()) {
|
for (final Map.Entry<String, HeaderField> entries : headers.entrySet()) {
|
||||||
newInstance.headers.put(key, headers.get(key).clone());
|
newInstance.headers.put(entries.getKey(), entries.getValue().clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
return newInstance;
|
return newInstance;
|
||||||
|
|
|
@ -59,15 +59,9 @@ public class HeaderField implements Cloneable {
|
||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getValueNotNull() {
|
|
||||||
final String value = getValue();
|
|
||||||
|
|
||||||
return (value == null) ? "" : value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public HeaderField clone() {
|
public HeaderField clone() {
|
||||||
List<String> newValues = new ArrayList<String>();
|
List<String> newValues = new ArrayList<String>(values.size());
|
||||||
newValues.addAll(values);
|
newValues.addAll(values);
|
||||||
|
|
||||||
return new HeaderField(fieldName, newValues, lineNumber);
|
return new HeaderField(fieldName, newValues, lineNumber);
|
||||||
|
|
|
@ -102,9 +102,9 @@ public class PreferParser {
|
||||||
}
|
}
|
||||||
if (matcher.hitEnd()) {
|
if (matcher.hitEnd()) {
|
||||||
// Here we also have to keep already existing preferences.
|
// Here we also have to keep already existing preferences.
|
||||||
for (final String key : partResult.keySet()) {
|
for (final Map.Entry<String, Preference> entry : partResult.entrySet()) {
|
||||||
if (!result.containsKey(key)) {
|
if (!result.containsKey(entry.getKey())) {
|
||||||
result.put(key, partResult.get(key));
|
result.put(entry.getKey(), entry.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,8 +61,8 @@ public class AsyncResponseSerializer {
|
||||||
final ByteArrayOutputStream buffer) throws IOException {
|
final ByteArrayOutputStream buffer) throws IOException {
|
||||||
final Map<String, String> header = response.getHeaders();
|
final Map<String, String> header = response.getHeaders();
|
||||||
|
|
||||||
for (final String key: header.keySet()) {
|
for (final Map.Entry<String, String> entry: header.entrySet()) {
|
||||||
appendHeader(key, header.get(key), buffer);
|
appendHeader(entry.getKey(), entry.getValue(), buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -105,7 +105,7 @@ public class BatchResponseSerializer {
|
||||||
|
|
||||||
private void appendChangeSetHeader(final BodyBuilder builder, final String changeSetBoundary) {
|
private void appendChangeSetHeader(final BodyBuilder builder, final String changeSetBoundary) {
|
||||||
appendHeader(HttpHeader.CONTENT_TYPE, HttpContentType.MULTIPART_MIXED + "; boundary="
|
appendHeader(HttpHeader.CONTENT_TYPE, HttpContentType.MULTIPART_MIXED + "; boundary="
|
||||||
+ changeSetBoundary, builder);
|
+ changeSetBoundary, builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void appendHeader(final String name, final String value, final BodyBuilder builder) {
|
private void appendHeader(final String name, final String value, final BodyBuilder builder) {
|
||||||
|
@ -121,18 +121,26 @@ public class BatchResponseSerializer {
|
||||||
.append(SP)
|
.append(SP)
|
||||||
.append(response.getStatusCode())
|
.append(response.getStatusCode())
|
||||||
.append(SP)
|
.append(SP)
|
||||||
.append(HttpStatusCode.fromStatusCode(response.getStatusCode()).toString())
|
.append(getStatusCodeInfo(response))
|
||||||
.append(CRLF);
|
.append(CRLF);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getStatusCodeInfo(ODataResponse response) {
|
||||||
|
HttpStatusCode status = HttpStatusCode.fromStatusCode(response.getStatusCode());
|
||||||
|
if(status == null) {
|
||||||
|
throw new ODataRuntimeException("Invalid status code in response '" + response.getStatusCode() + "'");
|
||||||
|
}
|
||||||
|
return status.getInfo();
|
||||||
|
}
|
||||||
|
|
||||||
private void appendResponseHeader(final ODataResponse response, final int contentLength,
|
private void appendResponseHeader(final ODataResponse response, final int contentLength,
|
||||||
final BodyBuilder builder) {
|
final BodyBuilder builder) {
|
||||||
final Map<String, String> header = response.getHeaders();
|
final Map<String, String> header = response.getHeaders();
|
||||||
|
|
||||||
for (final String key : header.keySet()) {
|
for (final Map.Entry<String, String> entry : header.entrySet()) {
|
||||||
// Requests do never has a content id header
|
// Requests do never has a content id header
|
||||||
if (!key.equalsIgnoreCase(HttpHeader.CONTENT_ID)) {
|
if (!entry.getKey().equalsIgnoreCase(HttpHeader.CONTENT_ID)) {
|
||||||
appendHeader(key, header.get(key), builder);
|
appendHeader(entry.getKey(), entry.getValue(), builder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,6 @@ public class HeaderTest {
|
||||||
|
|
||||||
assertNull(header.getHeader(HttpHeader.CONTENT_TYPE));
|
assertNull(header.getHeader(HttpHeader.CONTENT_TYPE));
|
||||||
assertEquals(0, header.getHeaders(HttpHeader.CONTENT_TYPE).size());
|
assertEquals(0, header.getHeaders(HttpHeader.CONTENT_TYPE).size());
|
||||||
assertEquals("", header.getHeaderNotNull(HttpHeader.CONTENT_TYPE));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue