From YiPs Wiki (i powered)

XMLSERVICE: Rails Application

Under construction (notes)

Project

Apache configuration

e /www/apachedft/conf/httpd.conf

ENDTCPSVR SERVER(*HTTP) HTTPSVR(APACHEDFT)

bash-4.2$ e /www/apachedft/conf/httpd.conf &

# redirect /xmlrest to rails application
RewriteEngine On                  
RewriteRule ^/xmlrest(.*) http://127.0.0.1:4243/xmlrest/$1 [P]
ProxyPassReverse /xmlrest/ http://127.0.0.1:4243/xmlrest/ 

# xmlservice rest interface (xmlservice/xmlcgi.pgm)
ScriptAlias /cgi-bin/ /QSYS.LIB/XMLSERVICE.LIB/
<Directory /QSYS.LIB/XMLSERVICE.LIB/>
  AllowOverride None
  order allow,deny
  allow from all
  SetHandler cgi-script
  Options +ExecCGI
</Directory>

STRTCPSVR SERVER(*HTTP) HTTPSVR(APACHEDFT)

Starting xmlrest application

rails new xmlrest —skip-active-record —skip-bundle —skip-test-unit


bash-4.2$ rails new xmlrest --skip-active-record --skip-bundle --skip-test-unit
      create  
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/views/layouts/application.html.erb
      create  app/assets/images/.keep
      create  app/mailers/.keep
      create  app/models/.keep
      create  app/controllers/concerns/.keep
      create  app/models/concerns/.keep
      create  bin
      create  bin/bundle
      create  bin/rails
      create  bin/rake
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/filter_parameter_logging.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/initializers/session_store.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  db
      create  db/seeds.rb
      create  lib
      create  lib/tasks
      create  lib/tasks/.keep
      create  lib/assets
      create  lib/assets/.keep
      create  log
      create  log/.keep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/robots.txt
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/javascripts
      create  vendor/assets/javascripts/.keep
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.keep

e Gemfile

# Use xmlservice
gem 'xmlservice'

bundle install —local

bash-4.2$ bundle install --local
Using rake (10.1.0) 
Using i18n (0.6.5) 
Using minitest (4.7.5) 
Using multi_json (1.8.0) 
Using atomic (1.1.14) 
Using thread_safe (0.1.3) 
Using tzinfo (0.3.37) 
Using activesupport (4.0.0) 
Using builder (3.1.4) 
Using erubis (2.7.0) 
Using rack (1.5.2) 
Using rack-test (0.6.2) 
Using actionpack (4.0.0) 
Using mime-types (1.25) 
Using polyglot (0.3.3) 
Using treetop (1.4.15) 
Using mail (2.5.4) 
Using actionmailer (4.0.0) 
Using activemodel (4.0.0) 
Using activerecord-deprecated_finders (1.0.3) 
Using arel (4.0.0) 
Using activerecord (4.0.0) 
Using coffee-script-source (1.6.3) 
Using execjs (2.0.2) 
Using coffee-script (2.2.0) 
Using thor (0.18.1) 
Using railties (4.0.0) 
Using coffee-rails (4.0.1) 
Using hike (1.2.3) 
Using jbuilder (1.5.2) 
Using jquery-rails (3.0.4) 
Using json (1.8.0) 
Using bundler (1.3.5) 
Using tilt (1.4.1) 
Using sprockets (2.10.0) 
Using sprockets-rails (2.0.0) 
Using rails (4.0.0) 
Using rdoc (3.12.2) 
Using sass (3.2.11) 
Using sass-rails (4.0.0) 
Using sdoc (0.3.20) 
Using turbolinks (1.3.0) 
Using uglifier (2.2.1) 
Using xmlservice (1.2.0) 
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
bash-4.2$ 

e config.ru

map Rails.application.config.relative_url_root || "/" do
  run Rails.application
end

e config/application.rb

module Xmlrest
  class Application < Rails::Application
    # subdirectory htdocs/xmlrest
    config.relative_url_root = "/xmlrest"
  end
end

rails server -p 4243

bash-4.2$ rails server -p 4243
=> Ctrl-C to shutdown server
[2014-03-18 16:28:35] INFO  WEBrick::HTTPServer#start: pid=84257 port=4243

Welcome aboard

http://lp0364d:10022/xmlrest/
Welcome aboard
You're seeing this page because you're running 
in development mode and you haven't set a root route yet.

Routes are set up in config/routes.rb.

Ctrl-C to shutdown server

^C[2014-03-18 16:31:31] INFO  going to shutdown ...
[2014-03-18 16:31:31] INFO  WEBrick::HTTPServer#start done.
Exiting
bash-4.2$

rails generate controller welcome index

bash-4.2$ rails generate controller welcome index
      create  app/controllers/welcome_controller.rb
       route  get "welcome/index"
      invoke  erb
      create    app/views/welcome
      create    app/views/welcome/index.html.erb
      invoke  helper
      create    app/helpers/welcome_helper.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/welcome.js.coffee
      invoke    scss
      create      app/assets/stylesheets/welcome.css.scss

e config/routes.rb

Xmlrest::Application.routes.draw do
  get "welcome/index"
  root :to => 'welcome#index'
end

rails server -p 4243

bash-4.2$ rails server -p 4243
=> Ctrl-C to shutdown server
[2014-03-18 16:28:35] INFO  WEBrick::HTTPServer#start: pid=84257 port=4243

Welcome (xmlrest)

http://lp0364d:10022/xmlrest/
Welcome#index
Find me in app/views/welcome/index.html.erb

Asset pipeline already hard at work observed in terminal output rails server -p 4243

Started GET "/xmlrest//" for 9.10.111.222 at 2014-03-18 16:40:38 -0500
Processing by WelcomeController#index as HTML
  Rendered welcome/index.html.erb within layouts/application (4.4ms)
Completed 200 OK in 9026ms (Views: 9020.4ms)
Started GET "/xmlrest//assets/application.css?body=1" for 9.10.111.222 at 2014-03-18 16:40:47 -0500
Started GET "/xmlrest//assets/jquery_ujs.js?body=1" for 9.10.111.222 at 2014-03-18 16:40:47 -0500
Started GET "/xmlrest//assets/jquery.js?body=1" for 9.10.111.222 at 2014-03-18 16:40:47 -0500
Started GET "/xmlrest//assets/welcome.css?body=1" for 9.10.111.222 at 2014-03-18 16:40:47 -0500
Started GET "/xmlrest//assets/turbolinks.js?body=1" for 9.10.111.222 at 2014-03-18 16:40:47 -0500
Started GET "/xmlrest//assets/welcome.js?body=1" for 9.10.111.222 at 2014-03-18 16:40:47 -0500
Started GET "/xmlrest//assets/application.js?body=1" for 9.10.111.222 at 2014-03-18 16:40:47 -0500

Ctrl-C to shutdown server

^C[2014-03-18 16:31:31] INFO  going to shutdown ...
[2014-03-18 16:31:31] INFO  WEBrick::HTTPServer#start done.
Exiting
bash-4.2$

Login session encrypted password

rails generate controller login

bash-4.2$ rails generate controller login     
      create  app/controllers/login_controller.rb
      invoke  erb
      create    app/views/login
      invoke  helper
      create    app/helpers/login_helper.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/login.js.coffee
      invoke    scss
      create      app/assets/stylesheets/login.css.scss

e app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  # access helper methods (application_helper.rb)
  include ApplicationHelper
end

e app/helpers/application_helper.rb

module ApplicationHelper
  # current user
  def application_user
    session[:current_user_id]
  end
  # current pwd (encrypted)
  def application_pwd
    session[:current_user_pwd]
  end
  # encryption key (encrypt/decrypt key)
  def application_key
    session[:current_user_key]
  end
end

e app/controllers/login_controller.rb

class LoginController < ApplicationController
  # "Create" a login, aka "log the user in" 
  def create
    session[:current_user_id] = params[:user_id]
    session[:current_user_key] = ActiveXMLService::Base.generate_key()
    session[:current_user_pwd] = ActiveXMLService::Base.generate_password(params[:user_pwd],session[:current_user_key])
    flash[:notice] = "logged on" 
    redirect_to root_url
  end
  # "Delete" a login, aka "log the user out" 
  def destroy
    session[:current_user_id] = nil
    session[:current_user_key] = nil
    session[:current_user_pwd] = nil
    flash[:notice] = "logged out" 
    redirect_to root_url
  end
end

e app/views/welcome/index.html.erb

<% if !application_user -%>
  <%= form_tag "#{config.relative_url_root}/login/create" do %>
  <%= text_field_tag "user_id", 'DB2', size: 10 %>
  <%= password_field_tag "user_pwd", 'MYPWD', size: 10 %>
  <%= submit_tag "Login", :action => 'login' %>
  <% end %>
<% else -%>
  <%= form_tag "#{config.relative_url_root}/login/destroy" do %>
  <%= submit_tag "Logout", :action => 'logout' %>
  <% end %>
<% end -%>
<% if flash[:notice] -%>
  <p class="notice"><%= flash[:notice] %></p>
<% end -%>
<% if flash[:error] -%>
  <p class="error"><%= flash[:error] %></p>
<% end -%>

Extend login XMLSERVICE (any machine)

e app/models/ibmi.rb

class Ibmi
  # generated encrypt/decrypt key
  def self.generate_key
    ActiveXMLService::Base.generate_key()
  end
  # generate encrypted password
  def self.generate_password(pwd,key)
    ActiveXMLService::Base.generate_password(pwd,key)
  end
  # decrypt encrypted password (not recommended)
  def self.decrypt_password(pwd,key)
    XMLSERVICEPassword::Encrypt.decrypt(pwd, key)
  end

  # possible multiple constructors
  def initialize *args
    case args.size
    when 9
      initialize_xmlservice_rest *args
    when 11
      initialize_xmlservice_all *args
    else
      raise "Invalid number arguments for intitilization"
    end
  end
  # ctor all parms
  def initialize_xmlservice_all(type,database,user,pwd,key,size,ctl,ipc,install,url,head)
    if type.eql? 'REST'
      initialize_xmlservice_rest(url,database,user,pwd,key,size,ctl,ipc,head)
    else
      raise "Unknown connection type #{type}"
    end
  end
  # ctor xmlservice REST (Apache xmlcgi.pgm)
  def initialize_xmlservice_rest(url,database,user,pwd,key,size,ctl,ipc,head)
    ActiveXMLService::Base.establish_connection(
       :connection => url,
       :database => database,
       :username => user,
       :pwd_enc => pwd,
       :pwd_key => key,
       :ctl => ctl,
       :ipc => ipc,
       :size => size,
       :head => head)
  end
end

e app/controllers/login_controller.rb

class LoginController < ApplicationController
  # "Create" a login, aka "log the user in" 
  def create
    session[:current_user_id] = params[:user_id]
    session[:current_user_key] = Ibmi::generate_key
    session[:current_user_pwd] = Ibmi::generate_password(params[:user_pwd],session[:current_user_key])
    session[:current_user_xmlservice_connection_type] = 'REST'
    session[:current_user_xmlservice_database] = "*LOCAL"
    session[:current_user_xmlservice_output_size] = 15000000
    session[:current_user_xmlservice_install] = "XMLSERVICE"
    session[:current_user_xmlservice_rest_url] = params[:user_xmlservice_url]
    session[:current_user_xmlservice_rest_header] = "<?xml version='1.0'?>"
    session[:current_user_xmlservice_ipc] = "*none"
    session[:current_user_xmlservice_ctl] = "*here *cdata"
    flash[:notice] = "logged on" 
    redirect_to root_url
  end
  # "Delete" a login, aka "log the user out" 
  def destroy
    session[:current_user_id] = nil
    session[:current_user_key] = nil
    session[:current_user_pwd] = nil
    session[:current_user_xmlservice_connection_type] = nil
    session[:current_user_xmlservice_database] = nil
    session[:current_user_xmlservice_output_size] = nil
    session[:current_user_xmlservice_install] = nil
    session[:current_user_xmlservice_rest_url] = nil
    session[:current_user_xmlservice_rest_header] = nil
    session[:current_user_xmlservice_ipc] = nil
    session[:current_user_xmlservice_ctl] = nil
    flash[:notice] = "logged out" 
    redirect_to root_url
  end
end

e app/helpers/application_helper.rb

module ApplicationHelper
  # current user
  def application_user
    session[:current_user_id]
  end
  # current pwd (encrypted)
  def application_pwd
    session[:current_user_pwd]
  end
  # encryption key (encrypt/decrypt key)
  def application_key
    session[:current_user_key]
  end
  # current xmlservice connection type
  # 'REST' - xmlservice REST Apache (xmlcgi.pgm)
  # 'ibm_db' - xmlservice ibm_db connection (ruby)
  # 'ActiveRecord' - xmlservice ibm_db ActiveRecord connection (rails)
  def application_connection_type
    session[:current_user_xmlservice_connection_type]
  end
  # current xmlservice database
  def application_database
    session[:current_user_xmlservice_database]
  end
  # current xmlservice install library
  def application_xmlservice_install
    session[:current_user_xmlservice_install]
  end
  # current xmlservice output size
  def application_output_size
    session[:current_user_xmlservice_output_size]
  end
  # current xmlservice rest url
  def application_xmlservice_rest_url
    session[:current_user_xmlservice_rest_url]
  end
  # current xmlservice head
  def application_xmlservice_rest_head
    session[:current_user_xmlservice_rest_header]
  end
  # current xmlservice ipc (private connection)
  def application_xmlservice_ipc
    session[:current_user_xmlservice_ipc]
  end
  # current xmlservice ctl (control keywords)
  def application_xmlservice_ctl
    session[:current_user_xmlservice_ctl]
  end
  # current application statistics
  def application_statistics
    stat = "" + application_connection_type
    if application_connection_type.eql? 'REST'
      stat << " " + application_xmlservice_rest_url
    end
    stat << " ("
    stat << " " + application_database
    stat << ","
    stat << " " + application_user
    stat << ","
    stat << " " + application_xmlservice_ctl
    stat << ","
    stat << " " + application_xmlservice_ipc
    stat << ")"
    stat
  end
end

e app/views/welcome/index.html.erb

<% if !application_user -%>
  <%= form_tag "#{config.relative_url_root}/login/create" do %>
  <%= submit_tag "Login", :action => 'login' %>
  <%= text_field_tag "user_id", 'DB2', size: 10 %>
  <%= password_field_tag "user_pwd", 'MYPWD', size: 10 %>
  <%= text_field_tag "user_xmlservice_url", 'http://lp0364d:10022/cgi-bin/xmlcgi.pgm', size: 80 %>
  <% end %>
<% else -%>
  <%= form_tag "#{config.relative_url_root}/login/destroy" do %>
  <%= submit_tag "Logout", :action => 'logout' %>
  <%= application_statistics %>
  <% end %>
<% end -%>
<% if flash[:notice] -%>
  <p class="notice"><%= flash[:notice] %></p>
<% end -%>
<% if flash[:error] -%>
  <p class="error"><%= flash[:error] %></p>
<% end -%>

rails server -p 4243

bash-4.2$ rails server -p 4243
=> Ctrl-C to shutdown server
[2014-03-18 16:28:35] INFO  WEBrick::HTTPServer#start: pid=84257 port=4243

Welcome (xmlrest)

Login MYUID ****** http://lp0364d:10022/cgi-bin/xmlcgi.pgm
-- or --
Logout REST http://lp0364d:10022/cgi-bin/xmlcgi.pgm ( *LOCAL, DB2, *here *cdata, *none) 

Ctrl-C to shutdown server

^C[2014-03-18 16:31:31] INFO  going to shutdown ...
[2014-03-18 16:31:31] INFO  WEBrick::HTTPServer#start done.
Exiting
bash-4.2$

Pretty Welcome

Retrieved from http://youngiprofessionals.com/wiki/index.php/XMLSERVICE/RailsApplication
Page last modified on April 03, 2014, at 02:23 PM EST