示例#1
0
 public static void setForbiddenLevel(LogLevel level) {
   // If null, reset to the default level
   if (level == null) {
     level = ERROR;
   }
   ((LoggerWrapper) logger).setForbiddenLevel(level);
 }
//
// ошибки в процессе логина не ловятся ControllerAdvice, поэтому на клиента поедет страничка с кодом
// 404
// ну а нам нужен JSON - вот и сделаем JSON
//
public class Resp401BasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {
  LoggerWrapper LOG = LoggerWrapper.get(Resp401BasicAuthenticationEntryPoint.class);

  @Override
  public void commence(
      HttpServletRequest request,
      HttpServletResponse response,
      AuthenticationException authException)
      throws IOException, ServletException {
    //        if( authException instanceof InsufficientAuthenticationException) {
    //           return;
    //       }
    response.addHeader("Access-Control-Allow-Origin", "null");
    response.addHeader("WWW-Authenticate", "Basic realm=\"" + getRealmName() + "\"");
    response.addHeader("Content-Type", "application/json");
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

    PrintWriter writer = response.getWriter();

    ObjectMapper mapper = new ObjectMapper();
    ErrorInfo errorInfo =
        new ErrorInfo(HttpServletResponse.SC_UNAUTHORIZED, authException.getLocalizedMessage());
    String jsonError = mapper.writeValueAsString(errorInfo);
    writer.println(jsonError);
    LOG.info("result = " + jsonError);
  }
}
  /**
   * Validate the Http response for any faults returned from the server.
   *
   * @throws FaultException
   * @throws ClientException
   */
  private void checkForFault() throws FaultException, ClientException {
    try {
      logger.log(Logger.LT_INFO, "Reading response...");

      int responseCode = getHttpResponseCode();

      // if successful, there's nothing left to do here.
      // we'll process the response in a later method.
      if (responseCode == HttpURLConnection.HTTP_OK) return;

      InputStream errorStream = getResponseErrorStream();

      // if there is no error stream, then it is not a fault
      if (errorStream == null) {
        throw new ClientException(responseCode, logger);
      }

      // read error stream into a byte array
      byte[] errorBytes;
      try {
        errorBytes = Utility.read(errorStream);
        errorStream.close();
      } catch (IOException ioe) {
        throw new ClientException(
            responseCode, "Failed to read additional HTTP error", true, logger);
      }

      // server will return HTTP 500 on a fault
      if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
        try {
          ByteArrayInputStream bais = new ByteArrayInputStream(errorBytes);

          Document faultDoc = builder.parse(bais);
          bais.close();

          throw new FaultException(faultDoc, mc.getEffectiveNamespaceURI(), logger);

        } catch (IOException ioe) {
          // If an IO exception occurs, we're not sure whether
          // or not it was a fault.  So we mark it as critical.
          String text = new String(errorBytes);
          throw new ClientException(responseCode, text, true, logger);
        } catch (SAXException ioe) {
          // If parsing fails, it means it's not a fault after all.
          String text = new String(errorBytes);
          throw new ClientException(responseCode, text, logger);
        }
      } else {
        // non-500 return codes are definitely not faults
        String text = new String(errorBytes);
        throw new ClientException(responseCode, text, logger);
      }
    }
    // catch other IOException's that we have not already handled.
    catch (IOException e) {
      throw new ClientException(e, true, logger);
    }
  }
 /**
  * Method helps to parse/read the response xml into Document object.
  *
  * @return - returns a Document object
  * @throws IOException
  * @throws SAXException
  */
 private Document parseReceivedDocument() throws IOException, SAXException {
   logger.log(Logger.LT_INFO, "Parsing response...");
   return builder.parse(getResponseStream());
 }
示例#5
0
 private void performLogging(String message) {
   // TODO
   LoggerWrapper.get().errorLog("GeneralCustomException: " + message);
 }
示例#6
0
 public static void setLogger(Logger newLogger) {
   // Note: the "logger" attribute is not of type LoggerWrapper so that it appears
   // as a standard Logger in the javadocs.
   ((LoggerWrapper) logger).setLogger(newLogger);
 }