Rev 1814 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
430 | jmachado | 1 | package pt.estgp.estgweb.web.ajaxapis; |
2 | |||
3 | |||
1827 | jmachado | 4 | import org.apache.log4j.Logger; |
1814 | jmachado | 5 | import pt.estgp.estgweb.utils.Globals; |
430 | jmachado | 6 | import pt.estgp.estgweb.web.UserSessionProxy; |
1827 | jmachado | 7 | //import java.util.Base64; |
430 | jmachado | 8 | |
9 | import javax.servlet.ServletException; |
||
10 | import javax.servlet.http.HttpServlet; |
||
11 | import javax.servlet.http.HttpServletRequest; |
||
12 | import javax.servlet.http.HttpServletResponse; |
||
13 | import java.io.*; |
||
14 | import java.net.MalformedURLException; |
||
15 | |||
16 | /** |
||
17 | * @author Jorge |
||
18 | * @date 24/Mar/2009 |
||
19 | * @time 3:25:40 |
||
20 | */ |
||
21 | public class ServicosImpressao extends HttpServlet |
||
22 | { |
||
23 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(ServicosImpressao.class); |
||
24 | |||
25 | protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException |
||
26 | { |
||
27 | doPost(httpServletRequest, httpServletResponse); |
||
28 | } |
||
29 | |||
30 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, 1.5.0/docs/api/java/io/IOException.html">IOException |
||
31 | { |
||
32 | 1.5.0/docs/api/java/lang/StringBuffer.html">StringBuffer sbResponse = new 1.5.0/docs/api/java/lang/StringBuffer.html">StringBuffer(); |
||
33 | |||
34 | 5+0%2Fdocs%2Fapi+DataOutputStream">DataOutputStream out; |
||
35 | |||
36 | |||
37 | try |
||
38 | { |
||
39 | sbResponse = new 1.5.0/docs/api/java/lang/StringBuffer.html">StringBuffer(); |
||
40 | |||
41 | java.net.1.5.0/docs/api/java/net/URL.html">URL programUrl = new java.net.1.5.0/docs/api/java/net/URL.html">URL(Globals.INTRANET_IMPRESSAO_SERVICO_API + "?username=" + UserSessionProxy.loadUserSession(request,response).getUsername()); |
||
42 | |||
43 | |||
44 | java.net.1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection connection = (java.net.1.5.0/docs/api/java/net/HttpURLConnection.html">HttpURLConnection)programUrl.openConnection(); |
||
45 | if(Globals.INTRANET_IMPRESSAO_SERVICO_AUTH) |
||
46 | { |
||
47 | 1.5.0/docs/api/java/lang/String.html">String user = Globals.INTRANET_IMPRESSAO_SERVICO_AUTH_USER; |
||
48 | 1.5.0/docs/api/java/lang/String.html">String pass = Globals.INTRANET_IMPRESSAO_SERVICO_AUTH_PASS; |
||
1827 | jmachado | 49 | //String encoding = new BASE64Encoder().encode((user + ":" + pass).getBytes()); |
50 | //todo rever no futuro |
||
51 | 1.5.0/docs/api/java/lang/String.html">String encoding = ""; |
||
430 | jmachado | 52 | connection.setRequestProperty ("Authorization", "Basic " + encoding); |
53 | } |
||
54 | |||
55 | (connection).setRequestMethod("POST"); |
||
56 | connection.setDoOutput(true); |
||
57 | connection.setDoInput(true); //Only if you expect to read a response... |
||
58 | connection.setUseCaches(false); //Highly recommended... |
||
59 | connection.setAllowUserInteraction(true); |
||
60 | connection.setFollowRedirects(true); |
||
61 | connection.setInstanceFollowRedirects(true); |
||
62 | connection.setRequestProperty ("Content-Type", |
||
63 | "application/x-www-form-urlencoded"); |
||
64 | |||
65 | // System.out.println(request.getParameter("request")); |
||
66 | // System.out.println(request.getParameter("showmap")); |
||
67 | |||
68 | |||
69 | |||
70 | // out = new DataOutputStream(connection.getOutputStream()); |
||
71 | // String content = "request=" + URLEncoder.encode (request.getParameter("request")) |
||
72 | // + "&showmap=" + URLEncoder.encode (request.getParameter("showmap")); |
||
73 | 1.5.0/docs/api/java/lang/System.html">System.out.println("\n" + "sending form to HTTP server ..."); |
||
74 | // out.writeBytes (content); |
||
75 | // out.flush (); |
||
76 | // out.close (); |
||
77 | |||
78 | |||
79 | // PrintWriter output = new PrintWriter(new OutputStreamWriter(connection.getOutputStream())); |
||
80 | // output.print("request="+ URIUtil.encodeQuery(request.getParameter("request"))); |
||
81 | // output.print("&showmap=" + URIUtil.encodeQuery(request.getParameter("showmap"))); |
||
82 | // //output.flush(); |
||
83 | // output.close(); |
||
84 | |||
85 | |||
86 | 1.5.0/docs/api/java/io/BufferedReader.html">BufferedReader rd = new 1.5.0/docs/api/java/io/BufferedReader.html">BufferedReader(new 1.5.0/docs/api/java/io/InputStreamReader.html">InputStreamReader(connection.getInputStream())); |
||
87 | |||
88 | 1.5.0/docs/api/java/lang/String.html">String line; |
||
89 | |||
90 | while ((line = rd.readLine()) != null) |
||
91 | { |
||
92 | sbResponse.append(line); |
||
93 | } |
||
94 | |||
95 | rd.close(); |
||
96 | |||
97 | } catch (1.5.0/docs/api/java/net/MalformedURLException.html">MalformedURLException e) |
||
98 | { |
||
99 | sbResponse.setLength(0); |
||
100 | sbResponse.append(e.getMessage()); |
||
101 | e.printStackTrace(); |
||
102 | response.sendError(500); |
||
103 | } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) |
||
104 | { |
||
105 | e.printStackTrace(); |
||
106 | sbResponse.setLength(0); |
||
107 | sbResponse.append(e.getMessage()); |
||
108 | response.sendError(500); |
||
109 | } |
||
110 | catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable throwable) |
||
111 | { |
||
112 | logger.error(throwable,throwable); |
||
113 | response.sendError(500); |
||
114 | } |
||
115 | |||
116 | response.setContentType("text/xml"); |
||
117 | 1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter pw = new 1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter(response.getOutputStream()); |
||
118 | pw.write(sbResponse.toString()); |
||
119 | pw.close(); |
||
120 | } |
||
121 | } |