@Override
 public void flush() throws IOException {
   if (tempMemoryBuffer != null) {
     tempMemoryBuffer.flush();
   } else {
     bufOut.flush();
     fout.flush();
   }
 }
Esempio n. 2
0
  /** Perform the transformation to always return a String object */
  protected Object doTransform(Object src, String encoding) throws TransformerException {
    try {
      HttpResponse response = (HttpResponse) src;
      ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
      OutputStream outstream = bos;
      ResponseWriter writer = new ResponseWriter(outstream, encoding);
      writer.println(response.getStatusLine());
      Iterator item = response.getHeaderIterator();
      while (item.hasNext()) {
        Header header = (Header) item.next();
        writer.print(header.toExternalForm());
      }
      writer.println();
      writer.flush();

      InputStream content = response.getBody();
      if (content != null) {
        Header transferenc = response.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING);
        if (transferenc != null) {
          response.removeHeaders(HttpConstants.HEADER_CONTENT_LENGTH);
          if (transferenc.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1) {
            outstream = new ChunkedOutputStream(outstream);
          }
        }

        IOUtils.copy(content, outstream);

        if (outstream instanceof ChunkedOutputStream) {
          ((ChunkedOutputStream) outstream).finish();
        }
      }

      outstream.flush();
      bos.flush();
      byte[] result = bos.toByteArray();
      outstream.close();
      writer.close();
      bos.close();

      String output = null;
      try {
        output = new String(result, encoding);
      } catch (UnsupportedEncodingException uee) {
        // I believe this is never reached since a TransformerExcpetion
        // is thrown before at new ResponseWriter(outstream, encoding) if
        // encoding is not supported
        output = new String(result);
      }

      return output;
    } catch (IOException e) {
      throw new TransformerException(this, e);
    }
  }
Esempio n. 3
0
 protected String read(InputStream resource) throws IOException {
   int i;
   byte[] buffer = new byte[65565];
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   while ((i = resource.read(buffer, 0, buffer.length)) != -1) {
     out.write(buffer, 0, i);
   }
   out.flush();
   out.close();
   return new String(out.toByteArray(), "UTF-8");
 }
  public static ForensicReportBase64 getBase64(String urlHash, String mongoHostIP)
      throws UnknownHostException {
    System.out.println("Create base64 for hash " + urlHash);
    MongoClient mongoclient = new MongoClient(mongoHostIP, 27017);
    Morphia morphia = new Morphia();
    morphia.map(ForensicReport.class).map(dqReport.class);
    Datastore ds = new Morphia().createDatastore(mongoclient, "ForensicDatabase");
    ds.ensureCaps();
    ForensicReportBase64 reportBase64 = new ForensicReportBase64();
    ForensicReport report = ds.get(ForensicReport.class, urlHash);

    if (report != null) {
      if (report.displayImage != null) {
        byte[] displayImageInByte;
        BufferedImage displayImage;
        try {
          displayImage = ImageIO.read(new File(report.displayImage));
          ByteArrayOutputStream displayImagebuffer = new ByteArrayOutputStream();
          ImageIO.write(displayImage, "png", displayImagebuffer);
          displayImagebuffer.flush();
          displayImageInByte = displayImagebuffer.toByteArray();
          displayImagebuffer.close();
          ScalingReport dataScale = new ScalingReport();
          try {
            dataScale = scale(displayImageInByte, 500, 500);
            String displayImageBase64String =
                Base64.getEncoder().encodeToString(dataScale.scaledByte);
            reportBase64.displayImageBase64 = "data:image/jpeg;base64," + displayImageBase64String;
            reportBase64.widthdisplayImage = dataScale.width;
          } catch (ApplicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("Scalling exception");
            String displayImageBase64String =
                Base64.getEncoder().encodeToString(displayImageInByte);
            reportBase64.displayImageBase64 = "data:image/jpeg;base64," + displayImageBase64String;
            reportBase64.widthdisplayImage = displayImage.getWidth();
          }
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          System.out.println("Display image exception");
        }
      }
      if (report.thumbnailReport.numberOfThumbnails > 0) {
        for (int ThumbInd = 0; ThumbInd < report.thumbnailReport.thumbnailList.size(); ThumbInd++) {
          try {
            byte[] thumbInByte;
            BufferedImage thumbImage;
            thumbImage = ImageIO.read(new File(report.thumbnailReport.thumbnailList.get(ThumbInd)));
            ByteArrayOutputStream thumbbuffer = new ByteArrayOutputStream();
            ImageIO.write(thumbImage, "png", thumbbuffer);
            thumbbuffer.flush();
            thumbInByte = thumbbuffer.toByteArray();
            thumbbuffer.close();
            ScalingReport thumbScale = new ScalingReport();
            try {
              thumbScale = scale(thumbInByte, 500, 500);
              String thumbBase64String = Base64.getEncoder().encodeToString(thumbScale.scaledByte);
              reportBase64.thumbBase64.add("data:image/jpeg;base64," + thumbBase64String);
              reportBase64.widththumb.add(thumbScale.width);
            } catch (ApplicationException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
              System.out.println("Scalling exception");
              String thumbBase64String = Base64.getEncoder().encodeToString(thumbInByte);
              reportBase64.thumbBase64.add("data:image/jpeg;base64," + thumbBase64String);
              reportBase64.widththumb.add(thumbImage.getWidth());
            }
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        }
      }
      if (report.dqReport.completed) {
        byte[] dqInByte;
        BufferedImage dqImage;
        try {
          dqImage = ImageIO.read(new File(report.dqReport.map));
          ByteArrayOutputStream dqbuffer = new ByteArrayOutputStream();
          ImageIO.write(dqImage, "png", dqbuffer);
          dqbuffer.flush();
          dqInByte = dqbuffer.toByteArray();
          dqbuffer.close();
          ScalingReport dqScale = new ScalingReport();
          try {
            dqScale = scale(dqInByte, 130, 130);
            String dqBase64String = Base64.getEncoder().encodeToString(dqScale.scaledByte);
            reportBase64.dqBase64 = "data:image/jpeg;base64," + dqBase64String;
            reportBase64.widthdq = dqScale.width;
          } catch (ApplicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("Scalling exception");
            String dqBase64String = Base64.getEncoder().encodeToString(dqInByte);
            reportBase64.dqBase64 = "data:image/jpeg;base64," + dqBase64String;
            reportBase64.widthdq = dqImage.getWidth();
          }
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }

      if (report.dwNoiseReport.completed) {
        byte[] dwNoiseInByte;
        BufferedImage dwNoiseImage;
        try {
          dwNoiseImage = ImageIO.read(new File(report.dwNoiseReport.map));
          ByteArrayOutputStream dwNoisebuffer = new ByteArrayOutputStream();
          ImageIO.write(dwNoiseImage, "png", dwNoisebuffer);
          dwNoisebuffer.flush();
          dwNoiseInByte = dwNoisebuffer.toByteArray();
          dwNoisebuffer.close();
          ScalingReport dwNoiseScale = new ScalingReport();
          try {
            dwNoiseScale = scale(dwNoiseInByte, 130, 130);
            String dwNoiseBase64String =
                Base64.getEncoder().encodeToString(dwNoiseScale.scaledByte);
            reportBase64.dwNoiseBase64 = "data:image/jpeg;base64," + dwNoiseBase64String;
            reportBase64.widthdwNoise = dwNoiseScale.width;
          } catch (ApplicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("Scalling exception");
            String dwNoiseBase64String = Base64.getEncoder().encodeToString(dwNoiseInByte);
            reportBase64.dwNoiseBase64 = "data:image/jpeg;base64," + dwNoiseBase64String;
            reportBase64.widthdwNoise = dwNoiseImage.getWidth();
          }
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
      if (report.elaReport.completed) {
        byte[] elaInByte;
        BufferedImage elaImage;
        try {
          elaImage = ImageIO.read(new File(report.elaReport.map));
          ByteArrayOutputStream elabuffer = new ByteArrayOutputStream();
          ImageIO.write(elaImage, "png", elabuffer);
          elabuffer.flush();
          elaInByte = elabuffer.toByteArray();
          elabuffer.close();
          ScalingReport elaScale = new ScalingReport();
          try {
            elaScale = scale(elaInByte, 130, 130);
            String elaBase64String = Base64.getEncoder().encodeToString(elaScale.scaledByte);
            reportBase64.elaBase64 = "data:image/jpeg;base64," + elaBase64String;
            reportBase64.widthela = elaScale.width;
          } catch (ApplicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("Scalling exception");
            String elaBase64String = Base64.getEncoder().encodeToString(elaInByte);
            reportBase64.elaBase64 = "data:image/jpeg;base64," + elaBase64String;
            reportBase64.widthela = elaImage.getWidth();
          }
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
      if (report.blockingReport.completed) {
        byte[] blockingInByte;
        BufferedImage blockingImage;
        try {
          blockingImage = ImageIO.read(new File(report.blockingReport.map));
          ByteArrayOutputStream blockingbuffer = new ByteArrayOutputStream();
          ImageIO.write(blockingImage, "png", blockingbuffer);
          blockingbuffer.flush();
          blockingInByte = blockingbuffer.toByteArray();
          blockingbuffer.close();
          ScalingReport blockingScale = new ScalingReport();
          try {
            blockingScale = scale(blockingInByte, 130, 130);
            String blockingBase64String =
                Base64.getEncoder().encodeToString(blockingScale.scaledByte);
            reportBase64.blockingBase64 = "data:image/jpeg;base64," + blockingBase64String;
            reportBase64.widthblocking = blockingScale.width;
          } catch (ApplicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("Scalling exception");
            String blockingBase64String = Base64.getEncoder().encodeToString(blockingInByte);
            reportBase64.blockingBase64 = "data:image/jpeg;base64," + blockingBase64String;
            reportBase64.widthblocking = blockingImage.getWidth();
          }
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
      if (report.medianNoiseReport.completed) {
        byte[] medianNoiseInByte;
        BufferedImage medianNoiseImage;
        try {
          medianNoiseImage = ImageIO.read(new File(report.medianNoiseReport.map));
          ByteArrayOutputStream medianNoisebuffer = new ByteArrayOutputStream();
          ImageIO.write(medianNoiseImage, "png", medianNoisebuffer);
          medianNoisebuffer.flush();
          medianNoiseInByte = medianNoisebuffer.toByteArray();
          medianNoisebuffer.close();
          ScalingReport medianNoiseScale = new ScalingReport();
          try {
            medianNoiseScale = scale(medianNoiseInByte, 130, 130);
            String medianNoiseBase64String =
                Base64.getEncoder().encodeToString(medianNoiseScale.scaledByte);
            reportBase64.medianNoiseBase64 = "data:image/jpeg;base64," + medianNoiseBase64String;
            reportBase64.widthmedianNoise = medianNoiseScale.width;
          } catch (ApplicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("Scalling exception");
            String medianNoiseBase64String = Base64.getEncoder().encodeToString(medianNoiseInByte);
            reportBase64.medianNoiseBase64 = "data:image/jpeg;base64," + medianNoiseBase64String;
            reportBase64.widthmedianNoise = medianNoiseImage.getWidth();
          }
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
      if (report.ghostReport.completed) {

        try {
          for (int GhostInd = 0; GhostInd < report.ghostReport.maps.size(); GhostInd++) {
            byte[] ghostInByte;
            BufferedImage ghostImage;
            ghostImage = ImageIO.read(new File(report.ghostReport.maps.get(GhostInd)));
            ByteArrayOutputStream ghostbuffer = new ByteArrayOutputStream();
            ImageIO.write(ghostImage, "png", ghostbuffer);
            ghostbuffer.flush();
            ghostInByte = ghostbuffer.toByteArray();
            ghostbuffer.close();
            ScalingReport ghostScale = new ScalingReport();
            try {
              ghostScale = scale(ghostInByte, 130, 130);
              String ghostBase64String = Base64.getEncoder().encodeToString(ghostScale.scaledByte);
              reportBase64.ghostBase64.add("data:image/jpeg;base64," + ghostBase64String);
              reportBase64.widthghost.add(ghostScale.width);
            } catch (ApplicationException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
              System.out.println("Scalling exception");
              String ghostBase64String = Base64.getEncoder().encodeToString(ghostInByte);
              reportBase64.ghostBase64.add("data:image/jpeg;base64," + ghostBase64String);
              reportBase64.widthghost.add(ghostImage.getWidth());
            }
          }
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }

      if (report.gridsReport.completed) {
        byte[] gridsInByte, gridsInvInByte;
        BufferedImage gridsImage, gridsInvImage;
        String gridsBase64String, gridsInvBase64String;
        try {
          // GRIDS
          gridsImage = ImageIO.read(new File(report.gridsReport.map));
          ByteArrayOutputStream gridsbuffer = new ByteArrayOutputStream();
          ImageIO.write(gridsImage, "png", gridsbuffer);
          gridsbuffer.flush();
          gridsInByte = gridsbuffer.toByteArray();
          gridsbuffer.close();
          ScalingReport gridsScale = new ScalingReport();
          try {
            gridsScale = scale(gridsInByte, 130, 130);
            gridsBase64String = Base64.getEncoder().encodeToString(gridsScale.scaledByte);
            reportBase64.gridsBase64 = "data:image/jpeg;base64," + gridsBase64String;
            reportBase64.widthgrids = gridsScale.width;
          } catch (ApplicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("Scalling exception");
            gridsBase64String = Base64.getEncoder().encodeToString(gridsInByte);
            reportBase64.gridsBase64 = "data:image/jpeg;base64," + gridsBase64String;
            reportBase64.widthgrids = gridsImage.getWidth();
          }

          // GRIDS INVERSED
          gridsInvImage = ImageIO.read(new File(report.gridsInversedReport.map));
          ByteArrayOutputStream gridsInvbuffer = new ByteArrayOutputStream();
          ImageIO.write(gridsInvImage, "png", gridsInvbuffer);
          gridsInvbuffer.flush();
          gridsInvInByte = gridsInvbuffer.toByteArray();
          gridsInvbuffer.close();
          ScalingReport gridsInvScale = new ScalingReport();
          try {
            gridsInvScale = scale(gridsInvInByte, 130, 130);
            gridsInvBase64String = Base64.getEncoder().encodeToString(gridsInvScale.scaledByte);
            reportBase64.gridsInversedBase64 = "data:image/jpeg;base64," + gridsInvBase64String;
            reportBase64.widthgridsInversed = gridsInvScale.width;
          } catch (ApplicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("Scalling exception");
            gridsInvBase64String = Base64.getEncoder().encodeToString(gridsInvInByte);
            reportBase64.gridsInversedBase64 = "data:image/jpeg;base64," + gridsInvBase64String;
            reportBase64.widthgridsInversed = gridsInvImage.getWidth();
          }
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }

    } else {
      System.out.println("report is empty");
    }
    mongoclient.close();
    return reportBase64;
  }