| 6988 |
rajveer |
1 |
package in.shop2020.serving.controllers;
|
|
|
2 |
|
|
|
3 |
|
| 6991 |
rajveer |
4 |
import in.shop2020.model.v1.order.TransactionService;
|
| 6988 |
rajveer |
5 |
import in.shop2020.model.v1.order.TransactionServiceException;
|
|
|
6 |
import in.shop2020.thrift.clients.TransactionClient;
|
|
|
7 |
|
|
|
8 |
import java.io.ByteArrayOutputStream;
|
|
|
9 |
import java.io.File;
|
|
|
10 |
import java.io.IOException;
|
|
|
11 |
import java.nio.ByteBuffer;
|
|
|
12 |
import java.nio.channels.Channels;
|
|
|
13 |
import java.nio.channels.WritableByteChannel;
|
|
|
14 |
import java.util.ArrayList;
|
|
|
15 |
import java.util.Calendar;
|
|
|
16 |
import java.util.Date;
|
|
|
17 |
import java.util.GregorianCalendar;
|
|
|
18 |
import java.util.HashMap;
|
|
|
19 |
import java.util.List;
|
|
|
20 |
import java.util.Map;
|
|
|
21 |
|
|
|
22 |
import javax.servlet.ServletContext;
|
|
|
23 |
import javax.servlet.ServletOutputStream;
|
|
|
24 |
import javax.servlet.http.HttpServletRequest;
|
|
|
25 |
import javax.servlet.http.HttpServletResponse;
|
|
|
26 |
import javax.servlet.http.HttpSession;
|
|
|
27 |
|
| 6989 |
rajveer |
28 |
import org.apache.struts2.convention.annotation.InterceptorRef;
|
|
|
29 |
import org.apache.struts2.convention.annotation.InterceptorRefs;
|
| 6988 |
rajveer |
30 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
31 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
|
|
32 |
import org.apache.struts2.util.ServletContextAware;
|
|
|
33 |
import org.apache.thrift.TException;
|
|
|
34 |
import org.apache.thrift.transport.TTransportException;
|
|
|
35 |
import org.slf4j.Logger;
|
|
|
36 |
import org.slf4j.LoggerFactory;
|
|
|
37 |
|
|
|
38 |
@SuppressWarnings("serial")
|
|
|
39 |
@InterceptorRefs({
|
|
|
40 |
@InterceptorRef("myDefault"),
|
|
|
41 |
@InterceptorRef("login")
|
|
|
42 |
})
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
*
|
|
|
46 |
* @author Rajveer
|
|
|
47 |
*
|
|
|
48 |
*/
|
|
|
49 |
|
|
|
50 |
public class InvoiceController implements ServletResponseAware,
|
|
|
51 |
ServletRequestAware, ServletContextAware {
|
|
|
52 |
|
|
|
53 |
private static Logger logger = LoggerFactory.getLogger(InvoiceController.class);
|
|
|
54 |
private ServletContext context;
|
|
|
55 |
private HttpServletRequest request;
|
|
|
56 |
private HttpServletResponse response;
|
|
|
57 |
private HttpSession session;
|
|
|
58 |
|
|
|
59 |
private String id;
|
|
|
60 |
private String orderId;
|
|
|
61 |
|
|
|
62 |
private String errorMsg = "";
|
|
|
63 |
|
|
|
64 |
public String show(){
|
|
|
65 |
orderId = id;
|
|
|
66 |
response.setContentType("application/pdf");
|
|
|
67 |
response.setHeader("Content-disposition", "inline; filename=invoice-"+orderId+".pdf" );
|
|
|
68 |
|
| 6991 |
rajveer |
69 |
ByteBuffer buffer = null;
|
|
|
70 |
try {
|
|
|
71 |
if (orderId == null || orderId.isEmpty()) {
|
|
|
72 |
logger.error("Order Id is null or empty");
|
|
|
73 |
return "show";
|
|
|
74 |
}
|
|
|
75 |
TransactionClient tc = new TransactionClient("support_transaction_service_server_host", "transaction_service_server_port");
|
|
|
76 |
TransactionService.Client orderClient = transactionServiceClient.getClient();
|
|
|
77 |
buffer = orderClient.retrieveInvoice(Long.parseLong(orderId));
|
|
|
78 |
if(!buffer.hasArray()) {
|
|
|
79 |
logger.error("The invoice does not found");
|
|
|
80 |
}
|
|
|
81 |
} catch (Exception e) {
|
|
|
82 |
System.out.println(e.getMessage());
|
|
|
83 |
}
|
|
|
84 |
|
| 6988 |
rajveer |
85 |
ServletOutputStream sos;
|
|
|
86 |
try {
|
| 6991 |
rajveer |
87 |
sos = response.getOutputStream();
|
|
|
88 |
sos.write(buffer.array());
|
| 6988 |
rajveer |
89 |
sos.flush();
|
|
|
90 |
} catch (Exception e) {
|
| 6991 |
rajveer |
91 |
System.out.println("Unable to stream the invoice file");
|
| 6988 |
rajveer |
92 |
}
|
|
|
93 |
return "show";
|
| 6991 |
rajveer |
94 |
}
|
| 6988 |
rajveer |
95 |
|
|
|
96 |
@Override
|
|
|
97 |
public void setServletContext(ServletContext context) {
|
|
|
98 |
this.context = context;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
@Override
|
|
|
102 |
public void setServletResponse(HttpServletResponse response) {
|
|
|
103 |
this.response = response;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
@Override
|
|
|
107 |
public void setServletRequest(HttpServletRequest request) {
|
|
|
108 |
this.request = request;
|
|
|
109 |
this.session = request.getSession();
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
public String getId(){
|
|
|
113 |
return id;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
public void setId(String id){
|
|
|
117 |
this.id = id;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
public String getServletContextPath(){
|
|
|
121 |
return context.getContextPath();
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
|
|
|
125 |
public String getErrorMsg() {
|
|
|
126 |
return errorMsg;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
public void setErrorMsg(String errorMsg) {
|
|
|
130 |
this.errorMsg = errorMsg;
|
|
|
131 |
}
|
|
|
132 |
}
|