#
# 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'
require 'rmobio/rxml/transformer_factory'
module Rmobio
module ClientInfo
#== checkClient
#=== The method checks the client type, version and family by analyzing the request header.
#=== HTTP_ACCEPT
#* If header contains application/pxml => 'xf' (xforms client)
#* If header contains wml or wap => 'wap'
#* If header contains applicaton/html => 'html'
#* Otherwise, default to html => 'html'
# If xf cient, the "POST" parameters are parsed to name/value pairs in params
#
#=== HTTP_CLIENT_INFO_URL
#
#* If header exists => 0.8 client
#* If header doesn't exist => 0.6 client
# Sample of what HTTP_CLIENT_INFO_URL returns:
# http://dev.getmobio.com/hub/devicecap/devices/capability?id=1.0.5:Motorola:V3m
#
#=== HTTP_MOBIO_AGENT
#* If header contains WM5 => Window Mobile family
#
#== Return Codes
# The method returns the following instance varables:
# @client => 'xf' or 'html' or 'wap'
# params[] => array contains name/value pairs of all parameters from POST request
# @paramsdoc => POST request in xml (parsed by hpricot)
# @xml => rxml transformer for the client type
# @client_version => '0.6' or '0.8'
#
def checkClient
logger.debug('checkClient http_accept header:')
logger.debug(request.env['HTTP_ACCEPT'])
if request.env['HTTP_MOBIO_AGENT'] or request.env['HTTP_ACCEPT']=~/application\/pxml/
@client="xf"
elsif (request.env['HTTP_ACCEPT']=~/application\/xhtml/)
headers['Content-Type']="text/html"
@client = "xhtml"
elsif ((request.env['HTTP_ACCEPT']=~/wml/) ||
(request.env['HTTP_ACCEPT']=~/wap/) )
@client="wap"
else
headers['Content-Type']="text/html"
@client="html"
end
# Get the rxml transformer for the client type
@xml = TransformerFactory::get_transformer(@client)
if @client == "xf"
headers.delete("Cache-Control")
headers['Content-Type']="application/mform"
@body = request.body.read.to_s if request.body
if request.env['REQUEST_METHOD'] == "POST" and @body
paramsFromXml(@body,'data')
end
end
# now check client version
#
@client_version = request.env['HTTP_CLIENT_INFO_URL']? '0.8' : '0.6'
# check if it's Window Mobile family
@client_family = request.env['HTTP_MOBIO_AGENT']=~/WM5/? 'wm': 'other'
# get user_id
@user_id=request.env['HTTP_USERID']
if (@user_id.nil?)
@user_id="101"
end
end
# creates params[] array from xml in argument xml
def paramsFromXml(xml,parentname)
@paramsdoc = Hpricot(xml)
if not parentname.nil?
doc = @paramsdoc.search("/" + parentname + "/*")
doc.each do |x|
params[(x.name).to_sym]= x.inner_html
end
end
end
#== getDeviceAttrValue(attribute, default)
#=== To get the value for a device attribute
# This utility queries the device capability service to get the value for
# a particular device attribute.
#
#=== Sample device url:
# http://dev.getmobio.com/hub/devicecap/devices/capability?id=1.0.5:Motorola:V3m
#=== Sample device attributes:
#
# .0.6.28:Sony-Ericsson:Z800
# Sony-Ericsson/Z800
# 220
# 176
# 0
# 1.5 MB
# 0
# 0
#
#
#=== parameter
#* attribute => the device attribute string, ex: "width", "height"
#* default => default value if attribute is not available in the client_info_url
#
#=== return
# Value of the attribute
#
def getDeviceAttrValue(attribute, default)
if request.env['HTTP_CLIENT_INFO_URL']
logger.debug 'HTTP_CLIENT_INFO_URL:'+request.env['HTTP_CLIENT_INFO_URL']
begin
doc = Hpricot.XML(open(request.env['HTTP_CLIENT_INFO_URL']))
value = doc.search("//mob:" + attribute).inner_html
if value and not value.empty?
default = value
end
rescue
# do nothing
end
end
default
end
#== get_device_info
#=== For asset management
#
#=== Sample device url:
# http://dev.getmobio.com/hub/devicecap/devices/capability?id=1.0.5:Motorola:V3m
#
# All client attributes are stored in Hash plus "iconsize" which is "13" or "17"
# If device width is null or 0, it defaults to '176' for asset to work.
#
def get_device_info
device={:iconsize=> "13"}
if request.env['HTTP_CLIENT_INFO_URL']
logger.debug 'HTTP_CLIENT_INFO_URL:'+request.env['HTTP_CLIENT_INFO_URL']
begin
doc = Hpricot.XML(open(request.env['HTTP_CLIENT_INFO_URL']))
doc.search("//mob:Client/*").each do |x|
if x.inner_html and not x.inner_html.empty?
# strip off "mob:"
name = x.name[4..-1]
device[name.to_sym] = x.inner_html
end
if x.name == "mob:fontsize"
if x.inner_html.to_i >=21
device[:iconsize] = "17"
end
end
end
rescue
# do nothing
end
end
# Make sure we always have a device width for asset to work
if device[:width].nil? or device[:width]==0
device[:width] = '176'
end
device
end #end get_device_info
end #end Device_Info
end #end Rmobio
include Rmobio::ClientInfo