package eu.clarin.cmdi.mdservice.action; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; import java.util.HashMap; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.xml.transform.TransformerException; import net.sf.json.JSON; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; //import net.sf.saxon.Controller; //import net.sf.saxon.event.MessageEmitter; import org.apache.struts2.interceptor.ServletRequestAware; import com.opensymphony.xwork2.ActionSupport; import eu.clarin.cmdi.mdservice.model.Diagnostics; /** * main Struts 2 controller * responds to requests (collections, model, recordset) * by dispatching the requests to appropriate internal methods and filling the inputStream with the result * * @author vronk * */ public class GenericProxyAction extends ActionSupport implements ServletRequestAware { private static final long serialVersionUID = 1L; private static JSONArray repositories = null; private static JSONObject wp_options = null; private Diagnostics diagnostics; /** * Properties to be filled by Struts with according request-parameters */ private String actionkey; private String q; private String squery; private String cache = Cache.USE; private String collection; private String columns; private String startItem; private String maximumItems; private String sort; private String lang="en"; private int maxdepth; private String format; private String options; private String actionContentType; private String userMsg; private String repository; //private Map session; private HttpServletRequest request; private long duration = 0; public GenericProxyAction(){ super(); initialize(); } public void setRepositoryByIndex(int id){ try { setRepositories(GenericProxyAction.createRepositories()); // static repositories net.sf.json.JSONArray json = getRepositories(); if (json.size() > 0){ repository = json.getJSONObject(id).getString("name"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoStylesheetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void initialize(){ //repository = 1; setRepositoryByIndex(0); } public Diagnostics getDiagnostics(){ return this.diagnostics; } public void setDiagnostics(Diagnostics diagnostics){ this.diagnostics = diagnostics; } public Diagnostics Diagnostics(){ if (diagnostics == null){ diagnostics = new Diagnostics(); } return this.diagnostics; } public String getQ() { return q; } public void setQ(String pq) { if (pq == null) pq=""; /* if (q != null){ if (q.trim().length() == 0){ q = null; } } */ this.q = pq; } public String getSquery() { return squery; } public void setSquery(String psquery) { if (psquery==null) psquery=""; this.squery = psquery; } public String getCache() { return cache; } public void setCache(String cache) { this.cache = cache; } public String getCollection() { return collection; } public void setCollection(String collection) { this.collection = collection; } //TODO defaults public String getColumns() { String cols = columns; /*if (columns == null){ cols = "Id,Name,Title"; } */ return cols; } public void setColumns(String columns) { this.columns = columns; } public int getMaxdepth() { return maxdepth; } public void setMaxdepth(int maxdepth) { this.maxdepth = maxdepth; } public String getFormat() { return format; } public void setFormat(String format) { this.format = format; } public String getOptions() { return options; } public void setOptions(String options) { this.options = options; } public String getActionContentType() { if (format.toLowerCase().startsWith("html")) { this.actionContentType = "text/html"; //this.actionContentType = "application/xhtml+xml"; } else { this.actionContentType = "text/xml"; } return actionContentType; } public void setActionContentType(String actionContentType) { this.actionContentType = actionContentType; } public void setRepository(String repository) { this.repository = repository; } public String getRepository() { //default repository = 1 (set on init) return repository; } public String getActionkey() { return actionkey; } public void setActionkey(String actionKey) { actionkey = actionKey; } public String getStartItem() { return startItem; } public void setStartItem(String startItem) { this.startItem = startItem; } public String getMaximumItems() { return maximumItems; } public void setMaximumItems(String maximumItems) { this.maximumItems = maximumItems; } public String getSort() { return sort; } public void setSort(String sort) { this.sort = sort; } public String getLang() { return lang; } public void setLang(String lang) { this.lang = lang; } public String getFullFormat() { return actionkey + "2" + format; } /** * primitive identification of the target-proxy * base for finding the right base_url in the props * subclass has to override with its specific proxykey * @return */ public String getProxyKey() { return ""; } public String getUserMsg() { return userMsg; } public void setUserMsg(String userMsg) { this.userMsg = this.userMsg + "\n" + userMsg; } @Override public void setServletRequest(HttpServletRequest arg0) { request = arg0; } public HttpServletRequest getServletRequest() { return request; } private InputStream resultStream; private InputStream sourceStream; /** * The stream holding the resulting data to be sent back to the user as response * @return */ public InputStream getResultStream() { return resultStream; } public void setResultStream(InputStream _resultStream){ resultStream = _resultStream; } public void setSourceStream(InputStream _sourceStream){ sourceStream = _sourceStream; } /* public InputStream getSourceStream() throws IOException { return getTargetRequest().openStream(); } */ public JSONArray getRepositories(){ return repositories; } public void setRepositories(JSONArray rep){ GenericProxyAction.repositories = rep; } public JSONObject getWPOptions(){ return wp_options; } public void setWPOptions(JSONObject op){ GenericProxyAction.wp_options = op; } protected URL base_url ; public URL getBaseURL() throws MalformedURLException { if (base_url == null) { base_url = new URL(getBaseURI());//Admin.getConfig().getProperty(getProxyKey() + ".uri")); } return base_url; } public String getBaseURI() { String uri =Admin.getConfig().getProperty(getProxyKey() + ".uri"); return uri; } public URL getTargetRequest() throws IOException { // URL targetURL =new URL( base_url, compname + ".xml"); URL targetURL = getBaseURL(); return targetURL; } /* public String getRequestKey() { String key=""; if (getActionkey()!=null) { key += getActionkey() + "//-" ; } if (getQ()!=null) { key += getQ() + "//-" ; } if (getCollection()!=null) { key += getCollection(); } return key; } */ public String getRequestKey() { String key=""; if (getActionkey()!=null) { key += getActionkey() + "//-" ; }else { key +="//-" ; } if (getQ()!=null) { key += getQ() + "//-" ; } else { key +="//-" ; } if (getCollection()!=null) { key += getCollection() + "//-"; } else { key +="//-" ; } if (getSquery()!=null) { key += getSquery() + "//-" ; } else { key +="//-" ; } if (getStartItem()!=null) { key += getStartItem() + "//-"; } else{ key +="//-" ; } if (getMaximumItems()!=null) { key += getMaximumItems() + "//-"; } else{ key +="//-" ; } key += getRepository() + "//-"; key += getMaxdepth() + "//-"; if (getLang()!=null) { key += getLang() + "//-"; }else{ key +="//-" ; } return key; } public InputStream getSourceStream() throws IOException, NoStylesheetException { return getTargetRequest().openStream(); // Admin.notifyUser(getProxyKey() + ".getSourceStream() - unable to open stream: " + getTargetRequest(); } public static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { is.reset(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return sb.toString(); } public static JSONArray createRepositories() throws IOException, InterruptedException, TransformerException, NoStylesheetException{ String path = Admin.getConfig().getProperty("workspaceprofile.path") + WorkspaceAction.WORKSPACE_FILENAME;//PROFILENAME_SERVER; File file=new File(path); InputStream in = new BufferedInputStream( new FileInputStream(path));; MDTransformer transformer = new MDTransformer(); transformer.setSrcFile(file.toURI().toURL()); transformer.setParams(MDTransformer.createParamsMap("xml2json")); InputStream jsonstream = transformer.transformXML(in); JSONObject json = JSONObject.fromObject(convertStreamToString(jsonstream)); JSONArray wparray = json.getJSONObject("Profiles").getJSONArray("WorkspaceProfiles"); JSONArray array = null; for (int i = 0; i < wparray.size(); ++i) { JSONObject wp = wparray.getJSONObject(i); if (wp.get("Repositories") != null){ array = wp.getJSONArray("Repositories"); break; } } // repository as static variable return array; // repository as session attribute /* HttpSession session = getServletRequest().getSession(); //JSONArray param = (JSONArray) session.getAttribute("repositories"); JSONArray param = new JSONArray(); param.add(array); session.setAttribute("repositories", param); */ } public static JSONObject createWPOptions() throws IOException, InterruptedException, TransformerException, NoStylesheetException{ String path = Admin.getConfig().getProperty("workspaceprofile.path") + WorkspaceAction.WORKSPACE_FILENAME;//PROFILENAME_SERVER; File file=new File(path); InputStream in = new BufferedInputStream( new FileInputStream(path));; MDTransformer transformer = new MDTransformer(); transformer.setSrcFile(file.toURI().toURL()); transformer.setParams(MDTransformer.createParamsMap("xml2json")); InputStream jsonstream = transformer.transformXML(in); JSONObject json = JSONObject.fromObject(convertStreamToString(jsonstream)); JSONArray wparray = json.getJSONObject("Profiles").getJSONArray("WorkspaceProfiles"); JSONObject obj = null; for (int i = 0; i < wparray.size(); ++i) { JSONObject wp = wparray.getJSONObject(i); if (wp.get("Options") != null){ obj = wp.getJSONObject("Options"); break; } } // options as static variable return obj; } public String getWPOption(String opt_key){ String option_val = null; if (wp_options == null ) { try { setWPOptions(GenericProxyAction.createWPOptions()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoStylesheetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // static options if (wp_options != null){ net.sf.json.JSONObject json = getWPOptions(); option_val = json.getString(opt_key); } return option_val; } public String getRepositoryPath(){ String repository_path = null; if (repositories == null ) { try { setRepositories(GenericProxyAction.createRepositories()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoStylesheetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // static repositories net.sf.json.JSONArray json = getRepositories(); for(int i=0;i createTransformerParams(){ HashMap hm = new HashMap(); if (getFullFormat() != null){ hm.put("format", getFullFormat()); } if (getColumns() != null){ hm.put("cols", getColumns()); } else { hm.put("cols", ""); } if (getStartItem() != null){ hm.put("startItem", getStartItem()); } if (getMaximumItems() != null){ hm.put("maximumItems", getMaximumItems()); } if (getLang() != null){ hm.put("lang", getLang()); } if (getQ() != null){ hm.put("q", getQ()); } //if (getRepository() != null){ hm.put("repository_name", String.valueOf(getRepository())); //} return hm; } public void prepare() throws Exception { String req_key = getRequestKey(); Admin.notifyUser("request_key: " + req_key); Admin.notifyUser(getProxyKey() + ".targetURL: " + getTargetRequest() + " .format:" + getFullFormat()); // Caching String xcid; // Admin.notifyUser("GPA.prepareSourceStream"); if (getCache().equals(Cache.SKIP)) { sourceStream = getSourceStream(); } else { if (getCache().equals(Cache.USE)) { sourceStream = Cache.getCache().getFromCache(req_key); } if (sourceStream == null) { // either not in cache or cache_flag=refresh //sourceStream = getTargetRequest().openStream(); long startMillis = System.currentTimeMillis(); sourceStream = getSourceStream(); duration = System.currentTimeMillis() - startMillis; req_key = addDurationKey(); long now = System.currentTimeMillis(); xcid = Cache.getCache().putInCache(req_key,sourceStream); //Admin.notifyUser("putting in cache: " + req_key); sourceStream = Cache.getCache().getFromCache(req_key); } /* else { Admin.notifyUser("reading from cache: " + req_key); } */ } if (format.equals("xml")) { resultStream = sourceStream; }else { // set URL as srcFile (for MDTransformer to pass to xsl-scripts) MDTransformer transformer = new MDTransformer(); transformer.setSrcFile(getTargetRequest()); // getColumns // Admin.notifyUser("GPA.getRepository, before xsl:" + getRepository()); transformer.setParams(createTransformerParams()); resultStream = transformer.transformXML(sourceStream);//, getFullFormat(), getColumns(), getStartRecord(), getMaximumRecords(),getLang(),getQ(),String.valueOf(getRepository())); } Admin.notifyUser(getProxyKey() + " success:" + (resultStream!=null)); } /** * default Action method */ public String execute() throws Exception { // HttpServletRequest request = ServletActionContext.getRequest(); //Admin.notifyUser("session-attrs:"); //Admin.notifyUser(getServletRequest().getRemoteUser() ); //Admin.notifyUser(String.valueOf(getSession())); long now = System.currentTimeMillis(); prepare(); long duration = (System.currentTimeMillis() - now); //duration if (resultStream == null) { return ERROR; } else { return SUCCESS; } } }