Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Showcases/GroupDocs.Conversion-for-java-using-servlets/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/target/
/.idea/
*.iml

.DS_Store
Thumbs.db

*.lic
42 changes: 42 additions & 0 deletions Showcases/GroupDocs.Conversion-for-java-using-servlets/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>conversion-servlets</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="**/bower_components/*|**/node_modules/*|**/*.min.js" kind="src" path="src/main/webapp"/>
<classpathentry kind="src" path="target/m2e-wtp/web-resources"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
<attributes>
<attribute name="hide" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
<classpathentry kind="output" path=""/>
</classpath>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.7
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="conversion-servlets">
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<property name="context-root" value="conversion-servlets"/>
<property name="java-output-path" value="/conversion-servlets/target/classes"/>
</wb-module>
</project-modules>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<root>
<facet id="jst.jsf">
<node name="libprov">
<attribute name="provider-id" value="jsf-no-op-library-provider"/>
</node>
</facet>
<facet id="jst.jaxrs">
<node name="libprov">
<attribute name="provider-id" value="jaxrs-no-op-library-provider"/>
</node>
</facet>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<fixed facet="wst.jsdt.web"/>
<installed facet="java" version="1.7"/>
<installed facet="jst.web" version="3.1"/>
<installed facet="jst.jsf" version="2.2"/>
<installed facet="jst.jaxrs" version="2.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Window
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
disabled=06target
eclipse.preferences.version=1
53 changes: 53 additions & 0 deletions Showcases/GroupDocs.Conversion-for-java-using-servlets/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.groupdocs.ui</groupId>
<artifactId>conversion-servlets</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>7</maven.compiler.source>
<maven.compiler.target>7</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-conversion</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.11.v20150529</version>
<configuration>
<httpConnector>
<!--host>localhost</host-->
<port>8080</port>
</httpConnector>
</configuration>
</plugin>
</plugins>
</build>

</project>

Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package com.groupdocs.ui;

import com.groupdocs.conversion.License;
import com.groupdocs.conversion.config.ConversionConfig;
import com.groupdocs.conversion.converter.option.CellsSaveOptions;
import com.groupdocs.conversion.converter.option.HtmlSaveOptions;
import com.groupdocs.conversion.converter.option.ImageSaveOptions;
import com.groupdocs.conversion.converter.option.LoadOptions;
import com.groupdocs.conversion.converter.option.PdfSaveOptions;
import com.groupdocs.conversion.converter.option.SaveOptions;
import com.groupdocs.conversion.converter.option.SlidesSaveOptions;
import com.groupdocs.conversion.converter.option.WordsSaveOptions;
import com.groupdocs.conversion.domain.ConversionType;
import com.groupdocs.conversion.handler.ConversionHandler;
import com.groupdocs.foundation.utils.wrapper.stream.GroupDocsInputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@WebServlet("/convert")
public class Conversion extends HttpServlet {

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// It is assumed that we are redirected here by Upload servlet

//Initializing Configuration
try{

//Applying License
License license = new License();
if (Files.isReadable(FileSystems.getDefault().getPath(getProjectProperty("license.path")))) {
license.setLicense(getProjectProperty("license.path"));
if (!License.isValidLicense()) {
throw new RuntimeException("License is not valid");
}
}

// We already know paths of source conversion-type
Path source = (Path) request.getSession().getAttribute("source");

// Setup Conversion configuration
ConversionConfig conversionConfig = new ConversionConfig();
conversionConfig.setStoragePath(source.getParent().toString());

//Getting conversion type
int conversionType = (int) request.getSession().getAttribute("conversion-type");

// Guess the extension of result file and set savOptions according to extention
SaveOptions saveOptions = new ImageSaveOptions();
String ext = "";
switch (conversionType) {
case ConversionType.WORDS:
ext = "docx";
saveOptions = new WordsSaveOptions();
break;
case ConversionType.CELLS:
ext = "xlsx";
saveOptions = new CellsSaveOptions();
break;
case ConversionType.PDF:
ext = "pdf";
saveOptions = new PdfSaveOptions();
break;
case ConversionType.SLIDES:
ext = "pptx";
saveOptions = new SlidesSaveOptions();
break;
case ConversionType.HTML:
ext = "html";
saveOptions = new HtmlSaveOptions();
break;
case ConversionType.IMAGE:
ext = "jpeg";
break;
}

//Inititalizing the Conversion Handler
ConversionHandler conversionHandler = new ConversionHandler(conversionConfig);
LoadOptions loadOptions = null;

//Checking output extension
if(ext == "jpeg"){
//Converting Document to Image
List<GroupDocsInputStream> converted = conversionHandler
.<List<GroupDocsInputStream>> convert(source.getFileName().toString(), new ImageSaveOptions());

List<Path> Pages = new ArrayList<Path>();
int i = 0;
for(GroupDocsInputStream SinglePage: converted){
//Creating temporary file to save Converted File data
Path Page = Files.createTempFile(Integer.toString(i) + "groupdocs-conversion-result-", "." + ext);

//Copying converted file data into temporary file
Files.copy(SinglePage.toInputStream(), Page, StandardCopyOption.REPLACE_EXISTING);

Pages.add(Page);
i++;
}
Path Zip = Files.createTempFile("groupdocs-conversion-result-", ".zip");
addImagesToZip(Pages, Zip);
request.getSession().setAttribute("result", Zip);

}
else{
//Converting Document
GroupDocsInputStream converted = conversionHandler.convert(source.getFileName().toString(), saveOptions);

//Creating temporary file to save Converted File data
Path result = Files.createTempFile("groupdocs-conversion-result-", "." + ext);

//Copying converted file data into temporary file
Files.copy(converted.toInputStream(), result, StandardCopyOption.REPLACE_EXISTING);


request.getSession().setAttribute("result", result);
}

}
catch (Exception exp) {
System.out.println("Exception: " + exp.getMessage());
exp.printStackTrace();
}

response.sendRedirect("download");

}

//Enclosing all pages in a zip file (While converting to Images only)
public void addImagesToZip(List<Path> Pages, Path ZipFile){
byte[] buffer = new byte[2048];
try{
FileOutputStream fileOutputStream = new FileOutputStream(ZipFile.toFile());
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
for(Path Page : Pages){
ZipEntry zipEntry= new ZipEntry(Page.getFileName().toString());
zipOutputStream.putNextEntry(zipEntry);
FileInputStream fileInputStream = new FileInputStream(Page.toFile());
int length;
while ((length = fileInputStream.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, length);
}

fileInputStream.close();
}
zipOutputStream.closeEntry();
//remember close it
zipOutputStream.close();
}
catch(IOException ex){
ex.printStackTrace();
}
}

//Getting project properties
public static String getProjectProperty(String name) {
Properties properties = new Properties();
try (InputStream inputStream = Conversion.class.getResourceAsStream("/project.properties")) {
properties.load(inputStream);
} catch (IOException e) {
// Ignore
}
return properties.getProperty(name);
}
}

Loading