Beispiel #1
0
 @Before
 public void setUp() throws Exception {
   lc = new LoggerContext();
   converter = new MDCConverter();
   converter.start();
   MDC.clear();
 }
  public Object step(int step) {
    helper.assertStep(step);
    try {
      MDC.put("fileset", logFilename);
      log.debug("Step " + step);
      Job j = activity.getChild();
      if (j == null) {
        throw helper.cancel(new ERR(), null, "null-job");
      } else if (!(j instanceof MetadataImportJob)) {
        throw helper.cancel(new ERR(), null, "unexpected-job-type", "job-type", j.ice_id());
      }

      if (step == 0) {
        return importMetadata((MetadataImportJob) j);
      } else if (step == 1) {
        return pixelData(null); // (ThumbnailGenerationJob) j);
      } else if (step == 2) {
        return generateThumbnails(null); // (PixelDataJob) j); Nulls image
      } else if (step == 3) {
        // TODO: indexing and scripting here as well.
        store.launchProcessing();
        return null;
      } else if (step == 4) {
        return objects;
      } else {
        throw helper.cancel(new ERR(), null, "bad-step", "step", "" + step);
      }
    } catch (MissingLibraryException mle) {
      notifyObservers(new ErrorHandler.MISSING_LIBRARY(fileName, mle, usedFiles, format));
      throw helper.cancel(new ERR(), mle, "import-missing-library", "filename", fileName);
    } catch (UnsupportedCompressionException uce) {
      // Handling as UNKNOWN_FORMAT for 4.3.0
      notifyObservers(new ErrorHandler.UNKNOWN_FORMAT(fileName, uce, this));
      throw helper.cancel(new ERR(), uce, "import-unknown-format", "filename", fileName);
    } catch (UnknownFormatException ufe) {
      notifyObservers(new ErrorHandler.UNKNOWN_FORMAT(fileName, ufe, this));
      throw helper.cancel(new ERR(), ufe, "import-unknown-format", "filename", fileName);
    } catch (IOException io) {
      notifyObservers(new ErrorHandler.FILE_EXCEPTION(fileName, io, usedFiles, format));
      throw helper.cancel(new ERR(), io, "import-file-exception", "filename", fileName);
    } catch (FormatException fe) {
      notifyObservers(new ErrorHandler.FILE_EXCEPTION(fileName, fe, usedFiles, format));
      throw helper.cancel(new ERR(), fe, "import-file-exception", "filename", fileName);
    } catch (Cancel c) {
      throw c;
    } catch (Throwable t) {
      notifyObservers(
          new ErrorHandler.INTERNAL_EXCEPTION(
              fileName, new RuntimeException(t), usedFiles, format));
      throw helper.cancel(new ERR(), t, "import-request-failure");
    } finally {
      try {
        long size = logPath.size();
        store.updateFileSize(logFile, size);
      } catch (Throwable t) {
        throw helper.cancel(new ERR(), t, "update-log-file-size");
      }
      MDC.clear();
    }
  }
Beispiel #3
0
 @After
 public void tearDown() throws Exception {
   lc = null;
   converter.stop();
   converter = null;
   MDC.clear();
 }
Beispiel #4
0
 @Test
 public void mdc() throws EvaluationException {
   MDC.put("key", "val");
   LoggingEvent event = makeEvent("x");
   doEvaluateAndCheck("e.mdc['key'] == 'val'", event, true);
   MDC.clear();
 }
Beispiel #5
0
 private void calPerf() {
   this.bytesRead = (long) (vol.getReadBytes() - this.pbytesRead);
   this.pbytesRead = vol.getReadBytes();
   MDC.put("bytesRead", Long.toString(bytesRead));
   this.bytesWritten = (long) (vol.getActualWriteBytes() - this.pbytesWritten);
   this.pbytesWritten = vol.getActualWriteBytes();
   MDC.put("bytesWritten", Long.toString(this.bytesWritten));
   this.duplicateBytes = (long) (vol.getDuplicateBytes() - this.pduplicateBytes);
   this.pduplicateBytes = vol.getDuplicateBytes();
   MDC.put("duplicateBytes", Long.toString(this.duplicateBytes));
   this.virtualBytesWritten = (long) (vol.getVirtualBytesWritten() - this.pvirtualBytesWritten);
   this.pvirtualBytesWritten = vol.getVirtualBytesWritten();
   MDC.put("virtualBytesWritten", Long.toString(this.virtualBytesWritten));
   this.RIOPS = (long) (vol.getReadOperations() - this.pRIOPS);
   this.pRIOPS = vol.getReadOperations();
   MDC.put("RIOPS", Long.toString(this.RIOPS));
   this.WIOPS = (long) (vol.getWriteOperations() - this.pWIOPS);
   this.pWIOPS = vol.getWriteOperations();
   this.dseSz = HCServiceProxy.getDSESize();
   this.dseCompSz = HCServiceProxy.getDSECompressedSize();
   MDC.put("dseSz", Long.toString(this.dseSz));
   MDC.put("dseCompSz", Long.toString(this.dseCompSz));
   MDC.put("WIOPS", Long.toString(this.WIOPS));
   MDC.put("sdfsCpuLoad", Double.toString(perf.getProcessCpuLoad()));
   MDC.put("sdfsCpuTime", Double.toString(perf.getProcessCpuTime()));
   MDC.put("systemCpuLoad", Double.toString(perf.getSystemCpuLoad()));
   MDC.put("systemCpuAverage", Double.toString(perf.getSystemLoadAverage()));
   MDC.put("freeMemory", Long.toString(perf.getFreePhysicalMemorySize()));
   MDC.put("totalMemory", Long.toString(perf.getTotalPhysicalMemorySize()));
   MDC.put("freeSwap", Long.toString(perf.getFreeSwapSpaceSize()));
   MDC.put("totalSwap", Long.toString(perf.getTotalSwapSpaceSize()));
   log.info(vol.getName());
   MDC.clear();
 }
 @Before
 public void setUp() throws Exception {
   MDC.clear();
   ServerSetup serverSetup = new ServerSetup(port, "localhost", ServerSetup.PROTOCOL_SMTP);
   greenMailServer = new GreenMail(serverSetup);
   greenMailServer.start();
   // give the server a head start
   if (EnvUtilForTests.isRunningOnSlowJenkins()) {
     Thread.sleep(2000);
   } else {
     Thread.sleep(50);
   }
 }
  @Before
  public void setUp() throws Exception {

    OnConsoleStatusListener.addNewInstanceToContext(loggerContext);
    MDC.clear();
    ServerSetup serverSetup = new ServerSetup(port, "localhost", ServerSetup.PROTOCOL_SMTP);
    greenMailServer = new GreenMail(serverSetup);
    greenMailServer.start();
    // give the server a head start
    if (EnvUtilForTests.isRunningOnSlowJenkins()) {
      Thread.sleep(2000);
    } else {
      Thread.sleep(50);
    }
  }
Beispiel #8
0
 @Override
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
     throws IOException, ServletException {
   try {
     Principal principal = ((HttpServletRequest) request).getUserPrincipal();
     if (principal != null) {
       MDC.put(LogConstants.USER, principal.getName());
     } else {
       MDC.put(LogConstants.USER, "?");
     }
     chain.doFilter(request, response);
   } finally {
     MDC.clear();
   }
 }
  @Test
  public void LBCLASSIC_104() throws Exception {
    buildSMTPAppender(smtpServer.getSmtp().getPort(), SYNCHRONOUS);
    smtpAppender.setAsynchronousSending(false);
    smtpAppender.setLayout(buildPatternLayout(lc, DEFAULT_PATTERN));
    smtpAppender.start();
    logger.addAppender(smtpAppender);
    MDC.put("key", "val");
    logger.debug("hello");
    MDC.clear();
    logger.error("an error", new Exception("an exception"));

    MimeMultipart mp = verify(smtpServer, TEST_SUBJECT);
    String body = GreenMailUtil.getBody(mp.getBodyPart(0));
    assertTrue("missing HEADER in body", body.startsWith(HEADER.trim()));
    assertTrue("missing MDC in body", body.contains("key=val"));
    assertTrue("missing FOOTER in body", body.endsWith(FOOTER.trim()));
  }
  // lost MDC
  @Test
  public void LBCLASSIC_104() throws Exception {
    String subject = "LBCLASSIC_104";
    buildSMTPAppender(subject, SYNCHRONOUS);
    smtpAppender.setAsynchronousSending(false);
    smtpAppender.setLayout(buildPatternLayout(DEFAULT_PATTERN));
    smtpAppender.start();
    logger.addAppender(smtpAppender);
    MDC.put("key", "val");
    logger.debug("hello");
    MDC.clear();
    logger.error("en error", new Exception("test"));

    MimeMultipart mp = verifyAndExtractMimeMultipart(subject);
    String body = GreenMailUtil.getBody(mp.getBodyPart(0));
    assertTrue("missing HEADER in body", body.startsWith(HEADER.trim()));
    assertTrue("missing MDC in body", body.contains("key=val"));
    assertTrue("missing FOOTER in body", body.endsWith(FOOTER.trim()));
  }
  /** {@inheritDoc} */
  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    try {
      MDC.put(Version.MDC_ATTRIBUTE, Version.getVersion());
      MDC.put(CLIENT_ADDRESS_MDC_ATTRIBUTE, request.getRemoteAddr());
      MDC.put(SERVER_ADDRESS_MDC_ATTRIBUTE, request.getServerName());
      MDC.put(SERVER_PORT_MDC_ATTRIBUTE, Integer.toString(request.getServerPort()));
      if (request instanceof HttpServletRequest) {
        final HttpSession session = ((HttpServletRequest) request).getSession();
        if (session != null) {
          MDC.put(JSESSIONID_MDC_ATTRIBUTE, session.getId());
        }
      }

      chain.doFilter(request, response);
    } finally {
      MDC.clear();
    }
  }
  @Test
  public void eventWasPreparedForDeferredProcessing() {
    asyncAppender.addAppender(listAppender);
    asyncAppender.start();

    String k = "k" + diff;
    MDC.put(k, "v");
    asyncAppender.doAppend(builder.build(diff));
    MDC.clear();

    asyncAppender.stop();
    assertFalse(asyncAppender.isStarted());

    // check the event
    assertEquals(1, listAppender.list.size());
    ILoggingEvent e = listAppender.list.get(0);

    // check that MDC values were correctly retained
    assertEquals("v", e.getMDCPropertyMap().get(k));
    assertFalse(e.hasCallerData());
  }
  /** Called during {@link #getResponse()}. */
  private void cleanup() {
    MDC.put("fileset", logFilename);
    try {
      cleanupReader();
      cleanupStore();

      log.info(ClassicConstants.FINALIZE_SESSION_MARKER, "Finalizing log file.");
      /* hereafter back to normal log destination, not import log file*/
      MDC.clear();
      try {
        /* requires usable session */
        setLogFileSize();
      } catch (ServerError se) {
        log.error("failed to set import log file size", se);
      }

      cleanupSession();
    } finally {
      autoClose(); // Doesn't throw
      if (resources != null) {
        resources.remove(resourcesEntry);
      }
    }
  }
Beispiel #14
0
  @Test
  public void mdc() throws Exception {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    Mockito.when(request.getUserPrincipal()).thenReturn(null);
    Mockito.when(request.getMethod()).thenReturn("METHOD");
    Mockito.when(request.getPathInfo()).thenReturn("/pathinfo");

    ServletResponse response = Mockito.mock(ServletResponse.class);

    final AtomicBoolean invoked = new AtomicBoolean();

    FilterChain chain =
        new FilterChain() {
          @Override
          public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
              throws IOException, ServletException {
            Assert.assertEquals(MDC.get("hostname"), null);
            Assert.assertEquals(MDC.get("user"), null);
            Assert.assertEquals(MDC.get("method"), "METHOD");
            Assert.assertEquals(MDC.get("path"), "/pathinfo");
            invoked.set(true);
          }
        };

    MDC.clear();
    Filter filter = new MDCFilter();
    filter.init(null);

    filter.doFilter(request, response, chain);
    Assert.assertTrue(invoked.get());
    Assert.assertNull(MDC.get("hostname"));
    Assert.assertNull(MDC.get("user"));
    Assert.assertNull(MDC.get("method"));
    Assert.assertNull(MDC.get("path"));

    Mockito.when(request.getUserPrincipal())
        .thenReturn(
            new Principal() {
              @Override
              public String getName() {
                return "name";
              }
            });

    invoked.set(false);
    chain =
        new FilterChain() {
          @Override
          public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
              throws IOException, ServletException {
            Assert.assertEquals(MDC.get("hostname"), null);
            Assert.assertEquals(MDC.get("user"), "name");
            Assert.assertEquals(MDC.get("method"), "METHOD");
            Assert.assertEquals(MDC.get("path"), "/pathinfo");
            invoked.set(true);
          }
        };
    filter.doFilter(request, response, chain);
    Assert.assertTrue(invoked.get());

    HostnameFilter.HOSTNAME_TL.set("HOST");

    invoked.set(false);
    chain =
        new FilterChain() {
          @Override
          public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
              throws IOException, ServletException {
            Assert.assertEquals(MDC.get("hostname"), "HOST");
            Assert.assertEquals(MDC.get("user"), "name");
            Assert.assertEquals(MDC.get("method"), "METHOD");
            Assert.assertEquals(MDC.get("path"), "/pathinfo");
            invoked.set(true);
          }
        };
    filter.doFilter(request, response, chain);
    Assert.assertTrue(invoked.get());

    HostnameFilter.HOSTNAME_TL.remove();

    filter.destroy();
  }
Beispiel #15
0
  /**
   * Handle a request.
   *
   * @param request
   * @param response
   * @throws IOException
   * @throws ServletException
   */
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {

    MDC.clear();
    Date created = new java.util.Date();
    long start = created.getTime();

    String sendEncoding = request.getHeader("Accept-Encoding");
    String recvEncoding = request.getHeader("Content-Encoding");

    MDC.put("Accept-Encoding", sendEncoding);
    MDC.put("Content-Encoding", recvEncoding);
    DispatcherInterface dis = null;
    BufferedReader r = null;
    BufferedWriter out = null;
    try {

      Navajo in = null;

      if (streamingMode) {
        if (sendEncoding != null && sendEncoding.equals(COMPRESS_JZLIB)) {
          r =
              new BufferedReader(
                  new java.io.InputStreamReader(new InflaterInputStream(request.getInputStream())));
        } else if (sendEncoding != null && sendEncoding.equals(COMPRESS_GZIP)) {
          r =
              new BufferedReader(
                  new java.io.InputStreamReader(
                      new java.util.zip.GZIPInputStream(request.getInputStream()), "UTF-8"));
        } else {
          r = new BufferedReader(request.getReader());
        }
        in = NavajoFactory.getInstance().createNavajo(r);
        r.close();
        r = null;
      } else {
        logger.info(
            "Warning: Using non-streaming mode for "
                + request.getRequestURI()
                + ", file written: "
                + logfileIndex
                + ", total size: "
                + bytesWritten);
        InputStream is = request.getInputStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        copyResource(bos, is);
        is.close();
        bos.close();
        byte[] bytes = bos.toByteArray();
        try {
          if (sendEncoding != null && sendEncoding.equals(COMPRESS_JZLIB)) {
            r =
                new BufferedReader(
                    new java.io.InputStreamReader(
                        new InflaterInputStream(new ByteArrayInputStream(bytes))));
          } else if (sendEncoding != null && sendEncoding.equals(COMPRESS_GZIP)) {
            r =
                new BufferedReader(
                    new java.io.InputStreamReader(
                        new java.util.zip.GZIPInputStream(new ByteArrayInputStream(bytes)),
                        "UTF-8"));
          } else {
            r = new BufferedReader(new java.io.InputStreamReader(new ByteArrayInputStream(bytes)));
          }
          in = NavajoFactory.getInstance().createNavajo(r);
          if (in == null) {
            throw new Exception("Invalid Navajo");
          }
          r.close();
          r = null;
        } catch (Throwable t) {
          // Write request to temp file.
          File f = DispatcherFactory.getInstance().getTempDir();

          if (f != null) {
            bytesWritten += bytes.length;
            logfileIndex++;
            FileOutputStream fos = new FileOutputStream(new File(f, "request-" + logfileIndex));
            copyResource(fos, new ByteArrayInputStream(bytes));
            fos.close();
            PrintWriter fw =
                new PrintWriter(new FileWriter(new File(f, "exception-" + logfileIndex)));
            t.printStackTrace(fw);
            fw.flush();
            fw.close();
          }

          dumHttp(request, logfileIndex, f);
          throw new ServletException(t);
        }
      }

      long stamp = System.currentTimeMillis();
      int pT = (int) (stamp - start);

      if (in == null) {
        throw new ServletException("Invalid request.");
      }

      Header header = in.getHeader();
      if (header == null) {
        throw new ServletException("Empty Navajo header.");
      }

      dis = DispatcherFactory.getInstance();
      if (dis == null) {
        System.err.println(
            "SERIOUS: No dispatcher found. The navajo context did not initialize properly, check the logs to find out why!");
        return;
      }
      // Check for certificate.
      Object certObject = request.getAttribute("javax.servlet.request.X509Certificate");

      // Call Dispatcher with parsed TML document as argument.
      ClientInfo clientInfo =
          new ClientInfo(
              request.getRemoteAddr(),
              "unknown",
              recvEncoding,
              pT,
              (recvEncoding != null
                  && (recvEncoding.equals(COMPRESS_GZIP) || recvEncoding.equals(COMPRESS_JZLIB))),
              (sendEncoding != null
                  && (sendEncoding.equals(COMPRESS_GZIP) || sendEncoding.equals(COMPRESS_JZLIB))),
              request.getContentLength(),
              created);

      Navajo outDoc = handleTransaction(dis, in, certObject, clientInfo);

      response.setContentType("text/xml; charset=UTF-8");
      response.setHeader("Accept-Ranges", "none");
      response.setHeader("Connection", "close");

      if (recvEncoding != null && recvEncoding.equals(COMPRESS_JZLIB)) {
        response.setHeader("Content-Encoding", COMPRESS_JZLIB);
        out =
            new BufferedWriter(
                new OutputStreamWriter(
                    new DeflaterOutputStream(response.getOutputStream()), "UTF-8"));
      } else if (recvEncoding != null && recvEncoding.equals(COMPRESS_GZIP)) {
        response.setHeader("Content-Encoding", COMPRESS_GZIP);
        out =
            new BufferedWriter(
                new OutputStreamWriter(
                    new java.util.zip.GZIPOutputStream(response.getOutputStream()), "UTF-8"));
      } else {
        out = new BufferedWriter(response.getWriter());
      }

      outDoc.write(out);
      out.flush();
      out.close();

      if (in.getHeader() != null
          && outDoc.getHeader() != null
          && !Dispatcher.isSpecialwebservice(in.getHeader().getRPCName())) {
        statLogger.info(
            "("
                + dis.getApplicationId()
                + "): "
                + new java.util.Date()
                + ": "
                + outDoc.getHeader().getHeaderAttribute("accessId")
                + ":"
                + in.getHeader().getRPCName()
                + "("
                + in.getHeader().getRPCUser()
                + "):"
                + (System.currentTimeMillis() - start)
                + " ms. (st="
                + (outDoc.getHeader().getHeaderAttribute("serverTime")
                    + ",rpt="
                    + outDoc.getHeader().getHeaderAttribute("requestParseTime")
                    + ",at="
                    + outDoc.getHeader().getHeaderAttribute("authorisationTime")
                    + ",pt="
                    + outDoc.getHeader().getHeaderAttribute("processingTime")
                    + ",tc="
                    + outDoc.getHeader().getHeaderAttribute("threadCount")
                    + ",cpu="
                    + outDoc.getHeader().getHeaderAttribute("cpuload")
                    + ")"
                    + " ("
                    + sendEncoding
                    + "/"
                    + recvEncoding
                    + ")"));
      }

      out = null;

    } catch (Throwable e) {
      logger.error("Error: ", e);
      dumHttp(request, -1, null);
      if (e instanceof FatalException) {
        FatalException fe = (FatalException) e;
        if (fe.getMessage().equals("500.13")) {
          // Server too busy.
          throw new ServletException("500.13", e);
        }
      }
      throw new ServletException(e);
    } finally {
      dis = null;
      if (r != null) {
        try {
          r.close();
        } catch (Exception e) {
          // NOT INTERESTED.
        }
      }
      if (out != null) {
        try {
          out.close();
        } catch (Exception e) {
          // NOT INTERESTED.
        }
      }
    }
  }
  public void init(Helper helper) {
    this.helper = helper;
    helper.setSteps(5);

    final ImportConfig config = new ImportConfig();
    final String sessionUuid = helper.getEventContext().getCurrentSessionUuid();

    if (!(location instanceof ManagedImportLocationI)) {
      throw helper.cancel(
          new ERR(), null, "bad-location", "location-type", location.getClass().getName());
    }

    ManagedImportLocationI managedLocation = (ManagedImportLocationI) location;
    logPath = managedLocation.getLogFile();
    logFilename = logPath.getFullFsPath();
    MDC.put("fileset", logFilename);

    file = ((ManagedImportLocationI) location).getTarget();

    try {
      sf = reg.getInternalServiceFactory(sessionUuid, "unused", 3, 1, clientUuid);
      store = new OMEROMetadataStoreClient();
      store.setCurrentLogFile(logFilename, token);
      store.initialize(sf);
      registerKeepAlive();

      fileName = file.getFullFsPath();
      shortName = file.getName();
      format = null;
      usedFiles = new String[] {fileName};

      open(reader, store, file);
      format = reader.getFormat();
      if (reader.getUsedFiles() != null) {
        usedFiles = reader.getUsedFiles();
      }
      if (usedFiles == null) {
        throw new NullPointerException("usedFiles must be non-null");
      }

      // Process all information which has been passed in as annotations
      detectKnownAnnotations();

      // Now that we've possibly updated the settings object, copy out
      // all the values needed by import.
      userSpecifiedTarget = settings.userSpecifiedTarget;
      userSpecifiedName =
          settings.userSpecifiedName == null ? null : settings.userSpecifiedName.getValue();
      userSpecifiedDescription =
          settings.userSpecifiedDescription == null
              ? null
              : settings.userSpecifiedDescription.getValue();
      userPixels = settings.userSpecifiedPixels;
      annotationList = settings.userSpecifiedAnnotationList;
      doThumbnails = settings.doThumbnails == null ? true : settings.doThumbnails.getValue();
      noStatsInfo = settings.noStatsInfo == null ? false : settings.noStatsInfo.getValue();

      IFormatReader baseReader = reader.getImageReader().getReader();
      if (log.isInfoEnabled()) {
        log.info("File format: " + format);
        log.info("Base reader: " + baseReader.getClass().getName());
      }
      notifyObservers(new ImportEvent.LOADED_IMAGE(shortName, 0, 0, 0));

      formatString = baseReader.getClass().getSimpleName();
      formatString = formatString.replace("Reader", "");

    } catch (Cancel c) {
      throw c;
    } catch (Throwable t) {
      throw helper.cancel(new ERR(), t, "error-on-init");
    } finally {
      MDC.clear();
    }
  }
 @Before
 public void setUp() throws Exception {
   smtpServer = new GreenMail();
   smtpServer.start();
   MDC.clear();
 }
 /* (non-Javadoc)
  * @see org.springframework.ws.server.EndpointInterceptor#afterCompletion(org.springframework.ws.context.MessageContext, java.lang.Object, java.lang.Exception)
  */
 @Override
 public void afterCompletion(MessageContext messageContext, Object endpoint, Exception ex)
     throws Exception {
   MDC.clear();
 }