NIFI-10898 This closes #6729. Removed unused test HTTP Servlets

Signed-off-by: Joe Witt <joewitt@apache.org>
This commit is contained in:
exceptionfactory 2022-11-29 09:20:26 -06:00 committed by Joe Witt
parent e5941c4bf8
commit 47fc3ae77d
No known key found for this signature in database
GPG Key ID: 9093BF854F811A1A
8 changed files with 0 additions and 502 deletions

View File

@ -1,80 +0,0 @@
/*
* 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 org.apache.nifi.processors.standard;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response.Status;
import org.apache.nifi.flowfile.attributes.StandardFlowFileMediaType;
import org.apache.nifi.stream.io.StreamUtils;
import org.apache.nifi.util.file.FileUtils;
public class CaptureServlet extends HttpServlet {
private static final long serialVersionUID = 8402271018449653919L;
private volatile byte[] lastPost;
private volatile Map<String, String> lastPostHeaders;
public byte[] getLastPost() {
return lastPost;
}
public Map<String, String> getLastPostHeaders() {
return lastPostHeaders;
}
@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Capture all the headers for reference. Intentionally choosing to not special handling for headers with multiple values for clarity
final Enumeration<String> headerNames = request.getHeaderNames();
lastPostHeaders = new HashMap<>();
while (headerNames.hasMoreElements()) {
final String nextHeader = headerNames.nextElement();
lastPostHeaders.put(nextHeader, request.getHeader(nextHeader));
}
try {
StreamUtils.copy(request.getInputStream(), baos);
this.lastPost = baos.toByteArray();
} finally {
FileUtils.closeQuietly(baos);
}
response.setStatus(Status.OK.getStatusCode());
}
@Override
protected void doHead(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
final String acceptHeader = String.format("%s,%s", StandardFlowFileMediaType.VERSION_3.getMediaType(), StandardFlowFileMediaType.VERSION_2.getMediaType());
response.setHeader("Accept", acceptHeader);
response.setHeader("x-nifi-transfer-protocol-version", "1");
// Unless an acceptGzip parameter is explicitly set to false, respond that this server accepts gzip
if (!Boolean.toString(false).equalsIgnoreCase(request.getParameter("acceptGzip"))) {
response.setHeader("Accept-Encoding", "gzip");
}
}
}

View File

@ -1,68 +0,0 @@
/*
* 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 org.apache.nifi.processors.standard;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.DateGenerator;
public class CookieTestingServlet extends HttpServlet {
public static final String DATEMODE_COOKIE_DEFAULT = "cookieDateDefault";
public static final String DATEMODE_COOKIE_NOT_TYPICAL = "cookieDateNotTypical";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String redirect = req.getParameter("redirect");
if (redirect == null) {
redirect = "null";
}
String dateMode = req.getParameter("datemode");
if (dateMode == null) {
dateMode = DATEMODE_COOKIE_DEFAULT;
}
switch (dateMode) {
case DATEMODE_COOKIE_DEFAULT:
default:
// standard way of building a cookie header date uses format "EEE, dd-MMM-yy HH:mm:ss z"
// this results in Set-Cookie: session=abc123; path=/; expires=EEE, dd-MMM-yy HH:mm:ss z; HttpOnly
Cookie cookie = new Cookie("session", "abc123");
cookie.setPath("/");
cookie.setHttpOnly(true);
cookie.setMaxAge(86400);
resp.addCookie(cookie);
break;
case DATEMODE_COOKIE_NOT_TYPICAL:
// hacked way of building a cookie header, to get less-often-used date format "EEE, dd MMM yy HH:mm:ss z"
// this results in Set-Cookie: session=abc123; path=/; expires=EEE, dd MMM yy HH:mm:ss z; HttpOnly
StringBuilder buf = new StringBuilder("session=abc123; path=/; expires=");
buf.append(DateGenerator.formatDate(System.currentTimeMillis() + 1000L * 60 * 60 * 24));
buf.append("; HttpOnly");
resp.addHeader("Set-Cookie", buf.toString());
}
resp.sendRedirect(redirect);
}
}

View File

@ -1,52 +0,0 @@
/*
* 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 org.apache.nifi.processors.standard;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.nifi.stream.io.StreamUtils;
public class CookieVerificationTestingServlet extends HttpServlet {
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
Cookie[] cookies = req.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("session".equals(cookie.getName())) {
final ServletOutputStream out = resp.getOutputStream();
try (final FileInputStream fis = new FileInputStream("src/test/resources/hello.txt")) {
StreamUtils.copy(fis, out);
return;
}
}
}
}
// session cookie not found, error
resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Did not receive expected session cookie");
}
}

View File

@ -1,42 +0,0 @@
/*
* 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 org.apache.nifi.processors.standard;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.nifi.stream.io.StreamUtils;
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = -8821242726929583763L;
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
final ServletOutputStream out = resp.getOutputStream();
try (final FileInputStream fis = new FileInputStream("src/test/resources/hello.txt")) {
StreamUtils.copy(fis, out);
}
}
}

View File

@ -1,51 +0,0 @@
/*
* 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 org.apache.nifi.processors.standard;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Class to mock HTTP errors responses
*/
public class HttpErrorServlet extends HttpServlet {
/** serial UID */
private static final long serialVersionUID = 7428077098486751916L;
/** error to return */
private int errorToReturn = HttpServletResponse.SC_NOT_FOUND;
/**
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
resp.sendError(this.errorToReturn);
}
/**
* Set the HTTP error to return
* @param errorToReturn error to return
*/
public void setErrorToReturn(int errorToReturn) {
this.errorToReturn = errorToReturn;
}
}

View File

@ -1,78 +0,0 @@
/*
* 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 org.apache.nifi.processors.standard;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RESTServiceContentModified extends HttpServlet {
private static final long serialVersionUID = 1L;
static String result = "[\"sample1\",\"sample2\",\"sample3\",\"sample4\"]";
static long modificationDate = System.currentTimeMillis() / 1000 * 1000; // time resolution is to the second
static int ETAG;
public static boolean IGNORE_ETAG = false;
public static boolean IGNORE_LAST_MODIFIED = false;
public RESTServiceContentModified() {
ETAG = this.hashCode();
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String ifModifiedSince = request.getHeader("If-Modified-Since");
String ifNoneMatch = request.getHeader("If-None-Match");
final SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
response.setContentType("application/json");
if (ifNoneMatch != null && ifNoneMatch.length() > 0 && !IGNORE_ETAG && Integer.parseInt(ifNoneMatch) == ETAG) {
response.setStatus(304);
response.setHeader("Last-Modified", dateFormat.format(modificationDate));
response.setHeader("ETag", Integer.toString(ETAG));
return;
}
long date = -1;
if (ifModifiedSince != null && ifModifiedSince.length() > 0 && !IGNORE_LAST_MODIFIED) {
try {
date = dateFormat.parse(ifModifiedSince).getTime();
} catch (Exception e) {
}
}
if (date >= modificationDate) {
response.setStatus(304);
response.setHeader("Last-Modified", dateFormat.format(modificationDate));
response.setHeader("ETag", Integer.toString(ETAG));
return;
}
response.setStatus(200);
response.setHeader("Last-Modified", dateFormat.format(modificationDate));
response.setHeader("ETag", Integer.toString(ETAG));
response.getOutputStream().println(result);
}
}

View File

@ -1,90 +0,0 @@
/*
* 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 org.apache.nifi.processors.standard;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.concurrent.ArrayBlockingQueue;
public class UDPTestServer implements Runnable {
private final int MAX_DATAGRAM_PACKET_SIZE = 1000000;
private final InetAddress ipAddress;
private final int port;
private volatile DatagramSocket serverSocket;
private final ArrayBlockingQueue<DatagramPacket> recvQueue;
public UDPTestServer(final InetAddress ipAddress, final int port, final ArrayBlockingQueue<DatagramPacket> recvQueue) {
this.ipAddress = ipAddress;
this.port = port;
this.recvQueue = recvQueue;
}
public synchronized void startServer() throws SocketException {
if (!isRunning()) {
serverSocket = new DatagramSocket(port, ipAddress);
Thread t = new Thread(this);
t.setName(this.getClass().getSimpleName());
t.start();
}
}
public synchronized void shutdownServer() {
if (isRunning()) {
serverSocket.close();
serverSocket = null;
}
}
private DatagramPacket createDatagramPacket() {
return new DatagramPacket(new byte[MAX_DATAGRAM_PACKET_SIZE], MAX_DATAGRAM_PACKET_SIZE);
}
private void storeReceivedPacket(final DatagramPacket packet) {
recvQueue.add(packet);
}
private boolean isRunning() {
return serverSocket != null && !serverSocket.isClosed();
}
public DatagramPacket getReceivedPacket() {
return recvQueue.poll();
}
public int getLocalPort() {
return serverSocket == null ? 0 : serverSocket.getLocalPort();
}
@Override
public void run() {
try {
while (isRunning()) {
DatagramPacket packet = createDatagramPacket();
serverSocket.receive(packet);
storeReceivedPacket(packet);
}
} catch (Exception e) {
// Do Nothing
} finally {
shutdownServer();
}
}
}

View File

@ -1,41 +0,0 @@
/*
* 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 org.apache.nifi.processors.standard;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UserAgentTestingServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public UserAgentTestingServlet() {
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String userAgent = request.getHeader("User-Agent");
if ("testUserAgent".equals(userAgent)) {
response.setStatus(200);
} else {
response.setStatus(500);
}
}
}