Skip to content

Commit

Permalink
feat: Upgrade To Tomcat 10 - Meeds-io/MIPs#76
Browse files Browse the repository at this point in the history
  • Loading branch information
boubaker committed Aug 29, 2023
1 parent 531405e commit 3959224
Show file tree
Hide file tree
Showing 141 changed files with 1,890 additions and 428 deletions.
28 changes: 3 additions & 25 deletions component/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@
<groupId>org.exoplatform.gatein.pc</groupId>
<artifactId>pc-federation</artifactId>
</dependency>
<dependency>
<groupId>org.exoplatform.gatein.pc</groupId>
<artifactId>pc-bridge</artifactId>
</dependency>
<dependency>
<groupId>org.exoplatform.gatein.pc</groupId>
<artifactId>pc-api</artifactId>
Expand Down Expand Up @@ -149,12 +145,12 @@

<!-- Other Third Party libraries -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>compile</scope>
<!--
Attention: Avoid changing this to scope = provided here for transitive dependency.
javax.servlet is already defined by Web Container (Tomcat Server), thus it will be removed from packaging modules
jakarta.servlet is already defined by Web Container (Tomcat Server), thus it will be removed from packaging modules
-->
</dependency>
<dependency>
Expand Down Expand Up @@ -214,10 +210,6 @@
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
Expand All @@ -226,10 +218,6 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.portals.bridges</groupId>
<artifactId>portals-bridges-common</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
Expand All @@ -256,16 +244,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<exclusions>
<exclusion>
<artifactId>jboss-transaction-api_1.1_spec</artifactId>
<groupId>org.jboss.spec.javax.transaction</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import java.util.Locale;
import java.util.ResourceBundle;

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


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import java.util.Locale;

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

import org.exoplatform.services.organization.User;

Expand Down
6 changes: 5 additions & 1 deletion component/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<name>GateIn Portal Component Common</name>

<properties>
<exo.test.coverage.ratio>0.43</exo.test.coverage.ratio>
<exo.test.coverage.ratio>0.41</exo.test.coverage.ratio>
</properties>

<dependencies>
Expand All @@ -43,6 +43,10 @@
<artifactId>exo.portal.component.test.core</artifactId>
<!-- Test scope not added on purpose for transitive dependency -->
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-fileupload2-jakarta</artifactId>
</dependency>

<dependency>
<groupId>org.exoplatform.kernel</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.List;

import javax.portlet.PortletContext;
import javax.servlet.ServletContext;
import jakarta.servlet.ServletContext;

import org.exoplatform.container.PortalContainer;
import org.exoplatform.services.log.ExoLogger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import jakarta.servlet.ServletContext;

import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.HashMap;
import java.util.Map;

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

import org.exoplatform.services.html.refs.RefsDecoder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
Expand All @@ -32,21 +32,20 @@
import java.util.Set;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload2.core.DiskFileItem;
import org.apache.commons.fileupload2.core.DiskFileItemFactory;
import org.apache.commons.fileupload2.core.FileUploadException;
import org.apache.commons.fileupload2.core.ProgressListener;
import org.apache.commons.fileupload2.jakarta.JakartaServletDiskFileUpload;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.container.xml.PortalContainerInfo;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;

public class UploadService {
/** . */
private static final Log log = ExoLogger.getLogger(UploadService.class);
Expand All @@ -70,7 +69,7 @@ public class UploadService {

private Map<String, UploadLimit> uploadLimits = new LinkedHashMap<String, UploadLimit>();

public static String UPLOAD_RESOURCES_STACK = "uploadResourcesStack";
public static final String UPLOAD_RESOURCES_STACK = "uploadResourcesStack";

public static enum UploadUnit {
KB, MB, GB
Expand Down Expand Up @@ -107,6 +106,13 @@ public void createUploadResource(String uploadId, HttpServletRequest request) th
if(uploadId == null || !UPLOAD_ID_PATTERN.matcher(uploadId).matches()) {
throw new FileUploadException("Upload id " + uploadId + " is not valid, it cannot be null or contain '.' , '/' or '\\'");
}
try {
if (request.getParts() == null || request.getParts().size() != 1) {
throw new FileUploadException("You can upload 1 file per request");
}
} catch (Exception e) {
throw new FileUploadException("Error processing request parts");
}

UploadResource upResource = new UploadResource(uploadId);
upResource.setFileName("");// Avoid NPE in UploadHandler
Expand All @@ -115,62 +121,28 @@ public void createUploadResource(String uploadId, HttpServletRequest request) th
putToStackInSession(request.getSession(true), uploadId);

double contentLength = request.getContentLength();
upResource.setEstimatedSize(contentLength);
if (isLimited(upResource, contentLength)) {
upResource.setStatus(UploadResource.FAILED_STATUS);
return;
}

ServletFileUpload servletFileUpload = makeServletFileUpload(upResource);
// parse request
List<FileItem> itemList = null;
try {
itemList = servletFileUpload.parseRequest(request);
} catch (FileUploadException uploadEx) {
if (uploadEx instanceof FileUploadBase.IOFileUploadException) {
log.debug("IOException while upload resource", uploadEx);
} else {
throw uploadEx;
}
}
String storeLocation = uploadLocation_ + "/" + uploadId;
JakartaServletDiskFileUpload servletFileUpload = makeServletFileUpload(upResource, storeLocation);
List<DiskFileItem> itemList = servletFileUpload.parseRequest(request);
DiskFileItem fileItem = itemList.get(0);

if (itemList == null || itemList.size() != 1 || itemList.get(0).isFormField()) {
log.debug("Please upload 1 file per request");
removeUploadResource(uploadId);
return;
}

DiskFileItem fileItem = (DiskFileItem) itemList.get(0);
String fileName = fileItem.getName();
if (fileName == null)
fileName = uploadId;
fileName = fileName.substring(fileName.lastIndexOf('\\') + 1);
fileName = this.correctFileName(fileName);
String storeLocation = uploadLocation_ + "/" + uploadId + "." + fileName;

// commons-fileupload will store the temp file with name *.tmp
// we need to rename it to our desired name
if (fileItem.getStoreLocation() != null) {
fileItem.getStoreLocation().renameTo(new File(storeLocation));
}
File fileStore = new File(storeLocation);
if (!fileStore.exists())
try {
fileStore.createNewFile();
} catch (IOException e) {
throw new RuntimeException(e);
}

upResource.setFileName(fileName);
upResource.setMimeType(fileItem.getContentType());
if (plugins != null)
for (MimeTypeUploadPlugin plugin : plugins) {
String mimeType = plugin.getMimeType(fileName);
if (mimeType != null)
upResource.setMimeType(mimeType);
}
upResource.setStoreLocation(storeLocation);
upResource.setFileName(fileItem.getName());
upResource.setStoreLocation(fileItem.getPath().toFile().getAbsolutePath());
upResource.setEstimatedSize(fileItem.getSize());
upResource.setStatus(UploadResource.UPLOADED_STATUS);
upResource.setMimeType(fileItem.getContentType());
if (plugins != null) {
for (MimeTypeUploadPlugin plugin : plugins) {
String mimeType = plugin.getMimeType(fileItem.getName());
if (mimeType != null)
upResource.setMimeType(mimeType);
}
}
}

/**
Expand Down Expand Up @@ -224,7 +196,7 @@ public void createUploadResource(String uploadId, String encoding, String conten
private void putToStackInSession(HttpSession session, String uploadId) {
Set<String> uploadResouceIds = (Set<String>) session.getAttribute(UploadService.UPLOAD_RESOURCES_STACK);
if (uploadResouceIds == null) {
uploadResouceIds = new HashSet();
uploadResouceIds = new HashSet<>();
}
uploadResouceIds.add(uploadId);
session.setAttribute(UploadService.UPLOAD_RESOURCES_STACK, uploadResouceIds);
Expand All @@ -247,6 +219,7 @@ public UploadResource getUploadResource(String uploadId) {
*/
public void cleanUp(HttpSession session) {
log.debug("Cleaning up uploaded files for temporariness");
@SuppressWarnings("unchecked")
Set<String> uploadIds = (Set<String>) session.getAttribute(UploadService.UPLOAD_RESOURCES_STACK);
if (uploadIds != null) {
for (String id : uploadIds) {
Expand Down Expand Up @@ -334,29 +307,23 @@ public String correctFileName(String fileName) {
return new String(chars);
}

private ServletFileUpload makeServletFileUpload(final UploadResource upResource) {
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();

// Set factory constraints
factory.setSizeThreshold(0);
File uploadDir = new File(uploadLocation_);
if (!uploadDir.exists())
uploadDir.mkdirs();
factory.setRepository(uploadDir);

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
ProgressListener listener = new ProgressListener() {
public void update(long pBytesRead, long pContentLength, int pItems) {
if (pBytesRead == upResource.getUploadedSize())
return;
upResource.addUploadedBytes(pBytesRead - upResource.getUploadedSize());
}
};
upload.setProgressListener(listener);
return upload;
private JakartaServletDiskFileUpload makeServletFileUpload(final UploadResource upResource, String storeLocation) {
// Create a factory for disk-based file items
DiskFileItemFactory factory = DiskFileItemFactory.builder()
.setBufferSize(0)
.setFile(storeLocation)
.setCharset(StandardCharsets.UTF_8)
.get();
// Create a new file upload handler
JakartaServletDiskFileUpload upload = new JakartaServletDiskFileUpload(factory);
upload.setHeaderCharset(StandardCharsets.UTF_8);
ProgressListener listener = (bytesRead, contentLength, items) -> {
if (bytesRead != upResource.getUploadedSize()) {
upResource.addUploadedBytes(bytesRead - upResource.getUploadedSize());
}
};
upload.setProgressListener(listener);
return upload;
}

public boolean isLimited(UploadResource upResource, double contentLength) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.exoplatform.upload;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.HttpSessionEvent;

import org.exoplatform.container.PortalContainer;
import org.exoplatform.services.listener.Event;
Expand All @@ -22,7 +22,7 @@ public void onEvent(Event<PortalContainer, HttpSessionEvent> event) throws Excep
PortalContainer container = event.getSource();
HttpSession session = event.getData().getSession();

UploadService uploadService = (UploadService) container.getComponentInstanceOfType(UploadService.class);
UploadService uploadService = container.getComponentInstanceOfType(UploadService.class);
uploadService.cleanUp(session);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@
import java.io.File;
import java.io.IOException;

import org.apache.commons.fileupload.FileUploadException;
import org.exoplatform.component.test.AbstractKernelTest;
import org.exoplatform.component.test.ConfigurationUnit;
import org.exoplatform.component.test.ConfiguredBy;
import org.exoplatform.component.test.ContainerScope;

import org.apache.commons.fileupload2.core.FileUploadException;
import org.junit.Test;
import org.mockito.Mockito;

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

@ConfiguredBy({
@ConfigurationUnit(scope = ContainerScope.PORTAL, path = "conf/services/upload-service.xml")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.exoplatform.services.log.Log;
import org.exoplatform.services.log.ExoLogger;
import org.gatein.pc.api.PortletInvoker;
import org.gatein.pc.bridge.BridgeInterceptor;
import org.gatein.pc.federation.FederatingPortletInvoker;
import org.gatein.pc.federation.impl.FederatingPortletInvokerService;
import org.gatein.pc.portlet.PortletInvokerInterceptor;
Expand Down Expand Up @@ -117,10 +116,8 @@ public void start() {
requestAttributeConversationInterceptor.setNext(eventPayloadInterceptor);
CCPPInterceptor ccppInterceptor = new CCPPInterceptor();
ccppInterceptor.setNext(requestAttributeConversationInterceptor);
BridgeInterceptor bridgepInterceptor = new BridgeInterceptor();
bridgepInterceptor.setNext(ccppInterceptor);
ProducerCacheInterceptor producerCacheInterceptor = new ProducerCacheInterceptor();
producerCacheInterceptor.setNext(bridgepInterceptor);
producerCacheInterceptor.setNext(ccppInterceptor);
SessionInvalidatorInterceptor sessionInvalidatorInterceptor = new SessionInvalidatorInterceptor();
sessionInvalidatorInterceptor.setNext(producerCacheInterceptor);
ContextDispatcherInterceptor contextDispatcherInterceptor = new ContextDispatcherInterceptor();
Expand Down
Loading

0 comments on commit 3959224

Please sign in to comment.