[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 {
|
||||
|
||||
final HeaderField contentTypeField = headers.getHeaderField(HttpHeader.CONTENT_TYPE);
|
||||
final String changeSetBoundary = BatchParserCommon.getBoundary(contentTypeField.getValueNotNull(),
|
||||
final String changeSetBoundary = BatchParserCommon.getBoundary(contentTypeField.getValue(),
|
||||
contentTypeField.getLineNumber());
|
||||
validateChangeSetBoundary(changeSetBoundary, headers);
|
||||
|
||||
|
|
|
@ -46,9 +46,9 @@ public class BatchParserCommon {
|
|||
public static String getBoundary(final String contentType, final int line) throws BatchDeserializerException {
|
||||
final ContentType type = getContentType(contentType, ContentType.MULTIPART_MIXED, line);
|
||||
final Map<String, String> parameters = type.getParameters();
|
||||
for (final String parameterName : parameters.keySet()) {
|
||||
if (BOUNDARY.equalsIgnoreCase(parameterName)) {
|
||||
final String boundary = parameters.get(parameterName).trim();
|
||||
for (final Map.Entry<String, String> entries : parameters.entrySet()) {
|
||||
if (BOUNDARY.equalsIgnoreCase(entries.getKey())) {
|
||||
final String boundary = entries.getValue().trim();
|
||||
if (boundary.matches(PATTERN_BOUNDARY)) {
|
||||
return trimQuotes(boundary);
|
||||
} else {
|
||||
|
@ -63,7 +63,7 @@ public class BatchParserCommon {
|
|||
|
||||
public static ContentType getContentType(final String contentType, final ContentType expected, final int line)
|
||||
throws BatchDeserializerException {
|
||||
ContentType type = null;
|
||||
ContentType type;
|
||||
try {
|
||||
type = ContentType.create(contentType);
|
||||
} 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) {
|
||||
if (boundary != null && boundary.length() >= 2 && boundary.startsWith("\"") && boundary.endsWith("\"")) {
|
||||
return boundary.substring(1, boundary.length() - 1);
|
||||
|
|
|
@ -73,12 +73,6 @@ public class Header implements Iterable<HeaderField> {
|
|||
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) {
|
||||
final HeaderField headerField = getHeaderField(name);
|
||||
|
||||
|
@ -96,9 +90,9 @@ public class Header implements Iterable<HeaderField> {
|
|||
public Map<String, String> toSingleMap() {
|
||||
final Map<String, String> singleMap = new HashMap<String, String>();
|
||||
|
||||
for (final String key : headers.keySet()) {
|
||||
HeaderField field = headers.get(key);
|
||||
singleMap.put(field.getFieldName(), getHeader(key));
|
||||
for (final Map.Entry<String, HeaderField> entries : headers.entrySet()) {
|
||||
HeaderField field = entries.getValue();
|
||||
singleMap.put(field.getFieldName(), getHeader(entries.getKey()));
|
||||
}
|
||||
|
||||
return singleMap;
|
||||
|
@ -107,8 +101,8 @@ public class Header implements Iterable<HeaderField> {
|
|||
public Map<String, List<String>> toMultiMap() {
|
||||
final Map<String, List<String>> singleMap = new HashMap<String, List<String>>();
|
||||
|
||||
for (final String key : headers.keySet()) {
|
||||
HeaderField field = headers.get(key);
|
||||
for (final Map.Entry<String, HeaderField> entries : headers.entrySet()) {
|
||||
HeaderField field = entries.getValue();
|
||||
singleMap.put(field.getFieldName(), field.getValues());
|
||||
}
|
||||
|
||||
|
@ -130,8 +124,8 @@ public class Header implements Iterable<HeaderField> {
|
|||
public Header clone() {
|
||||
final Header newInstance = new Header(lineNumber);
|
||||
|
||||
for (final String key : headers.keySet()) {
|
||||
newInstance.headers.put(key, headers.get(key).clone());
|
||||
for (final Map.Entry<String, HeaderField> entries : headers.entrySet()) {
|
||||
newInstance.headers.put(entries.getKey(), entries.getValue().clone());
|
||||
}
|
||||
|
||||
return newInstance;
|
||||
|
|
|
@ -59,15 +59,9 @@ public class HeaderField implements Cloneable {
|
|||
return result.toString();
|
||||
}
|
||||
|
||||
public String getValueNotNull() {
|
||||
final String value = getValue();
|
||||
|
||||
return (value == null) ? "" : value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HeaderField clone() {
|
||||
List<String> newValues = new ArrayList<String>();
|
||||
List<String> newValues = new ArrayList<String>(values.size());
|
||||
newValues.addAll(values);
|
||||
|
||||
return new HeaderField(fieldName, newValues, lineNumber);
|
||||
|
|
|
@ -102,9 +102,9 @@ public class PreferParser {
|
|||
}
|
||||
if (matcher.hitEnd()) {
|
||||
// Here we also have to keep already existing preferences.
|
||||
for (final String key : partResult.keySet()) {
|
||||
if (!result.containsKey(key)) {
|
||||
result.put(key, partResult.get(key));
|
||||
for (final Map.Entry<String, Preference> entry : partResult.entrySet()) {
|
||||
if (!result.containsKey(entry.getKey())) {
|
||||
result.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -61,8 +61,8 @@ public class AsyncResponseSerializer {
|
|||
final ByteArrayOutputStream buffer) throws IOException {
|
||||
final Map<String, String> header = response.getHeaders();
|
||||
|
||||
for (final String key: header.keySet()) {
|
||||
appendHeader(key, header.get(key), buffer);
|
||||
for (final Map.Entry<String, String> entry: header.entrySet()) {
|
||||
appendHeader(entry.getKey(), entry.getValue(), buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -105,7 +105,7 @@ public class BatchResponseSerializer {
|
|||
|
||||
private void appendChangeSetHeader(final BodyBuilder builder, final String changeSetBoundary) {
|
||||
appendHeader(HttpHeader.CONTENT_TYPE, HttpContentType.MULTIPART_MIXED + "; boundary="
|
||||
+ changeSetBoundary, builder);
|
||||
+ changeSetBoundary, builder);
|
||||
}
|
||||
|
||||
private void appendHeader(final String name, final String value, final BodyBuilder builder) {
|
||||
|
@ -121,18 +121,26 @@ public class BatchResponseSerializer {
|
|||
.append(SP)
|
||||
.append(response.getStatusCode())
|
||||
.append(SP)
|
||||
.append(HttpStatusCode.fromStatusCode(response.getStatusCode()).toString())
|
||||
.append(getStatusCodeInfo(response))
|
||||
.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,
|
||||
final BodyBuilder builder) {
|
||||
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
|
||||
if (!key.equalsIgnoreCase(HttpHeader.CONTENT_ID)) {
|
||||
appendHeader(key, header.get(key), builder);
|
||||
if (!entry.getKey().equalsIgnoreCase(HttpHeader.CONTENT_ID)) {
|
||||
appendHeader(entry.getKey(), entry.getValue(), builder);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -47,7 +47,6 @@ public class HeaderTest {
|
|||
|
||||
assertNull(header.getHeader(HttpHeader.CONTENT_TYPE));
|
||||
assertEquals(0, header.getHeaders(HttpHeader.CONTENT_TYPE).size());
|
||||
assertEquals("", header.getHeaderNotNull(HttpHeader.CONTENT_TYPE));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue