<commit_before>package org.bbop.apollo

import grails.converters.JSON
import org.apache.commons.collections.MultiMap
import org.apache.commons.collections.map.MultiValueMap
import org.codehaus.groovy.grails.web.json.JSONObject

import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

@Transactional(readOnly = true)
class ProxyController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond Proxy.list(params), model:[proxyInstanceCount: Proxy.count()]
    }

    def show(Proxy proxyInstance) {
        respond proxyInstance
    }

    def create() {
        respond new Proxy(params)
    }

    /**
     * What is input . . .
     * http://localhost:8080/apollo/proxy/request?request=http://golr.geneontology.org/solr/select?defType=edismax&qt=standard&indent=on&wt=json&rows=10&start=0&fl=*,score&facet=true&facet.mincount=1&facet.sort=count&json.nl=arrarr&facet.limit=25&fq=document_category:%22ontology_class%22&fq=source:(biological_process%20OR%20molecular_function%20OR%20cellular_component)&facet.field=annotation_class&facet.field=synonym&facet.field=alternate_id&q=gree*&qf=annotation_class%5E5&qf=annotation_class_label_searchable%5E5&qf=synonym_searchable%5E1&qf=alternate_id%5E1&json.wrf=jQuery1710751773979049176_1445036592600&_=1445036624006
     * http://golr.geneontology.org/solr/select
     * ?defType=edismax&
qt=standard&
indent=on&
wt=json&
rows=10&
start=0&
fl=*,score&
facet=true&
facet.mincount=1&
facet.sort=count&
json.nl=arrarr&
facet.limit=25&
fq=document_category:%22ontology_class%22&
fq=source:(biological_process%20OR%20molecular_function%20OR%20cellular_component)&
facet.field=annotation_class&
facet.field=synonym&
facet.field=alternate_id&
q=gree*&
qf=annotation_class%5E5&
qf=annotation_class_label_searchable%5E5&
qf=synonym_searchable%5E1&
qf=alternate_id%5E1&
json.wrf=jQuery1710751773979049176_1445036592600&
_=1445036624006

     // what is returned . .
     // MISSING "/solr/select"

     http://golr.geneontology.org
 ?json.wrf=jQuery17109697239506058395_1445105400207&
json=[nl:arrarr, wrf:jQuery17109697239506058395_1445105400207]&
facet=true&
indent=on&
facet.mincount=1&
facet.limit=25&
qf=[annotation_class^5, annotation_class_label_searchable^5, synonym_searchable^1, alternate_id^1]&
json.nl=arrarr&
wt=json&
defType=edismax&
rows=10&
fl=*,score&
start=0&
facet.sort=count&
q=red*&
_=1445105574330&
facet.field=[annotation_class, synonym, alternate_id]&
qt=standard&
fq=[document_category:"ontology_class", source:(biological_process OR molecular_function OR cellular_component)]&
protocol=http&
action=request&
controller=proxy&
returnType=solr&
url=golr.geneontology.org. Stacktrace follows:

     // what SHOULD be returned
     http://golr.geneontology.org/solr/select
 ?defType=edismax&
qt=standard&
indent=on&
wt=json&
rows=10&
start=0&
fl=*,score&
facet=true&
facet.mincount=1&
facet.sort=count&
json.nl=arrarr&
facet.limit=25&
fq=document_category:%22ontology_class%22&
fq=source:(biological_process%20OR%20molecular_function%20OR%20cellular_component)&
facet.field=annotation_class&
facet.field=synonym&
facet.field=alternate_id&
q=red*&
qf=annotation_class%5E5&
qf=annotation_class_label_searchable%5E5&
qf=synonym_searchable%5E1&
qf=alternate_id%5E1&
json.wrf=jQuery17101135514669585973_1445106124853&
_=1445106155048



     * @return
     */
    def request(String url){
        println "params: ${params}"
//        println "protocol: ${protocol}"
        println "requestUrl: ${url}"
//        println "returnType: ${returnType}"
        println "request URI: ${request.requestURI}"
//        http://localhost:8080/apollo/proxy/request/http%253A%252F%252Fgolr.geneontology.org%252Fsolr%252Fselect?defType=edismax&qt=standard&indent=on&wt=json&rows=10&start=0&fl=*,score&facet=true&facet.mincount=1&facet.sort=count&json.nl=arrarr&facet.limit=25&fq=document_category:%22ontology_class%22&fq=source:(biological_process%20OR%20molecular_function%20OR%20cellular_component)&facet.field=annotation_class&facet.field=synonym&facet.field=alternate_id&q=blood*&qf=annotation_class%5E5&qf=annotation_class_label_searchable%5E5&qf=synonym_searchable%5E1&qf=alternate_id%5E1&json.wrf=jQuery171043437046254985034_1445234103214&_=1445234283067

        String referenceUrl = URLDecoder.decode(url,"UTF-8")
        Proxy proxy = Proxy.findByReferenceUrl(referenceUrl)
//        String targetUrl = proxy ? proxy.targetUrl : referenceUrl
//        request.parameterMap.each {
//            println "key: ${it.key} -> ${it.value}"
//        }

        String targetUrl = referenceUrl

        MultiMap multiMap =new MultiValueMap()

        params.eachWithIndex{ it, idx ->
            if(it.value instanceof String){
                multiMap.put(it.key,URLEncoder.encode(it.value,"UTF-8"))
            }
            else if(it.value instanceof HashMap){<commit_msg>#148 - remove ability to do a generic proxy<commit_after>package org.bbop.apollo

import grails.converters.JSON
import org.apache.commons.collections.MultiMap
import org.apache.commons.collections.map.MultiValueMap
import org.codehaus.groovy.grails.web.json.JSONObject

import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

@Transactional(readOnly = true)
class ProxyController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond Proxy.list(params), model:[proxyInstanceCount: Proxy.count()]
    }

    def show(Proxy proxyInstance) {
        respond proxyInstance
    }

    def create() {
        respond new Proxy(params)
    }

    /**
     * What is input . . .
     * http://localhost:8080/apollo/proxy/request?request=http://golr.geneontology.org/solr/select?defType=edismax&qt=standard&indent=on&wt=json&rows=10&start=0&fl=*,score&facet=true&facet.mincount=1&facet.sort=count&json.nl=arrarr&facet.limit=25&fq=document_category:%22ontology_class%22&fq=source:(biological_process%20OR%20molecular_function%20OR%20cellular_component)&facet.field=annotation_class&facet.field=synonym&facet.field=alternate_id&q=gree*&qf=annotation_class%5E5&qf=annotation_class_label_searchable%5E5&qf=synonym_searchable%5E1&qf=alternate_id%5E1&json.wrf=jQuery1710751773979049176_1445036592600&_=1445036624006
     * http://golr.geneontology.org/solr/select
     * ?defType=edismax&
qt=standard&
indent=on&
wt=json&
rows=10&
start=0&
fl=*,score&
facet=true&
facet.mincount=1&
facet.sort=count&
json.nl=arrarr&
facet.limit=25&
fq=document_category:%22ontology_class%22&
fq=source:(biological_process%20OR%20molecular_function%20OR%20cellular_component)&
facet.field=annotation_class&
facet.field=synonym&
facet.field=alternate_id&
q=gree*&
qf=annotation_class%5E5&
qf=annotation_class_label_searchable%5E5&
qf=synonym_searchable%5E1&
qf=alternate_id%5E1&
json.wrf=jQuery1710751773979049176_1445036592600&
_=1445036624006

     // what is returned . .
     // MISSING "/solr/select"

     http://golr.geneontology.org
 ?json.wrf=jQuery17109697239506058395_1445105400207&
json=[nl:arrarr, wrf:jQuery17109697239506058395_1445105400207]&
facet=true&
indent=on&
facet.mincount=1&
facet.limit=25&
qf=[annotation_class^5, annotation_class_label_searchable^5, synonym_searchable^1, alternate_id^1]&
json.nl=arrarr&
wt=json&
defType=edismax&
rows=10&
fl=*,score&
start=0&
facet.sort=count&
q=red*&
_=1445105574330&
facet.field=[annotation_class, synonym, alternate_id]&
qt=standard&
fq=[document_category:"ontology_class", source:(biological_process OR molecular_function OR cellular_component)]&
protocol=http&
action=request&
controller=proxy&
returnType=solr&
url=golr.geneontology.org. Stacktrace follows:

     // what SHOULD be returned
     http://golr.geneontology.org/solr/select
 ?defType=edismax&
qt=standard&
indent=on&
wt=json&
rows=10&
start=0&
fl=*,score&
facet=true&
facet.mincount=1&
facet.sort=count&
json.nl=arrarr&
facet.limit=25&
fq=document_category:%22ontology_class%22&
fq=source:(biological_process%20OR%20molecular_function%20OR%20cellular_component)&
facet.field=annotation_class&
facet.field=synonym&
facet.field=alternate_id&
q=red*&
qf=annotation_class%5E5&
qf=annotation_class_label_searchable%5E5&
qf=synonym_searchable%5E1&
qf=alternate_id%5E1&
json.wrf=jQuery17101135514669585973_1445106124853&
_=1445106155048



     * @return
     */
    def request(String url){
        println "params: ${params}"
//        println "protocol: ${protocol}"
        println "requestUrl: ${url}"
//        println "returnType: ${returnType}"
        println "request URI: ${request.requestURI}"
//        http://localhost:8080/apollo/proxy/request/http%253A%252F%252Fgolr.geneontology.org%252Fsolr%252Fselect?defType=edismax&qt=standard&indent=on&wt=json&rows=10&start=0&fl=*,score&facet=true&facet.mincount=1&facet.sort=count&json.nl=arrarr&facet.limit=25&fq=document_category:%22ontology_class%22&fq=source:(biological_process%20OR%20molecular_function%20OR%20cellular_component)&facet.field=annotation_class&facet.field=synonym&facet.field=alternate_id&q=blood*&qf=annotation_class%5E5&qf=annotation_class_label_searchable%5E5&qf=synonym_searchable%5E1&qf=alternate_id%5E1&json.wrf=jQuery171043437046254985034_1445234103214&_=1445234283067

        String referenceUrl = URLDecoder.decode(url,"UTF-8")
        Proxy proxy = Proxy.findByReferenceUrl(referenceUrl)
        if(!proxy){
            log.error "No proxy found for ${referenceUrl}"
            response.status = 404
            return
        }
//        String targetUrl = proxy ? proxy.targetUrl : referenceUrl
//        request.parameterMap.each {
//            println "key: ${it.key} -> ${it.value}"
//        }

        String targetUrl = referenceUrl

        MultiMap multiMap =new MultiValueMap()

        params.eachWithIndex{ it, idx ->
            if(it.value instanceof String){
                multiMap.put(it.key,URLEncoder.encode(it.value,"UTF-8"))
            }
            else if(it.value instanceof HashMap){
{'fraction_non_alphanumeric': 0.12866576034181396, 'fraction_numerical': 0.07943289959215381, 'mean_word_length': 5.766754270696452, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '5042816', 'n_tokens_mistral': 4272, 'n_tokens_neox': 3736, 'n_words': 432}