Skip to content

Commit

Permalink
migrate from Java EE to Jakarta EE
Browse files Browse the repository at this point in the history
  • Loading branch information
prutheus committed Sep 11, 2023
1 parent 95acad2 commit 9034697
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 176 deletions.
6 changes: 3 additions & 3 deletions ask-sdk-servlet-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
<version>2.86.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,27 @@

package com.amazon.ask.servlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;

import com.amazon.ask.Skill;
import com.amazon.ask.exception.AskSdkException;
import com.amazon.ask.model.RequestEnvelope;
import com.amazon.ask.model.services.Serializer;
import com.amazon.ask.request.impl.BaseSkillRequest;
import com.amazon.ask.response.SkillResponse;
import com.amazon.ask.servlet.util.ServletUtils;
import com.amazon.ask.servlet.verifiers.AlexaHttpRequest;
import com.amazon.ask.servlet.verifiers.ServletRequest;
import com.amazon.ask.servlet.verifiers.SkillRequestSignatureVerifier;
import com.amazon.ask.servlet.verifiers.SkillRequestTimestampVerifier;
import com.amazon.ask.servlet.verifiers.SkillServletVerifier;
import com.amazon.ask.servlet.verifiers.*;
import com.amazon.ask.util.JacksonSerializer;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;

import static com.amazon.ask.servlet.ServletConstants.DEFAULT_TOLERANCE_MILLIS;

/**
Expand All @@ -58,7 +46,6 @@
* invocation of the right method of the provided {@code Skill} . It also handles sending back
* modified session attributes, user attributes and authentication tokens when needed and handles
* exception cases.
*
*/
public class SkillServlet extends HttpServlet {
/**
Expand Down Expand Up @@ -90,6 +77,7 @@ public class SkillServlet extends HttpServlet {

/**
* Constructor to build an instance of SkillServlet.
*
* @param skill an Alexa skill instance.
*/
public SkillServlet(final Skill skill) {
Expand All @@ -106,7 +94,8 @@ public SkillServlet(final Skill skill) {

/**
* Constructor to build an instance of SkillServlet.
* @param skill instance of {@link Skill}.
*
* @param skill instance of {@link Skill}.
* @param verifiers list of {@link SkillServletVerifier}.
*/
SkillServlet(final Skill skill, final List<SkillServletVerifier> verifiers) {
Expand Down Expand Up @@ -166,7 +155,7 @@ protected void doPost(final HttpServletRequest request, final HttpServletRespons
* {@code SkillServlet} determines the type of request and passes the request to
* the configured {@code Skill}.
*
* @param input - input stream of the request.
* @param input - input stream of the request.
* @param output - output stream of the response.
* @throws IOException if an input or output error is detected when the servlet handles the request
*/
Expand Down Expand Up @@ -198,8 +187,9 @@ public void setProxy(final Proxy proxy) {

/**
* Method throws an {@link NotSerializableException} if the servlet is not serializable.
*
* @param in instance of {@link ObjectInputStream}.
* @throws IOException I/O exception.
* @throws IOException I/O exception.
* @throws ClassNotFoundException cannot a class through its fully-qualified name and can not find its definition on the classpath.
*/
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
Expand All @@ -208,6 +198,7 @@ private void readObject(final ObjectInputStream in) throws IOException, ClassNot

/**
* Method throws an {@link NotSerializableException} if the servlet is not serializable.
*
* @param out instance of {@link ObjectOutputStream}.
* @throws IOException I/O exception.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

import com.amazon.ask.model.RequestEnvelope;
import com.amazon.ask.servlet.ServletConstants;

import javax.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequest;

/**
* Servlet specific implementation of {@link AlexaHttpRequest}.
Expand Down Expand Up @@ -45,8 +44,9 @@ public class ServletRequest implements AlexaHttpRequest {

/**
* Constructor to build an instance of ServletRequest.
* @param httpServletRequest instance of type {@link HttpServletRequest}.
* @param serializedRequestEnvelope serialized request envelope.
*
* @param httpServletRequest instance of type {@link HttpServletRequest}.
* @param serializedRequestEnvelope serialized request envelope.
* @param deserializedRequestEnvelope de-serialized request envelope.
*/
public ServletRequest(final HttpServletRequest httpServletRequest, final byte[] serializedRequestEnvelope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,16 @@

package com.amazon.ask.servlet;

import static com.amazon.ask.util.SdkConstants.FORMAT_VERSION;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.doThrow;

import javax.servlet.http.HttpServletResponse;

import com.amazon.ask.Skill;
import com.amazon.ask.exception.AskSdkException;
import com.amazon.ask.model.LaunchRequest;
import com.amazon.ask.model.Response;
import com.amazon.ask.model.ResponseEnvelope;
import com.amazon.ask.response.impl.BaseSkillResponse;
import com.amazon.ask.servlet.verifiers.SkillRequestSignatureVerifier;
import com.amazon.ask.servlet.verifiers.SkillServletVerifier;
import com.amazon.ask.response.impl.BaseSkillResponse;
import com.amazon.ask.util.impl.JacksonJsonMarshaller;
import jakarta.servlet.http.HttpServletResponse;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -49,6 +38,12 @@
import java.util.Collections;
import java.util.Date;

import static com.amazon.ask.util.SdkConstants.FORMAT_VERSION;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.powermock.api.mockito.PowerMockito.doThrow;

/**
* Tests that the {@link SkillServlet} respects the provided environment variables controlling
* its behavior.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,102 +13,38 @@

package com.amazon.ask.servlet;

import static com.amazon.ask.util.SdkConstants.FORMAT_VERSION;
import static org.mockito.Mockito.when;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.amazon.ask.model.Application;
import com.amazon.ask.model.Request;
import com.amazon.ask.model.RequestEnvelope;
import com.amazon.ask.model.Session;
import com.amazon.ask.model.User;
import com.amazon.ask.model.*;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.mockito.Mockito;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.*;

import static com.amazon.ask.util.SdkConstants.FORMAT_VERSION;
import static org.mockito.Mockito.when;

public class SkillServletTestBase {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

static {
OBJECT_MAPPER.registerModule(new JavaTimeModule());
OBJECT_MAPPER.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}

/**
* Tuple for useful parameters for a servlet invocation.
*/
protected static final class ServletInvocationParameters {
public final HttpServletRequest request;
public final HttpServletResponse response;
public final ByteArrayOutputStream output;

public ServletInvocationParameters(final HttpServletRequest request,
final HttpServletResponse response, final ByteArrayOutputStream output) {
this.request = request;
this.response = response;
this.output = output;
}
}

/**
* Trivial implementation of the ServletInputStream that wraps an InputStream.
*/
private static class MyServletInputStream extends ServletInputStream {
private final InputStream in;

public MyServletInputStream(final InputStream in) {
this.in = in;
}

@Override
public int read() throws IOException {
return in.read();
}
}

/**
* Trivial implementation of the ServletOutputStream that wraps an OutputStream.
*/
private static class MyServletOutputStream extends ServletOutputStream {
private final OutputStream out;

public MyServletOutputStream(final OutputStream out) {
this.out = out;
}

@Override
public void write(int b) throws IOException {
out.write(b);
}
}

// ------------------------------
// Helper methods for subclasses

/**
* Common logic for all the tests.
*
* @param version
* the envelope version for the request envelope
* @param request
* a SkillRequest
* @param session
* a session for this invocation
* @param version the envelope version for the request envelope
* @param request a SkillRequest
* @param session a session for this invocation
* @return invocation parameters for a Skill
*
* @throws IOException
*/
protected ServletInvocationParameters build(final String version, final Request request, final Session session) throws IOException {
Expand Down Expand Up @@ -143,12 +79,9 @@ protected ServletInvocationParameters build(final String version, final Request
* <li>The current SDK version as a version</li>
* </ul>
*
* @param request
* a SkillRequest or OnSessionEndedRequest
* @param session
* a session for this invocation
* @param request a SkillRequest or OnSessionEndedRequest
* @param session a session for this invocation
* @return invocation parameters for a Skill
*
* @throws IOException
*/
protected ServletInvocationParameters build(final Request request,
Expand All @@ -159,17 +92,18 @@ protected ServletInvocationParameters build(final Request request,
/**
* Same as above using a simple Session and User.
*
* @param request
* a skill request
* @param request a skill request
* @return invocation parameters for a Skill
*
* @throws IOException
*/
protected ServletInvocationParameters build(final Request request)
throws IOException {
return build(request, buildSession());
}

// ------------------------------
// Helper methods for subclasses

/**
* @return a test session.
*/
Expand All @@ -186,4 +120,52 @@ protected Session buildSession(String applicationId) {
.build();
}

/**
* Tuple for useful parameters for a servlet invocation.
*/
protected static final class ServletInvocationParameters {
public final HttpServletRequest request;
public final HttpServletResponse response;
public final ByteArrayOutputStream output;

public ServletInvocationParameters(final HttpServletRequest request,
final HttpServletResponse response, final ByteArrayOutputStream output) {
this.request = request;
this.response = response;
this.output = output;
}
}

/**
* Trivial implementation of the ServletInputStream that wraps an InputStream.
*/
private static class MyServletInputStream extends ServletInputStream {
private final InputStream in;

public MyServletInputStream(final InputStream in) {
this.in = in;
}

@Override
public int read() throws IOException {
return in.read();
}
}

/**
* Trivial implementation of the ServletOutputStream that wraps an OutputStream.
*/
private static class MyServletOutputStream extends ServletOutputStream {
private final OutputStream out;

public MyServletOutputStream(final OutputStream out) {
this.out = out;
}

@Override
public void write(int b) throws IOException {
out.write(b);
}
}

}
Loading

0 comments on commit 9034697

Please sign in to comment.