PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/memedb/src/java/memedb/document/CGIPhpDocument.java

http://caffeine-hx.googlecode.com/
Java | 106 lines | 61 code | 16 blank | 29 comment | 1 complexity | af47159b6c88f3c3cdda694f6bb0e2e3 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. /*
  2. * Copyright 2008 The MemeDB Contributors (see CONTRIBUTORS)
  3. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  4. * use this file except in compliance with the License. You may obtain a copy of
  5. * the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. * License for the specific language governing permissions and limitations under
  13. * the License.
  14. */
  15. package memedb.document;
  16. import java.io.BufferedReader;
  17. import java.io.BufferedWriter;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.io.OutputStream;
  22. import java.io.OutputStreamWriter;
  23. import java.security.MessageDigest;
  24. import java.security.NoSuchAlgorithmException;
  25. import java.util.Map;
  26. import javax.servlet.http.HttpServletRequest;
  27. import javax.servlet.http.HttpServletResponse;
  28. @ContentTypes({
  29. "application/php"
  30. })
  31. /**
  32. * CGIPhpDocument generates results by running a PHP process
  33. * @author Russell Weir
  34. */
  35. public class CGIPhpDocument extends CGIScriptDocument {
  36. protected ProcessBuilder pb;
  37. @Override
  38. public void sendBody(OutputStream dataOutput, HttpServletRequest request, HttpServletResponse response) throws IOException {
  39. try {
  40. pb = new ProcessBuilder(new String[]{"/bin/sh", "-c",
  41. "/bin/echo \""+content.replace("\"","\\\"")+"\" | /usr/bin/php"});
  42. log.warn("{}", pb.command());
  43. } catch( Exception e ) {
  44. log.error("{}", e.getMessage());
  45. }
  46. try {
  47. // pb.directory(new File("myDir"));
  48. // pb.directory(null);
  49. createEnvironment(pb, request);
  50. } catch( Exception e ) {
  51. log.error("{}", e.getMessage());
  52. }
  53. try {
  54. runProcess(pb, dataOutput);
  55. } catch( Exception e ) {
  56. log.error("{}", e.getMessage());
  57. }
  58. }
  59. @Override
  60. protected void runProcess(ProcessBuilder pb, OutputStream dataOutput) throws IOException {
  61. pb.redirectErrorStream(true);
  62. // try
  63. Process proc = pb.start();
  64. OutputStreamWriter stdout = new OutputStreamWriter(proc.getOutputStream());
  65. BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
  66. BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(dataOutput));
  67. stdout.write(content);
  68. int c;
  69. while((c = reader.read()) != -1) {
  70. writer.write(c);
  71. }
  72. writer.flush();
  73. try {
  74. proc.waitFor();
  75. } catch (InterruptedException e) {
  76. }
  77. /*
  78. InputStream is = p.getInputStream();
  79. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  80. String line;
  81. while ((line = br.readLine()) != null) {
  82. System.out.println(line);
  83. }
  84. */
  85. }
  86. @Override
  87. public String getBrowserContentType() {
  88. return "text/html";
  89. }
  90. }