High priority Reading and Saving homes on my web server using SweetHome3DApplet

This topic has been viewed 10963 times and has 2 replies
Thread status: Active
Questions about extending features and developing plug-ins [img]http://www.sweethome3d.com/images/flags/en_small.gif[/img]
Post Reply New Topic
User avatar
marchenar
Posts: 5
Joined: Mon Jul 11, 2011 10:59 am
Country: Bariloche, Rio Negro, Argentina
Contact:

Reading and Saving homes on my web server using SweetHome3DApplet

Post by marchenar »

Hello, first of all, the SweetHome3D is great!, so I want to thank you for this beatiful and useful software.

I am interested to embed the SweetHome3DApplet in a GWT web application that calculate energy consumption based on the devices users define for their houses (today we use a very basic 2D design that we had developed, but I think SH3D would be great to replace our 2D editor), so I compiled my oun applet extending the SweetHome3DApplet to interact with my GWT application using the GWTAI library. I could show the SH3D editor inside my GWT application and invoke applet methods, but I have the problem that I didn't find the way to read and write home files located on my web server.

Reading the API documentation I found that from HomeController the list of homes could be managed, but is it possible to open and save specific files without using the list?, that is files that exist in the web server.

On the other hand, looking at the SweetHome3D online source code, I've seen that you use listHomes.jsp, readHome.jsp, writeHome.jsp, readPreferences.jsp, and writePreferences.jsp as parameters for the SweetHome3DApplet, and I found a link to download php version of the first 3 files. Could you share the jsp version of these files?

Also, I tried to use the defaultHome parameter of the SweetHome3DApplet but it didn't work, have you any example of how to use it?

Thank you in advance, and sorry for my bad English...
Roberto
User avatar
Puybaret
Posts: 9440
Joined: Mon Nov 07, 2005 12:00 pm
Country: Paris, France
Contact:

Re: Reading and Saving homes on my web server using SweetHome3DApplet

Post by Puybaret »

Welcome to Sweet Home 3D developers community. I hope you'll give us a chance to try your software (if you can open source it of course). [:)]

Sorry if the description of the parameters of SweetHome3DApplet class isn't clear enough. I tried to make logical choices according to the values you give to readHomeURL, writeHomeURL, listHomesURL and defaultHome parameters, and here are the most typical cases I can think about.
First their default values defined as follows:

Code: Select all

readHomeURL="readHome.php?home=%s",
writeHomeURL="writeHome.php",
listHomesURL="listHomes.php",
defaultHome=""
shows to the end user and empty home at start (no set default home), but lets him save the edited one, creates new homes and open existing ones (all that with readHome.php?home=%s, writeHome.php, listHomes.php services ready on server side).

Then, the parameters defined as follows:

Code: Select all

readHomeURL="%s",
writeHomeURL="",
listHomesURL="",
defaultHome="myHome.sh3d"
will automatically open the file myHome.sh3d (relative to applet code base here) when the end user launches the applet, but won't let him save the changes or create a new home (New, Open, Save, Save as tool bar buttons won't be displayed).

If you want to let the end user save his changes, but still not let him create a new home from the applet, then you'll have to define writeHomeURL service, for example:

Code: Select all

readHomeURL="readHome.php?home=%s",
writeHomeURL="writeHome.php",
listHomesURL="",
defaultHome="12345"
In this example, only the Save tool bar button will be displayed, and the other buttons are not displayed because you would need listHomesURL service to check an existing service exists or not. I changed also readHomeURL value to show that you can use a dynamic service to read a SH3D stream depending on home ID for example (here "%s" will be replaced by "12345").

If you want to let the end user open any home but never save his changes, the parameters could be as follows:

Code: Select all

readHomeURL="%s",
writeHomeURL="",
listHomesURL="listHomes.php",
defaultHome=""
In this example, only the Open tool bar button will be displayed, and the other buttons are not displayed because you would need writeHomesURL service to save changes (the fact that the New button isn't shown is probably arguable in this case).
On the other hand, looking at the SweetHome3D online source code, I've seen that you use listHomes.jsp, readHome.jsp, writeHome.jsp, readPreferences.jsp, and writePreferences.jsp as parameters for the SweetHome3DApplet, and I found a link to download php version of the first 3 files. Could you share the jsp version of these files?
I won't give you here the exact source code of the JSP pages I use, because it's valuable only in the context of sweethome3d.com where some data is stored in a database and an identification is required to open/save the homes of a user. So I prefer to give here translations of the existing php files that work on files local to the JSP pages and let you adapt them for your needs.

Code: Select all

<%-- listHomes.jsp 
   
     Sweet Home 3D, Copyright (c) 2011 Emmanuel PUYBARET / eTeks <[email protected]>
   
     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 2 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, write to the Free Software
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
--%>
<%-- Returns the list of homes --%>
<%@ page import="java.io.*" %>
<%@ page contentType="text/plain; charset=UTF-8" %>
<% out.clear();
   String homesDir = application.getRealPath(".");
   for (File homeFile : new File(homesDir).listFiles()) {
     if (homeFile.isFile() && homeFile.getName().endsWith(".sh3d")) {
       out.print(homeFile.getName().substring(0, homeFile.getName().length() - 5) + "\n");
     }
   } %>

Code: Select all

<%-- writeHome.jsp 
   
     Sweet Home 3D, Copyright (c) 2011 Emmanuel PUYBARET / eTeks <[email protected]> 
   
     Distributed under the terms of the GNU General Public License 
--%>
<%-- Uploads the file available in multipart file "home", saves it
     in homes directory and returns "1" if save was successful
--%>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page contentType="text/plain; charset=UTF-8" %>
<% out.clear();
   if (ServletFileUpload.isMultipartContent(request)) {
     // Create a factory for disk-based file items
     DiskFileItemFactory factory = new DiskFileItemFactory();
     factory.setSizeThreshold(1024);
     // Create a new file upload handler for files 
     ServletFileUpload upload = new ServletFileUpload(factory);
     
     // Parse the request
     List items = upload.parseRequest(request);
     // Process the uploaded items
     Iterator iter = items.iterator();
     while (iter.hasNext()) {
       FileItem item = (FileItem)iter.next();
       
       if (!item.isFormField()) {
         String fieldName = item.getFieldName();
         if ("home".equals(fieldName)) {
           String homesDir = application.getRealPath(".");
           File homeFile = new File(homesDir, item.getName() + ".sh3d");

           item.write(homeFile);
           out.print("1");
           return;
         }
       }
     }
   }
   
   out.print("0");
%>

Code: Select all

<%-- readHome.jsp 
   
     Sweet Home 3D, Copyright (c) 2011 Emmanuel PUYBARET / eTeks <[email protected]>
   
     Distributed under the terms of the GNU General Public License 
--%>
<%@ page import="java.io.*" %>
<%@ page contentType="application/SweetHome3D" %>
<% out.clear();
   String homesDir = application.getRealPath(".");
   File homeFile = new File(homesDir, request.getParameter("home") + ".sh3d");
   response.setIntHeader("Content-length", (int)homeFile.length());
   response.setHeader("Content-Disposition", "attachment; filename=" + homeFile.getName());
   InputStream input = null;
   OutputStream output = response.getOutputStream();
   try {
     input = new FileInputStream(homeFile);
     byte [] buffer = new byte [8096];
     int size; 
     while ((size = input.read(buffer)) != -1) {
       output.write(buffer, 0, size);
     }
   } finally {
     if (input != null) {
       input.close();
     }
     if (output != null) {
       output.close();
     }
   } %>

Code: Select all

<%-- writePreferences.jsp 
   
     Sweet Home 3D, Copyright (c) 2011 Emmanuel PUYBARET / eTeks <[email protected]>
   
     Distributed under the terms of the GNU General Public License 
--%>
<%-- Saves the XML content of parameter named preferences
     in homes directory and returns "1" if save was successful
--%>
<%@ page import="java.io.*" %>
<%@ page contentType="text/plain; charset=UTF-8" %>
<% out.clear();
   String preferences = request.getParameter("preferences");
   if (preferences != null) {
     String homesDir = application.getRealPath(".");
     File preferencesFile = new File(homesDir, "preferences.xml");
     Writer writer = new OutputStreamWriter(new FileOutputStream(preferencesFile), "UTF-8"); 
     try {
       writer.write(preferences);
       out.print("1");
       return;
     } finally {
       if (out != null) {
         writer.close();
       }
     } 
   }
     
   out.print("0");
%>

Code: Select all

<%-- readPreferences.jsp
   
     Sweet Home 3D, Copyright (c) 2011 Emmanuel PUYBARET / eTeks <[email protected]>
   
     Distributed under the terms of the GNU General Public License 
--%>
<%-- Returns the preferences stored in preferences.xml file --%>
<%@ page import="java.io.*" %>
<%@ page contentType="application/xml; charset=UTF-8" %>
<% out.clear();
   String homesDir = application.getRealPath(".");
   File preferencesFile = new File(homesDir, "preferences.xml");
   if (preferencesFile.exists()) {
     response.setIntHeader("Content-length", (int)preferencesFile.length());
     response.setHeader("Content-Disposition", "attachment; filename=preferences.xml");
     InputStream input = null;
     OutputStream output = response.getOutputStream();
     try {
       input = new FileInputStream(preferencesFile);
       byte [] buffer = new byte [8096];
       int size; 
       while ((size = input.read(buffer)) != -1) {
         output.write(buffer, 0, size);
       }
     } finally {
       if (input != null) {
         input.close();
       }
       if (output != null) {
         output.close();
       }
     }    
   } %>
Hope this will be useful.
Emmanuel Puybaret, Sweet Home 3D creator
User avatar
marchenar
Posts: 5
Joined: Mon Jul 11, 2011 10:59 am
Country: Bariloche, Rio Negro, Argentina
Contact:

Re: Reading and Saving homes on my web server using SweetHome3DApplet

Post by marchenar »

Thank you very much Emmanuel for you fast response, this information is very useful to me.

Soon, when I had finish the prototype that I am writing now to check the integration with GWT libraries, I will share here in the forum the additions I did to the SweetHome3DApplet source files to embed the applet into a GWT application, and some fragments of code of the GWT application.

Also, when the new version of our appliation will be online I will send you a link to our application, so you could check how we use Sweet Home 3D inside our project.

Have a nice weekend,
Roberto
Post Reply New Topic

Who is online

Users browsing this forum: No registered users and 1 guest