/**
 * Copyright 2016, RadiantBlue Technologies, Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package gateway.auth;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

import model.response.ErrorResponse;

/**
 * Custom Sprint 'Entry Point' that issues an appropriate response when authentication fails.
 * 
 * @author Russell.Orf
 * 
 */
@Component
public class PiazzaBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {

	@Override
	public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authEx)
			throws IOException, ServletException {
		response.addHeader("WWW-Authenticate", "Basic realm=\"" + getRealmName() + "\"");
		response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
		response.setContentType("application/json");
		PrintWriter writer = response.getWriter();
		// Create a Response Object
		ErrorResponse error = new ErrorResponse("Gateway is unable to authenticate the provided user.", "Gateway");
		writer.println(new ObjectMapper().writeValueAsString(error));
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		setRealmName("Piazza");
		super.afterPropertiesSet();
	}
}
@@ -824,16 +824,79 @@
 ponse;%0A%0A
+import org.springframework.beans.factory.annotation.Autowired;%0A
 import o
@@ -949,24 +949,24 @@
 nException;%0A
-
 import org.s
@@ -1182,16 +1182,42 @@
 esponse;
+%0Aimport util.PiazzaLogger;
 %0A%0A/**%0A *
@@ -1442,16 +1442,58 @@
 yPoint %7B
+%0A%09@Autowired%0A%09private PiazzaLogger logger;
 %0A%0A%09@Over
@@ -1916,16 +1916,16 @@
  Object%0A
-
 %09%09ErrorR
@@ -2025,16 +2025,256 @@
 teway%22);
+%0A%0A%09%09// Log the request%0A%09%09logger.log(String.format(%22Unable to authenticate a user with Auth Type %25s and Header %25s%22, request.getAuthType(),%0A%09%09%09%09request.getHeader(%22Authorization%22).toString()), PiazzaLogger.ERROR);%0A%0A%09%09// Write back the response
 %0A%09%09write

Add logging for auth header on failed request