Skip to content

Instantly share code, notes, and snippets.

@dindoliboon
Created May 6, 2016 20:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dindoliboon/2d0cc8cfbf2f0f018d25e6e3b3682452 to your computer and use it in GitHub Desktop.
Save dindoliboon/2d0cc8cfbf2f0f018d25e6e3b3682452 to your computer and use it in GitHub Desktop.
Generates JNLP file from an Oracle Forms application to use locally or on a web server. Allows Oracle Forms application to run without a web browser.
<#
Generates JNLP file from an Oracle Forms application.
Tested against an Oracle Forms 11g server.
Information from:
Jan Carlin's Blog - Forms and Java Web Start
https://web.archive.org/web/20081201161421/http://groundside.com/blog/JanCarlin.php
#>
#Requires -Version 3
Set-StrictMode -Version 3.0
function New-Jnlp
{
<#
.SYNOPSIS
Generates JNLP file from an Oracle Forms application.
.DESCRIPTION
This will generate a JNLP file based on information from a specified Oracle Forms application.
.PARAMETER OracleFormsUrl
URL of your Oracle Forms application.
.PARAMETER WebServerHostUrl
URL that will host the JNLP files.
If not provided, local JNLP files will be created.
.PARAMETER DownloadPath
Location to save the optional JNLP files to.
If not provided, the optional JNLP files will not be generated.
.EXAMPLE
$jnlpParams = @{
OracleFormsUrl = 'https://my-oracle-forms-webserver/forms/frmservlet?config=TEST'
DownloadPath = 'V:\save-jnlp'
}
[IO.File]::WriteAllLines('V:\save-jnlp\forms.jnlp', (New-Jnlp @jnlpParams))
Creates a JNLP for https://my-oracle-forms-webserver/forms/frmservlet?config=TEST, saves JNLP to V:\save-jnlp\forms.jnlp, downloads optional JNLP files to V:\save-jnlp.
.EXAMPLE
$jnlpParams = @{
OracleFormsUrl = 'https://my-oracle-forms-webserver/forms/frmservlet?config=TEST'
WebServerHostUrl = 'https://localhost/1.7-my-oracle-forms-webserver/'
}
New-Jnlp @jnlpParams
Creates a JNLP for https://my-oracle-forms-webserver/forms/frmservlet?config=TEST, with JNLP configured for https://localhost/1.7-my-oracle-forms-webserver/, display JNLP content to the screen.
.EXAMPLE
$jnlpParams = @{
OracleFormsUrl = 'https://my-oracle-forms-webserver/forms/frmservlet?config=TEST'
}
New-Jnlp @jnlpParams
Creates a JNLP for https://my-oracle-forms-webserver/forms/frmservlet?config=TEST, display JNLP content to the screen. All JARs must be signed with the same SSL certificate or an error will be displayed when launching the main application JNLP file.
.NOTES
In the generated JNLP file, replace the string '__REPLACE_WITH_RANDOM_ID__' with a time-based counter.
#>
[CmdletBinding()]
Param
(
[parameter(Mandatory=$true)]
[string]$OracleFormsUrl,
[string]$WebServerHostUrl,
[string]$DownloadPath
)
$oracleFormsUri = [System.Uri]$OracleFormsUrl
$javaVersion = ''
$jarFiles = @()
$codeBase = ''
$code = ''
$param = @{}
$codeBaseUrl = ''
$request = Invoke-WebRequest -Method 'Get' -Uri $OracleFormsUrl
$request.ParsedHtml.getElementsByTagName('param') | ForEach-Object {
$paramName = ''
$paramValue = ''
$_.attributes | Where-Object {
$_.specified -eq $true
} | ForEach-Object {
if ($_.nodeName -eq 'name')
{
$paramName = $_.nodeValue
}
if ($_.nodeName -eq 'value')
{
$paramValue = $_.nodeValue -replace '\.\d+&', '.__REPLACE_WITH_RANDOM_ID__&'
}
}
$param.Add($paramName, $paramValue)
if ($paramName -eq 'type')
{
$javaVersion = ($paramValue -split '=')[1]
}
if ($paramName -eq 'archive')
{
$jarFiles = $paramValue -split ','
}
if ($paramName -eq 'codebase')
{
$codeBase = $paramValue
}
if ($paramName -eq 'code')
{
$code = $paramValue
}
}
$codeBaseUrl = "$($oracleFormsUri.Scheme)://$($oracleFormsUri.Host):$($oracleFormsUri.Port)$($codeBase)"
$xmlSettings = New-Object -TypeName System.Xml.XmlWriterSettings
$xmlSettings.Indent = $true
$xmlSettings.IndentChars = ' ' * 4
$memoryStream = New-Object -TypeName System.IO.MemoryStream
$xmlWriter = [System.XMl.XmlTextWriter]::Create($memoryStream, $xmlSettings)
$xmlWriter.WriteStartElement('jnlp')
$xmlWriter.WriteAttributeString('spec', '1.0+')
$xmlWriter.WriteAttributeString('codebase', $codeBaseUrl)
$xmlWriter.WriteStartElement('information')
$xmlWriter.WriteElementString('title', $request.ParsedHtml.title)
$xmlWriter.WriteEndElement() # information
$xmlWriter.WriteStartElement('security')
$xmlWriter.WriteElementString('all-permissions', '')
$xmlWriter.WriteEndElement() # security
$xmlWriter.WriteStartElement('update')
$xmlWriter.WriteAttributeString('check', 'timeout')
$xmlWriter.WriteAttributeString('policy', 'always')
$xmlWriter.WriteEndElement() # update
$xmlWriter.WriteStartElement('resources')
$xmlWriter.WriteStartElement('java')
$xmlWriter.WriteAttributeString('version', $JavaVersion + ('+', '')[($JavaVersion -like '*+*')])
$xmlWriter.WriteEndElement() # java
$jarFiles | ForEach-Object {
if ($null -ne $DownloadPath -and $DownloadPath.Length -gt 0)
{
# Bypass same certificate JAR signing error by using either a local or hosted JNLP file.
[IO.File]::WriteAllLines((Join-Path -Path $DownloadPath -ChildPath "$($_).jnlp"), (New-JarXml -CodeBase $codeBaseUrl -Title $request.ParsedHtml.title -JavaVersion $javaVersion -JarFile $_))
$xmlWriter.WriteStartElement('extension')
$xmlWriter.WriteAttributeString('name', $_)
if ($null -ne $WebServerHostUrl -and $WebServerHostUrl.Length -gt 0)
{
# Use hosted JNLP file.
$xmlWriter.WriteAttributeString('href', "$($WebServerHostUrl)$($_).jnlp")
# Download JAR file. Not necessary for application to work.
# (New-Object System.Net.WebClient).DownloadFile("$($codeBaseUrl)/$($_)", (Join-Path -Path $DownloadPath -ChildPath $_))
}
else
{
# Use local JNLP file.
$xmlWriter.WriteAttributeString('href', "file://localhost/$($DownloadPath)\$($WebServerHostUrl)$($_).jnlp")
}
}
else
{
# Use original JAR files on server.
# All JAR files must be signed with the same certificate or an error message is displayed.
$xmlWriter.WriteStartElement('jar')
$xmlWriter.WriteAttributeString('href', $_)
}
$xmlWriter.WriteEndElement() # extension/jar
}
$xmlWriter.WriteEndElement() # resources
$xmlWriter.WriteStartElement('applet-desc')
$xmlWriter.WriteAttributeString('name', $request.ParsedHtml.title)
$xmlWriter.WriteAttributeString('main-class', $code)
$xmlWriter.WriteAttributeString('width', '1024')
$xmlWriter.WriteAttributeString('height', '768')
$param.Keys | ForEach-Object {
$xmlWriter.WriteStartElement('param')
$xmlWriter.WriteAttributeString('name', $_)
$xmlWriter.WriteAttributeString('value', $param.Item($_))
$xmlWriter.WriteEndElement() # param
}
$xmlWriter.WriteEndElement() # applet-desc
$xmlWriter.WriteEndElement() # jnlp
$xmlWriter.Flush()
$xmlWriter.Close()
[System.Text.Encoding]::UTF8.GetString($memoryStream.ToArray())
$memoryStream.Close()
}
function New-JarXml
{
[CmdletBinding()]
Param
(
[string]$CodeBase,
[string]$Title,
[string]$JavaVersion,
[string]$JarFile
)
$xmlSettings = New-Object -TypeName System.Xml.XmlWriterSettings
$xmlSettings.Indent = $true
$xmlSettings.IndentChars = ' ' * 4
$memoryStream = New-Object -TypeName System.IO.MemoryStream
$xmlWriter = [System.XMl.XmlTextWriter]::Create($memoryStream, $xmlSettings)
$xmlWriter.WriteStartElement('jnlp')
$xmlWriter.WriteAttributeString('spec', '1.0+')
$xmlWriter.WriteAttributeString('codebase', $CodeBase)
$xmlWriter.WriteStartElement('information')
$xmlWriter.WriteElementString('title', $Title)
$xmlWriter.WriteEndElement() # information
$xmlWriter.WriteStartElement('security')
$xmlWriter.WriteElementString('all-permissions', '')
$xmlWriter.WriteEndElement() # security
$xmlWriter.WriteStartElement('resources')
$xmlWriter.WriteStartElement('j2se')
$xmlWriter.WriteAttributeString('version', $JavaVersion + ('+', '')[($JavaVersion -like '*+*')])
$xmlWriter.WriteEndElement() # j2se
$xmlWriter.WriteStartElement('jar')
$xmlWriter.WriteAttributeString('href', $JarFile)
$xmlWriter.WriteAttributeString('size', '')
$xmlWriter.WriteAttributeString('main', 'false')
$xmlWriter.WriteEndElement() # jar
$xmlWriter.WriteEndElement() # resources
$xmlWriter.WriteElementString('component-desc', '')
$xmlWriter.WriteEndElement() # jnlp
$xmlWriter.Flush()
$xmlWriter.Close()
[System.Text.Encoding]::UTF8.GetString($memoryStream.ToArray())
$memoryStream.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment