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