package com.aem.community.core.servlets;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.*;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.json.JSONException;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import javax.jcr.Session;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.json.JSONObject;
import static com.day.cq.wcm.foundation.List.log;
@Component(service= Servlet.class,
property={
Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.paths="+ "/bin/zippingdocx"
})
public class ZippingUtility extends SlingSafeMethodsServlet {
private static final long serialVersionUid = 1L;
@Reference
private ResourceResolverFactory resolverFactory;
private Session session;
private ResourceResolver resolver = null;
@Override
protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp) throws ServletException, IOException {
try {
Map<String, Object> params = new HashMap<>();
params.put(ResourceResolverFactory.SUBSERVICE, "customreporting");
resolver = resolverFactory.getServiceResourceResolver(params);
log.info("user id -------->" + resolver.getUserID());
session = resolver.adaptTo(Session.class);
log.info("Session created");
//Reading the JSON file
StringBuilder stringBuilder = new StringBuilder();
readingJsonFile(stringBuilder);
//converting json string into jsonobject
JSONObject alljsonvalues = new JSONObject(stringBuilder.toString());
//starting the xml creation
try {
Morgan3DR morgan3DR=new Morgan3DR();
//setting the values in morgan 3dr class
morgan3drclassSetter(morgan3DR,alljsonvalues);
//Create JAXB Context
JAXBContext jaxbContext = JAXBContext.newInstance(Morgan3DR.class);
//Create Marshaller
Marshaller jaxbMarshaller = null;
jaxbMarshaller = jaxbContext.createMarshaller();
//Required formatting??
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//Print XML String to Console
StringWriter stringWriter = new StringWriter();
//Write XML to StringWriter
jaxbMarshaller.marshal(morgan3DR, stringWriter);
//Verify XML Content
String xmlContent = stringWriter.toString();
//converting pdf into bytes
File file=new File("C:\\Users\\Amit\\Desktop\\Morgan_3dr\\payslip_jan_capgemini.pdf");
// byte[] bytesOfPdf;
// bytesOfPdf = new byte[(int) file.length()];
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
convertPdftobyte(byteArrayOutputStream);
//now convert the vytesarray into byte and put in zip
//zippping the files
zipthexml(xmlContent,byteArrayOutputStream);
log.info("xml values in string-------->"+xmlContent);
} catch (JAXBException e) {
e.printStackTrace();
}
resp.getWriter().write(" ------stringBuilder.toString()-----" + stringBuilder.toString());
//we got all json as a full string --need to change in jssonobject
} catch (LoginException | JSONException e) {
e.printStackTrace();
}
}
private ByteArrayOutputStream convertPdftobyte(ByteArrayOutputStream byteArrayOutputStream) {
// File file = new File("C:\\Users\\Amit\\Desktop\\Morgan_3dr\\payslip_jan_capgemini.pdf");
try {
FileInputStream fileInputStream =new FileInputStream("C:\\Users\\Amit\\Desktop\\Morgan_3dr\\payslip_jan_capgemini.pdf");
//ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for (int readNum; (readNum = fileInputStream.read(buf)) != -1;) {
byteArrayOutputStream.write(buf, 0, readNum); //no doubt here is 0
//Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
}
fileInputStream.close();
// bytesOfPdf = byteArrayOutputStream.toByteArray();
// byteArrayOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return byteArrayOutputStream;
}
private void zipthexml(String xmlContent, ByteArrayOutputStream bytesOfPdf) {
try {
FileOutputStream fileOutputStream=new FileOutputStream("C:\\Users\\Amit\\Desktop\\Morgan_3dr\\zipfilelocation\\morganzipfile.zip");
ZipOutputStream zipOutputStream= new ZipOutputStream(fileOutputStream);
try {
zipOutputStream.putNextEntry(new ZipEntry("morgan.xml"));
byte[] bytes = xmlContent.getBytes();
zipOutputStream.write(bytes);
zipOutputStream.closeEntry();
zipOutputStream.putNextEntry(new ZipEntry("3dr.txt"));
byte[] bytess = "kuch bhi ho rha hai".getBytes();
zipOutputStream.write(bytess);
zipOutputStream.closeEntry();
zipOutputStream.putNextEntry(new ZipEntry("other.pdf"));
zipOutputStream.write(bytesOfPdf.toByteArray());
zipOutputStream.closeEntry();
zipOutputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private void morgan3drclassSetter(Morgan3DR morgan3DR, JSONObject alljsonvalues) {
try {
morgan3DR.setTitle(alljsonvalues.getString("content/wm3dr/zz-title"));
morgan3DR.setAuthentication_name(alljsonvalues.getString("conten/public/global/authentication_name"));
morgan3DR.setClassification(alljsonvalues.getString("content/wm3dr/content/classification"));
morgan3DR.setLanguague(alljsonvalues.getString("content/template_metadata/Languague"));
morgan3DR.setLdapUserName(alljsonvalues.getString("content/wm3dr/ldap_user_name"));
morgan3DR.setProductId("3DR");
morgan3DR.setPublish_date(alljsonvalues.getString("content/wm3dr/publish_date"));
morgan3DR.setShort_description(alljsonvalues.getString("content/wm3dr/Short_description"));
morgan3DR.setSomething_1(alljsonvalues.getString("content/wm3dr/something_1"));
morgan3DR.setSomething_2(alljsonvalues.getString("content/wm3dr/something_2"));
} catch (JSONException e) {
e.printStackTrace();
}
}
private String readingJsonFile(StringBuilder stringBuilder) {
try {
File file = new File("C:\\Users\\Amit\\Desktop\\Morgan_3dr\\sample_3DR.json");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String linestring = null;
linestring = bufferedReader.readLine();
while (linestring != null) {
stringBuilder.append(linestring);
linestring = bufferedReader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
}//class ends