Replacing CGI Script called from Java Applet

Last post 10-29-2009 8:48 AM by tux. 5 replies.

Sort Posts:

  • Replacing CGI Script called from Java Applet

    06-05-2009, 10:37 AM
    • Member
      10 point Member
    • tux
    • Member since 06-05-2009, 9:41 AM
    • Posts 27

    Question:

     I have a CAD image viewer applet coded in Java in my webpage.

    You can edit the CAD file, and save the result back to the server.

    The way this is accomplished is this: 

     The Java Applet calls a CGI script (Perl), and this script in turn saves the edits in a textfile at the appropriate location.

    I now want to replace the Perl Script by a ASP.NET page

    I've written a ASP.NET page below, which can get data send to it by http post, e.g. i type in http://localhost/testfolder/CGIreplacement.aspx?field1=abc&field2=cde&field3=ghi in the browser address field, then the CGIreplacement.aspx page returns field1=abc field2=cde etc.  as the page's content.

     

    But when I set the page as argument to the java applet, the page gets called, but NO fields can be read...

    I assume it has to do with the way the cgi script gets called. There are several possibilities, listed below:

    I use this method:

    <param name="save_redline_to_url" value="http://localhost/signedobfu/redsaver.cgi">
    <param name="save_redline_file_to_url" value="../drawing_redline.dwf">

    I figured I should probably use a non-TMS method, whatever that means.

    But why can't I get Input???

    SAVE REDLINES TO SERVER 
    Save with window dialog box displayed using TMS cgi script 
    <param name="redlinelocation" value="server">
    <param name="redline_cgi" value="http://localhost/signedobfu/redsaver.cgi">
    <param name="redline_file_path" value="../drawing_redlined.dwf">
    Save redline only to the server without popup dialog box using TMS cgi script or servlet (script example) 
    <param name="redlinelocation" value="server">(OPTIONAL)
    <param name="save_redline_to_url" value="http://localhost/signedobfu/redsaver.cgi">
    <param name="save_redline_file_to_url" value="../drawing_redline.dwf">
    Save redline only to the server without popup dialog box using TMS cgi script or servlet (servlet example) 
    <param name="redlinelocation" value="server">(OPTIONAL)
    <param name="save_redline_to_url" value="http://myserver/servlets/Redsaver">
    <param name="save_redline_file_to_url" value="c:/mydrawings/drawings_01/redlines/redline_01.red">
    Save redline only to the server without popup dialog box using non TMS cgi script 
    <param name="redlinelocation" value="server">(OPTIONAL)
    <param name="save_redline_to_url" value="http://localhost/signedobfu/yourscript.cgi?saveLocation=../drawing_redlines.dwf&saveURL=true&dn=demo&red=">
    Save redline in drawing to the server without popup dialog box using TMS cgi script 
    <param name="redlinelocation" value="server">(OPTIONAL)
    <param name="save_redline_to_url" value="http://localhost:8080/conversiontools/servlet/FileUploadServlet?saveLocation=../drawing_redlines1.dwf&redlinesdwf=">
    <param name="save_redline_in_drawing_as" value="redlinelayer">
    Save redline in drawing to the server without popup dialog box using non TMS cgi script 
    <param name="redlinelocation" value="server">(OPTIONAL)
    <param name="save_redline_to_url" value="../signedobfu/yourscript.cgi?saveLocation=../drawing_redlines.dwf&saveURL=true&dn=demo&red=">
    <param name="save_redline_in_drawing_as" value="redlinelayer">
    
     

     

     

     

     

    The CGI perl files contains this:

     

    #!/usr/local/bin/perl 
    #
    #***********************************************************
    #***********************************************************
    #  $location IS THE DIRECTORY OF THE REDLINE
    #C:\\Inetpub\\wwwroot\\cadviewer\\redlines\\
    #/cadviewer/redlines
    $location = "C:\\Inetpub\\wwwroot\\cadviewer\\redlines\\";
    #
    #***********************************************************
    
    
    
    my $logfile = "c:\\temp\\logfile.txt";
    open(LOGFILE , "&gt; " .  $logfile) ||  &sendNotice;
    print LOGFILE "Running cv7 redsaver\n";
    
    
    # Print out a content-type for HTTP/1.0 compatibility
    print "Content-type: text/html\n\n";
    #
    #test whether it's via a firewall (i.e. GET multiple times)
    # or direct, i.e. POST
    $method = $ENV{'REQUEST_METHOD'};
    if ($method eq "GET") {    
        $form_info = $ENV{'QUERY_STRING'};
    	print LOGFILE "Method found was: REQUEST_METHOD\n";
    }
    elsif ($method eq "POST"){
        # Get the input
        $data_size = $ENV{'CONTENT_LENGTH'};
        read(STDIN,$form_info,$data_size);
    	print LOGFILE "\nMethod found was: POST\n";
    }
    else {
        print "Client used unsupported method";
    	print LOGFILE "\nMethod found was: Client used unsupported method\n";
    }
    #***********************************************************
    #***********************************************************
    #   testsave.txt IS THE LOG FILE TO MAKE SURE THAT THIS SCRIPT HAS "WRITE" PERMISSION
    #   ON THE SERVER.  IF TESTSAVE.TXT CANNOT BE FOUND, THEN THIS SCRIPT CANNOT
    #   WRITE TO A FILE ON THIS SERVER ===> REDLINES CANNOT BE SAVED
    #
    #
    print LOGFILE "$form_info, datasize=$data_size";
    print LOGFILE "--------\n\n\n";
    #
    #
    #   END TESTING "WRITE" PRIVILEDGE ON THE SERVER
    #***********************************************************
    #
    # Split the name-value pairs
    @pairs = split(/&/, $form_info);
    
    
    foreach $pair (@pairs)
    {
         ($name, $value) = split(/=/, $pair);
    
    #     # Un-Webify plus signs and %-encoding
         $value =~ tr/+/ /;
         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
     
         # Uncomment for debugging purposes
         #print "Setting <I>$name</I> to <B>$value</B><P>";
    
         $FORM{$name} = $value; 
    #     print # "&lt;I>\$FORM{ ", $name," }</I> to <B>",$FORM{ '$name' },"&lt;/B><P>";
    print LOGFILE  "name=$name, value=$value \n";
    }
    
    # If the description is blank, then give a "blank form" response
    # &blank_response("Name:") unless $FORM{'your_name'};
    
    #$FORM{'name'} =~ s/ /_/g;
    
    # Now save data
    $saveURL = $FORM{'saveURL'};
    $location = "$location$FORM{'saveLocation'}";
    if($saveURL eq "true" )
    {
       $file_full = $FORM{'na'};
    #   $location =  "$baseDir$FORM{'saveLocation'}";
       
    }
    else
    {
       $file_first = join("_",$FORM{'dn'},$FORM{'na'});
    #   $file_full = join(".",$file_first,"red");
       $file_full = join("_",$FORM{'dn'},$FORM{'na'});
    
    }
    $file_name = join ("",$location,$file_full);
    #***********************************************************
    #***********************************************************
    #    FOR DEBUGING PURPOSE.  CAN BE REMOVED IF NECESSARY
    #
    print LOGFILE "1. dn=$FORM{'dn'} na=$FORM{'na'}\n";
    print LOGFILE "2. file_full=$file_full\n";
    print LOGFILE "3. filename=$file_name\n";
    print LOGFILE "4. urlFile=$FORM{'saveURL'}\n";
    print LOGFILE "\n\n";
    #
    #    END DEBUGGING
    #***********************************************************
    #
    #
    # Saving redline
    #my $redline_filename = "$file_name";
    #print LOGFILE "\nRedline: $redline_filename\n";
    #C:\\Inetpub\\wwwroot\\cadviewer\\redlines\\Firma\\redline_01.red
    #open(REDFILE , "&gt; " .  "C:\\Inetpub\\wwwroot\\cadviewer\\redlines\\Firma\\redline_01.red");
    $temp_file_name ="$file_name";
    #$temp_file_name =~ s/Inetpub\\wwwroot\\cadviewer\\redlines\\Firma/temp/; 
    print LOGFILE "$temp_file_name";
    open(REDFILE , "&gt; " .  "$temp_file_name") ||  &sendNotice;
    print REDFILE "$FORM{'red'}";
    close (REDFILE);
    #copy($temp_file_name, $file_name) or die "File cannot be copied.";
    print LOGFILE "\n\ncopy $temp_file_name $file_name\n\n";
    close (LOGFILE);
    #system("copy C:\temp\00_EG_Chur_1_05.red  C:\Inetpub\wwwroot\cadviewer\redlines\Firma\00_EG_Chur_1_05.red");
    #system( "copy $temp_file_name $file_name" );
    
    
    
    
    #print LOGFILE "Running cv7 redsaver\n";
    #
    #   END SAVING REDLINE FILE
    #***********************************************************
    #
    #
    #
    #***********************************************************
    #***********************************************************
    #   THIS PRINT "OK"  LINE IS REQUIRED.  CADVIEWER IS WAITING 
    #   FOR THIS LINE TO MAKE SURE THAT THE REDLINES ARE SAVED
    #
    print "ok";
    #
    #   PLEASE DO NOT REMOVE IT
    #***********************************************************
    #
    
    
    exit(0);
    
    sub sendNotice()
    {
           
    my $mailprog = '/usr/lib/sendmail';
    print <
    

    Error Saving Redliner

    TEST2 }
      

     

     

    --------------------------------------------

     

     The asp .net page i wrote looks like this

     

    &lt;%@ Page Language="VB" AutoEventWireup="false" CodeFile="CGIreplacement.aspx.vb" Inherits="java_CGIreplacement" %&gt;
    <%
        '  " save_redline_to_url=""/cadviewer/java/CGIreplacement.aspx""" & _
        Response.Write("@""HTTP/1.1 200 OK" & vbCrLf)
        Response.Write("Content-type: text/html" & vbCrLf)
        
    
        
        'Dim strSubmittedData As String = "dn=00_EG_Chur_1&saveLocation=Firma%5C00_EG_Chur_1_05.red&na=+&saveURL=true&red=p%2Cw%3D0%2C5%2C8268%2C26765%2C8268%2C2973%2C20045%2C2973%2C20045%2C26765%2C8268%2C26765%2C%7Bc%2C255%2C0%2C0%2C%7D%0AV%2C0%2C0%2C32000%2C32000%2CCurrView="
        Dim strParameters As String = ""
        
        
        
        Dim parampos As Integer = Request.RawUrl.IndexOf("?")
        
        Dim mioFile As New IO.StreamWriter("c:\temp\hah55aha.txt")
        mioFile.WriteLine(Request.RawUrl.ToString())
        mioFile.Close()
        
        
        
        
        
        Dim idictArguments As New System.Collections.Generic.Dictionary(Of String, String)()
        If parampos >= 0 Then
            strParameters = Request.RawUrl.Substring(parampos + 1)
            
            GetArguments(strParameters, idictArguments)
            'MsgBox(idictArguments.Item("red").ToString())
            'Response.Write(idictArguments.Item("red").ToString())
            Dim ioFile As New IO.StreamWriter("c:\temp\hah4aha.txt")
            ioFile.WriteLine("Hi There")
            'ioFile.WriteLine(idictArguments.Item("red").ToString())
            ioFile.Close()
            idictArguments = Nothing
        End If
        
        
        
        Dim Dict As IDictionary = Environment.GetEnvironmentVariables()
        Dim sb As StringBuilder = New System.Text.StringBuilder()
    
    
        For Each Item As DictionaryEntry In Dict
            sb.Append((DirectCast(Item.Key, String) & " - ") + DirectCast(Item.Value, String) & vbCr & vbLf)
        Next
    
    
        ' *** Read individual values
        Dim QueryString As String = Environment.GetEnvironmentVariable("QUERY_STRING")
        
        
        ' *** Read all the incoming form data both text and binary
        Dim FormData As String = ""
        Dim Data As Byte() = Nothing
        Dim form_info As String = ""
        If Environment.GetEnvironmentVariable("REQUEST_METHOD") = "GET" Then
            form_info = Environment.GetEnvironmentVariable("QUERY_STRING")
            ' print LOGFILE "Method found was: REQUEST_METHOD\n";
            
            
        ElseIf Environment.GetEnvironmentVariable("REQUEST_METHOD") = "POST" Then
            
            Dim clen As Integer = Context.Request.ContentLength
            Dim buf As Byte() = New Byte(clen - 1) {}
            Dim res As Integer = Context.Request.InputStream.Read(buf, 0, clen)
            form_info = ByteArray2String(buf)
            
            'Dim s As IO.Stream = Console.OpenStandardInput()
        
            'Dim br As New IO.BinaryReader(s)
        
            'Dim Length As String = Environment.GetEnvironmentVariable("CONTENT_LENGTH")
            'Dim Size As Integer = Int32.Parse(Length)
            'Data = New Byte(Size - 1) {}
            
            'br.Read(Data, 0, Size)
            ' *** don’t close the reader!
        
            'FormData = System.Text.Encoding.[Default].GetString(Data, 0, Size)
            'read(STDIN,$form_info,$data_size);
            '	print LOGFILE "\nMethod found was: POST\n";
        Else
            'print "Client used unsupported method";
            'print LOGFILE "\nMethod found was: Client used unsupported method\n";    
        End If
        
      
        Dim nioFile As New IO.StreamWriter("c:\temp\test.txt")
        nioFile.WriteLine("Hi hi again")
        nioFile.WriteLine(form_info)
        nioFile.WriteLine(FormData)
        nioFile.WriteLine(QueryString)
        nioFile.Close()
    %>
    
    

     

    using these functions

    Partial Class java_CGIreplacement
        Inherits System.Web.UI.Page
    
        'VB.NET to convert a byte array to a string.
        Public Shared Function ByteArray2String(ByRef baByteArrayToConvert As Byte()) As String
            Dim encText As New System.Text.ASCIIEncoding()
            Return encText.GetString(baByteArrayToConvert)
        End Function
    
        Sub GetArguments(ByRef strSubmittedData As String, ByRef idictParsedArguments As IDictionary)
            Dim strPairs As String() = strSubmittedData.Split("&"c)
            Dim strEachPair(2) As String
    
            For iIndex As Integer = 0 To strPairs.Length() - 1 Step 1
                If strPairs(iIndex) Is Nothing Then
                    strPairs(iIndex) = ""
                End If
    
                strEachPair = strPairs(iIndex).Split("="c)
    
                If strEachPair(0) Is Nothing Then
                    strEachPair(0) = ""
                End If
    
                If strEachPair(1) Is Nothing Then
                    strEachPair(1) = ""
                End If
                strEachPair(1) = System.Web.HttpUtility.UrlDecode(strEachPair(1))
                idictParsedArguments.Add(strEachPair(0), strEachPair(1))
            Next
        End Sub
    
    
    End Class
    
     
  • Re: Replacing CGI Script called from Java Applet

    06-09-2009, 9:50 AM
    • Member
      10 point Member
    • tux
    • Member since 06-05-2009, 9:41 AM
    • Posts 27

    Bump!

  • Re: Replacing CGI Script called from Java Applet

    06-10-2009, 4:55 PM
    • Member
      10 point Member
    • tux
    • Member since 06-05-2009, 9:41 AM
    • Posts 27
    bump.
  • Re: Replacing CGI Script called from Java Applet

    06-20-2009, 7:42 AM
    • Member
      10 point Member
    • tux
    • Member since 06-05-2009, 9:41 AM
    • Posts 27
    bump!
  • Re: Replacing CGI Script called from Java Applet

    07-14-2009, 8:32 AM
    • Member
      10 point Member
    • tux
    • Member since 06-05-2009, 9:41 AM
    • Posts 27

    tux:
    bump!
     

  • Re: Replacing CGI Script called from Java Applet

    10-29-2009, 8:48 AM
    Answer
    • Member
      10 point Member
    • tux
    • Member since 06-05-2009, 9:41 AM
    • Posts 27


    Solved using this code:

     Function readme() As String
        Dim sr As System.IO.StreamReader = New System.IO.StreamReader(Page.Request.InputStream())
        Return sr.ReadToEnd().Trim()
    End Function
    
    
    Sub WriteToFile(Optional ByRef strStringToWrite As String = "Hello World")
        Dim fp As System.IO.StreamWriter
    
        Try
            fp = System.IO.File.CreateText(Server.MapPath("./") & "test.txt")
            fp.WriteLine(strStringToWrite)
            Response.Write("File Succesfully created!")
            fp.Close()
        Catch err As Exception
            Response.Write("File Creation failed. Reason is as follows " + err.ToString())
        Finally
    
        End Try
    End Sub
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Response.Write(readme)
    
    
        WriteToFile(readme())
    
        'Dim p As String = Request.Form(0)
        'WriteToFile(p)
    
        'p = Request.Form(2)
        'WriteToFile(p)
    End Sub



Page 1 of 1 (6 items)