<commit_before>/**
 * 
 */
package com.trendrr.strest.server;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpVersion;

import com.trendrr.oss.DynMap;
import com.trendrr.strest.StrestUtil;


/**
 * @author <NAME>
 * @created Jan 26, 2011
 * 
 */
public class ResponseBuilder {

	protected Log log = LogFactory.getLog(ResponseBuilder.class);

	HttpResponse response;
	
	public static void main(String...strings) {
		ResponseBuilder b = new ResponseBuilder();
	}
	
	public static ResponseBuilder instance() {
		return new ResponseBuilder();
	}
	
	public static ResponseBuilder instance(HttpRequest request) {
		return new ResponseBuilder(request);
	}
	
	public static ResponseBuilder instance(HttpResponse response) {
		return new ResponseBuilder(response);
	}
	
	/**
	 * Creates a response builder 
	 * txn-status is complete by default
	 */
	public ResponseBuilder() {
		response = new DefaultHttpResponse(
				new HttpVersion("STREST", 0, 1, true), HttpResponseStatus.OK);
		this.txnStatus(StrestUtil.HEADERS.TXN_STATUS_VALUES.COMPLETE);
	}
	
	
	/**
	 * creates a new response builder based on the txn id of the request.
	 * @param request
	 */
	public ResponseBuilder(HttpRequest request) {
		response = new DefaultHttpResponse(
				request.getProtocolVersion(), HttpResponseStatus.OK);
		this.txnId(request.getHeader(StrestUtil.HEADERS.TXN_ID));
		
	}
	
	/**
	 * creates a new response builder that acts on the given response.
	 * @param response
	 */
	public ResponseBuilder(HttpResponse response) {
		this.response = response;
	}
	
	public ResponseBuilder txnStatus(String status) {
		response.setHeader(StrestUtil.HEADERS.TXN_STATUS, status);
		return this;
	}
	
	/**
	 * sets the txn status to complete
	 * @return
	 */
	public ResponseBuilder txnStatusComplete() {
		response.setHeader(StrestUtil.HEADERS.TXN_STATUS, StrestUtil.HEADERS.TXN_STATUS_VALUES.COMPLETE);
		return this;
	}
	
	/**
	 * sets txn status to continue
	 * @return
	 */
	public ResponseBuilder txnStatusContinue() {
		response.setHeader(StrestUtil.HEADERS.TXN_STATUS, StrestUtil.HEADERS.TXN_STATUS_VALUES.CONTINUE);
		return this;
	}
	
	/**
	 * changes the response to a MOVED PERMANENTLY redirect.
	 * 
	 * This could be problematic for STREST clients, it is up to them to implement
	 * redirects (or not). 
	 * 
	 * @param url
	 * @return
	 */
	public ResponseBuilder redirect(String url) {
		this.response.setStatus(HttpResponseStatus.MOVED_PERMANENTLY);
		this.response.setHeader("Location", url);
		return this;<commit_msg>Change redirect call to 302 instead of 301<commit_after>/**
 * 
 */
package com.trendrr.strest.server;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpVersion;

import com.trendrr.oss.DynMap;
import com.trendrr.strest.StrestUtil;


/**
 * @author <NAME>
 * @created Jan 26, 2011
 * 
 */
public class ResponseBuilder {

	protected Log log = LogFactory.getLog(ResponseBuilder.class);

	HttpResponse response;
	
	public static void main(String...strings) {
		ResponseBuilder b = new ResponseBuilder();
	}
	
	public static ResponseBuilder instance() {
		return new ResponseBuilder();
	}
	
	public static ResponseBuilder instance(HttpRequest request) {
		return new ResponseBuilder(request);
	}
	
	public static ResponseBuilder instance(HttpResponse response) {
		return new ResponseBuilder(response);
	}
	
	/**
	 * Creates a response builder 
	 * txn-status is complete by default
	 */
	public ResponseBuilder() {
		response = new DefaultHttpResponse(
				new HttpVersion("STREST", 0, 1, true), HttpResponseStatus.OK);
		this.txnStatus(StrestUtil.HEADERS.TXN_STATUS_VALUES.COMPLETE);
	}
	
	
	/**
	 * creates a new response builder based on the txn id of the request.
	 * @param request
	 */
	public ResponseBuilder(HttpRequest request) {
		response = new DefaultHttpResponse(
				request.getProtocolVersion(), HttpResponseStatus.OK);
		this.txnId(request.getHeader(StrestUtil.HEADERS.TXN_ID));
		
	}
	
	/**
	 * creates a new response builder that acts on the given response.
	 * @param response
	 */
	public ResponseBuilder(HttpResponse response) {
		this.response = response;
	}
	
	public ResponseBuilder txnStatus(String status) {
		response.setHeader(StrestUtil.HEADERS.TXN_STATUS, status);
		return this;
	}
	
	/**
	 * sets the txn status to complete
	 * @return
	 */
	public ResponseBuilder txnStatusComplete() {
		response.setHeader(StrestUtil.HEADERS.TXN_STATUS, StrestUtil.HEADERS.TXN_STATUS_VALUES.COMPLETE);
		return this;
	}
	
	/**
	 * sets txn status to continue
	 * @return
	 */
	public ResponseBuilder txnStatusContinue() {
		response.setHeader(StrestUtil.HEADERS.TXN_STATUS, StrestUtil.HEADERS.TXN_STATUS_VALUES.CONTINUE);
		return this;
	}
	
	/**
	 * Sets the status to 302, and the Location to the url.  This is a standard (sort of) redirect. 
	 * 
	 * This could be problematic for STREST clients, it is up to them to implement
	 * redirects (or not). 
	 * 
	 * @param url
	 * @return
	 */
	public ResponseBuilder redirect(String url) {
		this.response.setStatus(HttpResponseStatus.FOUND);
		this.response.setHeader("Location", url);
		return this;
{'fraction_non_alphanumeric': 0.10417629835105563, 'fraction_numerical': 0.003852673755586377, 'mean_word_length': 4.440067057837385, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '524012', 'n_tokens_mistral': 2176, 'n_tokens_neox': 1967, 'n_words': 456}