#
# Copyright (C) 2007 Mobio Networks, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
=begin
Some summary should go here
=end
#require 'rexml/document'
require 'open-uri'
require 'hpricot'
module Rmobio
module Utils
#== cachControl
#=== Adding cache control to response header
def cacheControl(cacheStr='priority=P3;max-age=604800')
headers.delete("Cache-Control");
headers["Cache-Control"] = cacheStr;
end
#== cacheloader
#=== A utility method to add cache control to images, styles and files
# To add a cache control, edit the configuration file 'cachehints.txt' with
# filename and header. Here is a ample configuration:
#
#
#logo.png: priority=P2;max-age=1296000
#
#base.rhtml: priority=P2;max-age=1296000
#
#
# The utility assumes base directory for images files in RAILS_ROOT/public/images
# and other files in RAILS_ROOT/apps/views.
#
#
# To access the image in your xform, use the following pattern:
#recipe/loader?name=logo.png
# To access the style in your xform, use the following pattern:
#
#
# where the controller 'recipe' should provide a method 'loader' that calls this
# utility:
# def loader
# cacheloader
# end
#
def cacheloader
if (name=params[:name]).nil?
# Can't do anything, just return empty content so we don't get
# 'no template' error.
render :text => ''
return
end
# Default cache configuration file
cache_hints = File::join RAILS_ROOT, 'public/cachehints.txt'
# Add cache header
headers.delete("Cache-Control")
if File.file?(cache_hints)
file = File.new(cache_hints,"r")
hintsTxt = file.read(File.size(cache_hints))
file.close()
hintsTxt.each_line do |line|
if line.index(name) == 0
line.scan(/(.*):(.*)/) do |garbage, ccheader|
headers["Cache-Control"] = ccheader.strip
end
break
end
end
end
if name=~ /.png$/
# for now hardcode the image directory
filePath = File::join RAILS_ROOT, 'public/images/', name
file = File.new(filePath,"r")
filecontents = file.read(File.size(filePath))
file.close()
headers["Content-Type"] = "image/png"
render :text => filecontents
return
elsif name =~ /.xfs$/ or name =~ /.rxf/
headers["Content-Type"] = "application/mform"
else
headers["Content-Type"] = "application/xml"
end
render :template => name
end
#== cacheimg
#=== A utility method to add cache control to images
#=== This utility is deprecated, use cacheloader instead
# To add a cache control for an image file in RAILS_ROOT/public/images, edit
# the configuration file "cachehints.txt with image name and header. Ex:
#
# logo.png: priority=P2;max-age=1296000
#
# To access the image in your xforms, use the following pattern:
# img?name=logo.png
# Where the controller should provide an img method that just call this cacheimg utility.
#
# Sample cachehints.txt:
# Skins_176A2-a.png: priority=P2;max-age=1296000
# logo.png: priority=P2;max-age=1296000
#
def cacheimg
public_folder = File::join RAILS_ROOT, "public/images"
filePath = File::join RAILS_ROOT, "public/images", params['name']
file = File.new(filePath,"r")
filecontents = file.read(File.size(filePath))
file.close()
headers["Content-Type"] = "image/png"
headers.delete("Cache-Control")
fileDir = File.dirname(filePath)
fileName = File.basename(filePath)
hints = File::join fileDir, "cachehints.txt"
if File.file?(hints)
file = File.new(hints,"r")
hintsTxt = file.read(File.size(hints))
file.close()
hintsTxt.each_line do |line|
if line.index(fileName) == 0
line.scan(/(.*):(.*)/) do |garbage, ccheader|
headers["Cache-Control"] = ccheader.strip
end
break
end
end
end
render :text => filecontents
end
#== backURL(key)
# This utility handles back url to GLP if page is cached.
# It expects params[:burl] or a session key and renders
# the following xml data back to the caller:
#
#
# blah blah url
#
#
# The returned instance data is determined by the following rules:
#* params[:burl] if parameter exists, the url in the session is also updated
#* session[:key] if params[:burl] is nil
#* empty string if none of the above
#
#
# To access the backurl from your xfroms, use the following pattern:
#
#
#
# Where the controller should provide a "storeBackurl" method that just call this backURL utility:
# def storeBackurl
# backURL("recipe_burl")
# end
#
#=== Parameter
# key => a unique session key to store the burl for the app
#
def backURL(key)
url = backurl_xml(key)
headers['Content-Type']='application/xml'
render :text => "" + url + ""
end # end backURLg
# if you want to just return partial backurl without headers and no rendering
def backurl_xml(key)
if params[:burl]
url = params[:burl].gsub(/&/,'&')
session[key] = url
elsif session[key]
url = session[key]
else
# don't know what to do, just return an empty url
url = ''
end
logger.debug('back url ' + key + ': ' + url)
"" + url + ""
end # end backurl_xml
def logHeaders
logger.debug( "Logging Headers...")
request.env.keys.each do |header|
logger.debug( header.to_s + ': ' + request.env[header].to_s)
end
logger.debug("End of Headers...")
end
end #end Utils
end #end Mobio