com.microsoft.kiota.serialization.TextParseNodeFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of microsoft-kiota-serialization-text Show documentation
Show all versions of microsoft-kiota-serialization-text Show documentation
Microsoft Kiota-Serialization for Text
package com.microsoft.kiota.serialization;
import jakarta.annotation.Nonnull;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.stream.Collectors;
/** Creates new text parse nodes */
public class TextParseNodeFactory implements ParseNodeFactory {
/** Creates a new instance of the factory */
public TextParseNodeFactory() {}
@Nonnull public String getValidContentType() {
return validContentType;
}
private static final String validContentType = "text/plain";
@Override
@Nonnull public ParseNode getParseNode(
@Nonnull final String contentType, @Nonnull final InputStream rawResponse) {
Objects.requireNonNull(contentType, "parameter contentType cannot be null");
Objects.requireNonNull(rawResponse, "parameter rawResponse cannot be null");
if (contentType.isEmpty()) {
throw new NullPointerException("contentType cannot be empty");
} else if (!contentType.equals(validContentType)) {
throw new IllegalArgumentException("expected a " + validContentType + " content type");
}
String rawText;
try (final InputStreamReader reader =
new InputStreamReader(rawResponse, StandardCharsets.UTF_8)) {
try (final BufferedReader buff = new BufferedReader(reader)) {
rawText = buff.lines().collect(Collectors.joining("\n"));
}
} catch (IOException ex) {
throw new RuntimeException("could not close the reader", ex);
}
return new TextParseNode(rawText);
}
}