
com.ghostframe.postmandoc.postman.PostmanRequestFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of postmandoc-maven-plugin Show documentation
Show all versions of postmandoc-maven-plugin Show documentation
Maven plugin for generating Postman collections from Spring REST Docs snippets
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ghostframe.postmandoc.postman;
import com.ghostframe.postmandoc.http.HttpParser;
import com.ghostframe.postmandoc.postman.domain.PostmanHeader;
import com.ghostframe.postmandoc.postman.domain.PostmanRequest;
import com.ghostframe.postmandoc.postman.domain.PostmanRequestBody;
import java.io.IOException;
import static java.util.Arrays.asList;
import java.util.List;
import static java.util.stream.Collectors.toList;
import java.util.stream.Stream;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpException;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpRequest;
public class PostmanRequestFactory {
private static final String ADOC_HEADER = "[source,http,options=\"nowrap\"]\n----\n";
private static final String ADOC_PAYLOAD = "\n----";
private static final String HTTP_URL_PREFIX = "http://";
private static final String BLANK_LINE_WIN = "\n\r\n";
private static final String BLANK_LINE_LINUX = "\n\n";
private static final List IGNORED_HTTP_HEADER_NAMES = asList(HttpHeaders.HOST, HttpHeaders.CONTENT_LENGTH);
public static PostmanRequest fromHttpRequestSnippet(String httpRequestSnippet, String replacementHost) throws IOException, HttpException {
String httpRequestText = stripAdocHeaderAndPayload(httpRequestSnippet);
HttpRequest httpRequest = HttpParser.parse(httpRequestText);
return PostmanRequest.builder()
.body(createPostmanRequestBody(httpRequestText))
.url(getUrl(httpRequest, replacementHost))
.method(httpRequest.getRequestLine().getMethod())
.header(createHeaders(httpRequest))
.build();
}
private static String stripAdocHeaderAndPayload(String httpRequestSnippet) {
String noHeader = httpRequestSnippet.substring(ADOC_HEADER.length());
String noHeaderAndNoPayload = noHeader.substring(0, noHeader.length() - ADOC_PAYLOAD.length());
return noHeaderAndNoPayload;
}
private static String getUrl(HttpRequest httpRequest, String replacementHost) {
String host = replacementHost != null ? replacementHost : httpRequest.getFirstHeader(HttpHeaders.HOST).getValue();
return HTTP_URL_PREFIX + host + httpRequest.getRequestLine().getUri();
}
private static PostmanRequestBody createPostmanRequestBody(String httpRequestText) {
return PostmanRequestBody.builder()
.mode("raw")
.raw(extractBody(httpRequestText))
.build();
}
private static String extractBody(String httpRequestText) {
if (hasLinuxEndOfLine(httpRequestText)) {
return convertEolToWindows(StringUtils.substringAfter(httpRequestText, BLANK_LINE_LINUX));
} else {
return StringUtils.substringAfter(httpRequestText, BLANK_LINE_WIN);
}
}
private static boolean hasLinuxEndOfLine(String string) {
return !string.contains(BLANK_LINE_WIN);
}
private static String convertEolToWindows(String entrada) {
if (!entrada.contains(BLANK_LINE_WIN)) {
return entrada.replaceAll("\\n", "\r\n");
} else {
return entrada;
}
}
private static List createHeaders(HttpRequest httpRequest) {
return Stream.of(httpRequest.getAllHeaders())
.filter(header -> !IGNORED_HTTP_HEADER_NAMES.contains(header.getName()))
.map(header -> new PostmanHeader(header.getName(), header.getValue(), null))
.collect(toList());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy