public void write(
      String citationFilename,
      List<String> variableNameSet,
      String subsetUNF,
      String subsettingCriteria) {
    OutputStream outs = null;
    if (subsetUNF == null) {
      subsetUNF = "";
    }
    if (subsettingCriteria == null) {
      subsettingCriteria = "";
    }
    try {
      File cf = new File(citationFilename);
      outs = new BufferedOutputStream(new FileOutputStream(cf));
      PrintWriter pw = new PrintWriter(new OutputStreamWriter(outs, "utf8"), true);
      pw.println(title);
      pw.println(offlineCitation);

      if (generateSubsetCriteriaLine().equals("")) {
        pw.println("\n\n");
      } else {
        pw.println("\n");
        pw.println(generateSubsetCriteriaLine() + "\n");
      }
      pw.println(subsetTitle);
      pw.print(offlineCitation + " ");
      pw.print(DvnDSButil.joinNelementsPerLine(variableNameSet, 5));
      pw.println(" [VarGrp/@var(DDI)];");
      pw.println(subsetUNF);
      outs.close();
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }
  public void write(File cf) {

    OutputStream outs = null;
    try {
      outs = new BufferedOutputStream(new FileOutputStream(cf));
      PrintWriter pw = new PrintWriter(new OutputStreamWriter(outs, "utf8"), true);
      pw.println(title);
      pw.println(offlineCitation);

      if (generateSubsetCriteriaLine().equals("")) {
        pw.println("\n\n");
      } else {
        pw.println("\n");
        pw.println(generateSubsetCriteriaLine() + "\n");
      }
      pw.println(subsetTitle);
      pw.print(offlineCitation + " ");
      pw.print(variableList);
      pw.println(" [VarGrp/@var(DDI)];");
      pw.println(subsetUNF);
      outs.close();
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }
  public void writeWholeFileCase(File cf) {
    OutputStream outs = null;
    try {
      outs = new BufferedOutputStream(new FileOutputStream(cf));
      PrintWriter pw = new PrintWriter(new OutputStreamWriter(outs, "utf8"), true);
      pw.println(title);
      pw.println(offlineCitation);

      outs.close();
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }
Example #4
0
 public static int pipe_stream(InputStream is, OutputStream os, boolean wantsKeepOpen)
     throws IOException { // U: copia de un stream al otro
   int cnt = 0;
   int n;
   byte[] buffer = new byte[BUFF_SZ];
   while ((n = is.read(buffer)) > -1) {
     cnt += n;
     os.write(buffer, 0, n);
   }
   if (!wantsKeepOpen) {
     is.close();
     os.close();
   }
   return cnt;
 }
Example #5
0
  public void save(String gamePath, String dataName) {
    XMLExporter ex = XMLExporter.getInstance();
    OutputStream os = null;
    try {

      File daveFolder = new File(System.getProperty("user.dir") + File.separator + gamePath);
      if (!daveFolder.exists() && !daveFolder.mkdirs()) {
        Logger.getLogger(Type.class.getName()).log(Level.SEVERE, "Error creating save file!");
        throw new IllegalStateException("SaveGame dataset cannot be created");
      }
      File saveFile = new File(daveFolder.getAbsolutePath() + File.separator + dataName);
      if (!saveFile.exists()) {
        if (!saveFile.createNewFile()) {
          Logger.getLogger(Type.class.getName()).log(Level.SEVERE, "Error creating save file!");
          throw new IllegalStateException("SaveGame dataset cannot be created");
        }
      }
      os = new BufferedOutputStream(new FileOutputStream(saveFile));
      // os = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(saveFile)));
      ex.save(this, os);
    } catch (IOException ex1) {
      Logger.getLogger(Type.class.getName()).log(Level.SEVERE, "Error saving data: {0}", ex1);
      ex1.printStackTrace();
      throw new IllegalStateException("SaveGame dataset cannot be saved");
    } finally {
      try {
        if (os != null) {
          os.close();
        }
      } catch (IOException ex1) {
        Logger.getLogger(Type.class.getName()).log(Level.SEVERE, "Error saving data: {0}", ex1);
        ex1.printStackTrace();
        throw new IllegalStateException("SaveGame dataset cannot be saved");
      }
    }
  }
  public void doProcess(HttpServletRequest req, HttpServletResponse res, boolean isPost) {
    StringBuffer bodyContent = null;
    OutputStream out = null;
    PrintWriter writer = null;
    String serviceKey = null;

    try {
      BufferedReader in = req.getReader();
      String line = null;
      while ((line = in.readLine()) != null) {
        if (bodyContent == null) bodyContent = new StringBuffer();
        bodyContent.append(line);
      }
    } catch (Exception e) {
    }
    try {
      if (requireSession) {
        // check to see if there was a session created for this request
        // if not assume it was from another domain and blow up
        // Wrap this to prevent Portlet exeptions
        HttpSession session = req.getSession(false);
        if (session == null) {
          res.setStatus(HttpServletResponse.SC_FORBIDDEN);
          return;
        }
      }
      serviceKey = req.getParameter("id");
      // only to preven regressions - Remove before 1.0
      if (serviceKey == null) serviceKey = req.getParameter("key");
      // check if the services have been loaded or if they need to be reloaded
      if (services == null || configUpdated()) {
        getServices(res);
      }
      String urlString = null;
      String xslURLString = null;
      String userName = null;
      String password = null;
      String format = "json";
      String callback = req.getParameter("callback");
      String urlParams = req.getParameter("urlparams");
      String countString = req.getParameter("count");
      // encode the url to prevent spaces from being passed along
      if (urlParams != null) {
        urlParams = urlParams.replace(' ', '+');
      }

      try {
        if (services.has(serviceKey)) {
          JSONObject service = services.getJSONObject(serviceKey);
          // default to the service default if no url parameters are specified
          if (urlParams == null && service.has("defaultURLParams")) {
            urlParams = service.getString("defaultURLParams");
          }
          String serviceURL = service.getString("url");
          // build the URL
          if (urlParams != null && serviceURL.indexOf("?") == -1) {
            serviceURL += "?";
          } else if (urlParams != null) {
            serviceURL += "&";
          }
          String apikey = "";
          if (service.has("username")) userName = service.getString("username");
          if (service.has("password")) password = service.getString("password");
          if (service.has("apikey")) apikey = service.getString("apikey");
          urlString = serviceURL + apikey;
          if (urlParams != null) urlString += "&" + urlParams;
          if (service.has("xslStyleSheet")) {
            xslURLString = service.getString("xslStyleSheet");
          }
        }
        // code for passing the url directly through instead of using configuration file
        else if (req.getParameter("url") != null) {
          String serviceURL = req.getParameter("url");
          // build the URL
          if (urlParams != null && serviceURL.indexOf("?") == -1) {
            serviceURL += "?";
          } else if (urlParams != null) {
            serviceURL += "&";
          }
          urlString = serviceURL;
          if (urlParams != null) urlString += urlParams;
        } else {
          writer = res.getWriter();
          if (serviceKey == null)
            writer.write("XmlHttpProxyServlet Error: id parameter specifying serivce required.");
          else
            writer.write(
                "XmlHttpProxyServlet Error : service for id '" + serviceKey + "' not  found.");
          writer.flush();
          return;
        }
      } catch (Exception ex) {
        getLogger().severe("XmlHttpProxyServlet Error loading service: " + ex);
      }

      Map paramsMap = new HashMap();
      paramsMap.put("format", format);
      // do not allow for xdomain unless the context level setting is enabled.
      if (callback != null && allowXDomain) {
        paramsMap.put("callback", callback);
      }
      if (countString != null) {
        paramsMap.put("count", countString);
      }

      InputStream xslInputStream = null;

      if (urlString == null) {
        writer = res.getWriter();
        writer.write(
            "XmlHttpProxyServlet parameters:  id[Required] urlparams[Optional] format[Optional] callback[Optional]");
        writer.flush();
        return;
      }
      // default to JSON
      res.setContentType(responseContentType);
      out = res.getOutputStream();
      // get the stream for the xsl stylesheet
      if (xslURLString != null) {
        // check the web root for the resource
        URL xslURL = null;
        xslURL = ctx.getResource(resourcesDir + "xsl/" + xslURLString);
        // if not in the web root check the classpath
        if (xslURL == null) {
          xslURL =
              XmlHttpProxyServlet.class.getResource(classpathResourcesDir + "xsl/" + xslURLString);
        }
        if (xslURL != null) {
          xslInputStream = xslURL.openStream();
        } else {
          String message =
              "Could not locate the XSL stylesheet provided for service id "
                  + serviceKey
                  + ". Please check the XMLHttpProxy configuration.";
          getLogger().severe(message);
          try {
            out.write(message.getBytes());
            out.flush();
            return;
          } catch (java.io.IOException iox) {
          }
        }
      }
      if (!isPost) {
        xhp.doGet(urlString, out, xslInputStream, paramsMap, userName, password);
      } else {
        if (bodyContent == null)
          getLogger()
              .info(
                  "XmlHttpProxyServlet attempting to post to url "
                      + urlString
                      + " with no body content");
        xhp.doPost(
            urlString,
            out,
            xslInputStream,
            paramsMap,
            bodyContent.toString(),
            req.getContentType(),
            userName,
            password);
      }
    } catch (Exception iox) {
      iox.printStackTrace();
      getLogger().severe("XmlHttpProxyServlet: caught " + iox);
      try {
        writer = res.getWriter();
        writer.write(iox.toString());
        writer.flush();
      } catch (java.io.IOException ix) {
        ix.printStackTrace();
      }
      return;
    } finally {
      try {
        if (out != null) out.close();
        if (writer != null) writer.close();
      } catch (java.io.IOException iox) {
      }
    }
  }
Example #7
0
 public static void set_stream(OutputStream os, String data, String encoding) throws IOException {
   os.write(data.getBytes(encoding));
   os.close();
 }