<commit_before>package org.sagebionetworks.repo.web;

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

import org.sagebionetworks.repo.model.StackStatusDao;
import org.sagebionetworks.repo.model.status.StackStatus;
import org.sagebionetworks.repo.model.status.StatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * This intercepter checks the status of the repository before allowing any request to go through.
 * 
 * @author jmhill
 *
 */
public class StackStatusInterceptor implements HandlerInterceptor {
	
	@Autowired
	StackStatusDao stackStatusDao;

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		// We want any admin call to go through no matter what the state of the stack.
		// This ensures that we can change the state of the stack even when DOWN.
		// If this was not in place, then once a stack was down, there would be no way
		// to bring it up again using the web-services.
		String prefix = request.getRequestURI();
		if(prefix != null && prefix.indexOf(UrlHelpers.ADMIN) > -1){
			return true;
		}
		
		// Get the current stack status
		StatusEnum status = stackStatusDao.getCurrentStatus();
		if(StatusEnum.DOWN == status){
			StackStatus full = stackStatusDao.getFullCurrentStatus();
			throw new ServiceUnavailableException("Synapse is down for maintenance.  Message: "+full.getCurrentMessage());
		}else if(StatusEnum.READ_WRITE == status){
			return true;
		}else if(StatusEnum.READ_ONLY == status){
			// Allow all GETs
			if("GET".equals(request.getMethod())){
				return true;
			}else{
				StackStatus full = stackStatusDao.getFullCurrentStatus();
				throw new ServiceUnavailableException("Synapse is in READ_ONLY mode for maintenance.  Only HTTP GETs are allowed at this time.  Message: "+full.getCurrentMessage());
			}
		}else{
			throw new IllegalStateException("Unknown Synapse status: "+status);
		}
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,<commit_msg>added migration to the filter<commit_after>package org.sagebionetworks.repo.web;

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

import org.sagebionetworks.repo.model.StackStatusDao;
import org.sagebionetworks.repo.model.status.StackStatus;
import org.sagebionetworks.repo.model.status.StatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * This intercepter checks the status of the repository before allowing any request to go through.
 * 
 * @author jmhill
 *
 */
public class StackStatusInterceptor implements HandlerInterceptor {
	
	@Autowired
	StackStatusDao stackStatusDao;

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		// We want any admin call to go through no matter what the state of the stack.
		// This ensures that we can change the state of the stack even when DOWN.
		// If this was not in place, then once a stack was down, there would be no way
		// to bring it up again using the web-services.
		String prefix = request.getRequestURI();
		if(prefix != null && prefix.indexOf(UrlHelpers.ADMIN) > -1){
			return true;
		}
		// We want all migration to go through not matter what.
		if(prefix != null && prefix.indexOf(UrlHelpers.MIGRATION) > -1){
			return true;
		}
		
		// Get the current stack status
		StatusEnum status = stackStatusDao.getCurrentStatus();
		if(StatusEnum.DOWN == status){
			StackStatus full = stackStatusDao.getFullCurrentStatus();
			throw new ServiceUnavailableException("Synapse is down for maintenance.  Message: "+full.getCurrentMessage());
		}else if(StatusEnum.READ_WRITE == status){
			return true;
		}else if(StatusEnum.READ_ONLY == status){
			// Allow all GETs
			if("GET".equals(request.getMethod())){
				return true;
			}else{
				StackStatus full = stackStatusDao.getFullCurrentStatus();
				throw new ServiceUnavailableException("Synapse is in READ_ONLY mode for maintenance.  Only HTTP GETs are allowed at this time.  Message: "+full.getCurrentMessage());
			}
		}else{
			throw new IllegalStateException("Unknown Synapse status: "+status);
		}
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
{'fraction_non_alphanumeric': 0.08853379399697689, 'fraction_numerical': 0.0006478082487583676, 'mean_word_length': 4.587454764776839, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6652236', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1285, 'n_words': 405}