Index: lams_tool_wookie/src/java/org/lamsfoundation/lams/tool/wookie/service/WookieService.java =================================================================== RCS file: /usr/local/cvsroot/lams_tool_wookie/src/java/org/lamsfoundation/lams/tool/wookie/service/WookieService.java,v diff -u -r1.3 -r1.4 --- lams_tool_wookie/src/java/org/lamsfoundation/lams/tool/wookie/service/WookieService.java 31 Jul 2009 04:37:20 -0000 1.3 +++ lams_tool_wookie/src/java/org/lamsfoundation/lams/tool/wookie/service/WookieService.java 5 Aug 2009 00:05:07 -0000 1.4 @@ -29,7 +29,9 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.util.Date; +import java.util.HashMap; import java.util.Hashtable; import java.util.List; import java.util.Set; @@ -64,6 +66,7 @@ import org.lamsfoundation.lams.tool.exception.DataMissingException; import org.lamsfoundation.lams.tool.exception.SessionDataExistsException; import org.lamsfoundation.lams.tool.exception.ToolException; +import org.lamsfoundation.lams.tool.service.ILamsToolService; import org.lamsfoundation.lams.tool.wookie.dao.IWookieAttachmentDAO; import org.lamsfoundation.lams.tool.wookie.dao.IWookieConfigItemDAO; import org.lamsfoundation.lams.tool.wookie.dao.IWookieDAO; @@ -77,11 +80,8 @@ import org.lamsfoundation.lams.tool.wookie.util.WookieConstants; import org.lamsfoundation.lams.tool.wookie.util.WookieException; import org.lamsfoundation.lams.tool.wookie.util.WookieToolContentHandler; -import org.lamsfoundation.lams.tool.service.ILamsToolService; +import org.lamsfoundation.lams.tool.wookie.util.WookieUtil; import org.lamsfoundation.lams.usermanagement.dto.UserDTO; -import org.lamsfoundation.lams.util.Configuration; -import org.lamsfoundation.lams.util.ConfigurationKeys; -import org.lamsfoundation.lams.util.FileUtil; import org.lamsfoundation.lams.util.MessageService; import org.lamsfoundation.lams.util.WebUtil; import org.lamsfoundation.lams.util.audit.IAuditService; @@ -212,6 +212,30 @@ fromContent = getDefaultContent(); } Wookie toContent = Wookie.newInstance(fromContent, toContentId, wookieToolContentHandler); + + String wookieUrl = getWookieURL(); + + try { + if (wookieUrl != null && fromContent.getWidgetIdentifier() != null && fromContent.getWidgetIdentifier() != "") { + + wookieUrl += WookieConstants.RELATIVE_URL_WIDGET_SERVICE; + HashMap params = new HashMap (); + + params.put(WookieConstants.PARAM_KEY_API_KEY, getWookieAPIKey()); + params.put(WookieConstants.PARAM_KEY_WIDGET_ID, fromContent.getWidgetIdentifier()); + params.put(WookieConstants.PARAM_KEY_PROPERTY_TEMPLATE_SHARED_KEY, fromContent.getToolContentId().toString()); + params.put(WookieConstants.PARAM_KEY_PROPERTY_CLONED_SHARED_KEY, toContentId.toString()); + + String xml = WookieUtil.getResponseStringFromExternalServer(wookieUrl, params); + + + } else { + throw new WookieException("Wookie url is not set"); + } + } catch (Exception e) { + logger.error("Problem calling wookie server to clone instance"); + throw new WookieException("Problem calling wookie server to clone instance", e); + } wookieDAO.saveOrUpdate(toContent); } Index: lams_tool_wookie/src/java/org/lamsfoundation/lams/tool/wookie/util/WookieConstants.java =================================================================== RCS file: /usr/local/cvsroot/lams_tool_wookie/src/java/org/lamsfoundation/lams/tool/wookie/util/WookieConstants.java,v diff -u -r1.3 -r1.4 --- lams_tool_wookie/src/java/org/lamsfoundation/lams/tool/wookie/util/WookieConstants.java 31 Jul 2009 04:17:01 -0000 1.3 +++ lams_tool_wookie/src/java/org/lamsfoundation/lams/tool/wookie/util/WookieConstants.java 5 Aug 2009 00:05:07 -0000 1.4 @@ -76,20 +76,23 @@ public static final String PARAM_KEY_REQUEST_ID = "requestid"; public static final String PARAM_KEY_API_KEY = "api_key"; public static final String PARAM_KEY_SERVICE_TYPE = "servicetype"; - public static final String PARAM_KEY_WIDGIT_ID = "widgetid"; + public static final String PARAM_KEY_WIDGET_ID = "widgetid"; public static final String PARAM_KEY_USER_ID = "userid"; public static final String PARAM_KEY_SHARED_DATA_KEY = "shareddatakey"; public static final String PARAM_KEY_PARTICIPANT_ID = "participant_id"; public static final String PARAM_KEY_PARTICIPANT_DISPLAY_NAME = "participant_display_name"; public static final String PARAM_KEY_PARTICIPANT_THUMBNAIL_URL = "participant_thumbnail_url"; public static final String PARAM_KEY_PROPERTY_NAME = "propertyname"; public static final String PARAM_KEY_PROPERTY_VALUE = "propertyvalue"; + public static final String PARAM_KEY_PROPERTY_TEMPLATE_SHARED_KEY = "templateSharedDataKey"; + public static final String PARAM_KEY_PROPERTY_CLONED_SHARED_KEY = "clonedSharedDataKey"; public static final String PARAM_VALUE_GET_WIDGET= "getwidget"; public static final String PARAM_VALUE_ADD_PARTICIPANT = "addparticipant"; public static final String PARAM_VALUE_SET_PERSONAL_PROPERTY = "setpersonalproperty"; public static final String PARAM_VALUE_PROPERTY_NAME_MODERATOR = "moderator"; public static final String PARAM_VALUE_PROPERTY_VALUE_TRUE = "true"; + public static final String PARAM_VALUE_PROPERTY_VALUE_CLONE = "cloneSharedData"; public static final String XML_WIDGETS = "widgets"; public static final String XML_WIDGET = "widget"; Index: lams_tool_wookie/src/java/org/lamsfoundation/lams/tool/wookie/util/WookieUtil.java =================================================================== RCS file: /usr/local/cvsroot/lams_tool_wookie/src/java/org/lamsfoundation/lams/tool/wookie/util/WookieUtil.java,v diff -u -r1.3 -r1.4 --- lams_tool_wookie/src/java/org/lamsfoundation/lams/tool/wookie/util/WookieUtil.java 31 Jul 2009 02:57:03 -0000 1.3 +++ lams_tool_wookie/src/java/org/lamsfoundation/lams/tool/wookie/util/WookieUtil.java 5 Aug 2009 00:05:07 -0000 1.4 @@ -28,20 +28,25 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map.Entry; +import javax.servlet.http.HttpServletResponse; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.apache.log4j.Logger; +import org.lamsfoundation.lams.tool.exception.ToolException; import org.lamsfoundation.lams.tool.wookie.dto.WidgetDefinition; import org.lamsfoundation.lams.tool.wookie.web.actions.AuthoringAction; import org.lamsfoundation.lams.usermanagement.dto.UserDTO; -import org.lamsfoundation.lams.util.WebUtil; import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; @@ -65,11 +70,8 @@ HashMap params = new HashMap(); params.put(WookieConstants.PARAM_ALL, WookieConstants.PARAM_TRUE); - // Make the request to the wookie server - InputStream is = WebUtil.getResponseInputStreamFromExternalServer(url, params); - // Reading the response from the wookie server - String xml = convertStreamToString(is); + String xml = getResponseStringFromExternalServer(url, params); if (xml == null) { logger.error("Got null xml from url: " + url); return null; @@ -123,10 +125,7 @@ params.put(WookieConstants.PARAM_ALL, WookieConstants.PARAM_TRUE); // Make the request to the wookie server - InputStream is = WebUtil.getResponseInputStreamFromExternalServer(url, params); - - // Reading the response from the wookie server - String xml = convertStreamToString(is); + String xml = getResponseStringFromExternalServer(url, params); if (xml == null) { logger.error("Got null xml from url: " + url); return 0; @@ -152,27 +151,28 @@ } HashMap params = new HashMap(); - params.put(WookieConstants.PARAM_KEY_WIDGIT_ID, URLEncoder.encode(widgetIdentifier, "UTF8")); + params.put(WookieConstants.PARAM_KEY_WIDGET_ID, URLEncoder.encode(widgetIdentifier, "UTF8")); params.put(WookieConstants.PARAM_KEY_USER_ID, URLEncoder.encode(user.getUserID().toString(), "UTF8")); params.put(WookieConstants.PARAM_KEY_SHARED_DATA_KEY, URLEncoder.encode(sharedDataKey, "UTF8")); - InputStream is = requestWookieServlet(url, apiKey, WookieConstants.PARAM_VALUE_GET_WIDGET, params); - String widgetXML = convertStreamToString(is); + params.put(WookieConstants.PARAM_KEY_API_KEY, URLEncoder.encode(apiKey, "UTF8")); + params.put(WookieConstants.PARAM_KEY_REQUEST_ID, URLEncoder.encode(WookieConstants.PARAM_VALUE_GET_WIDGET, "UTF8")); + + String widgetXML = getResponseStringFromExternalServer(url, params); String displayName = user.getFirstName() + " " + user.getLastName(); - // Add the participant - String participantAddedXML = addParticipant(url, apiKey, widgetIdentifier, user.getUserID().toString(), sharedDataKey, user.getUserID().toString(), displayName, null); - if (!checkCompleted(participantAddedXML)) { - throw new WookieException("Attempt to add participant failed: " + participantAddedXML); + boolean participantAdded = addParticipant(url, apiKey, widgetIdentifier, user.getUserID().toString(), sharedDataKey, user.getUserID().toString(), displayName, null); + if (!participantAdded) { + throw new WookieException("Attempt to add participant failed, check response code in logs"); } // If required, set the moderator property if (isModerator) { - String propertyAddedXML = addProperty(url, apiKey, widgetIdentifier, user.getUserID().toString(), sharedDataKey, user.getUserID().toString(), displayName, null, WookieConstants.PARAM_VALUE_PROPERTY_NAME_MODERATOR, WookieConstants.PARAM_VALUE_PROPERTY_VALUE_TRUE); - if (!checkCompleted(propertyAddedXML)) { - throw new WookieException("Attempt to add property failed: " + propertyAddedXML); + boolean propertyAdded = addProperty(url, apiKey, widgetIdentifier, user.getUserID().toString(), sharedDataKey, user.getUserID().toString(), displayName, null, WookieConstants.PARAM_VALUE_PROPERTY_NAME_MODERATOR, WookieConstants.PARAM_VALUE_PROPERTY_VALUE_TRUE); + if (!propertyAdded) { + throw new WookieException("Attempt to add property failed, check response code in logs"); } } @@ -195,15 +195,15 @@ * @throws Exception * @throws Exception */ - public static String addParticipant(String url, String apiKey, String widgetIdentifier, String userId, + public static boolean addParticipant(String url, String apiKey, String widgetIdentifier, String userId, String sharedDataKey, String participantId, String participantDisplayName, String participantThumbnailURL) throws Exception { if (url == null || apiKey == null || widgetIdentifier == null || userId == null || sharedDataKey == null) { throw new WookieException("Parameters missing in addParticipant call"); } HashMap params = new HashMap(); - params.put(WookieConstants.PARAM_KEY_WIDGIT_ID, URLEncoder.encode(widgetIdentifier, "UTF8")); + params.put(WookieConstants.PARAM_KEY_WIDGET_ID, URLEncoder.encode(widgetIdentifier, "UTF8")); params.put(WookieConstants.PARAM_KEY_USER_ID, URLEncoder.encode(userId, "UTF8")); params.put(WookieConstants.PARAM_KEY_SHARED_DATA_KEY, URLEncoder.encode(sharedDataKey, "UTF8")); params.put(WookieConstants.PARAM_KEY_PARTICIPANT_ID, URLEncoder.encode(participantId, "UTF8")); @@ -213,11 +213,18 @@ params.put(WookieConstants.PARAM_KEY_PARTICIPANT_THUMBNAIL_URL, URLEncoder.encode(participantThumbnailURL, "UTF8")); } + params.put(WookieConstants.PARAM_KEY_API_KEY, URLEncoder.encode(apiKey, "UTF8")); + params.put(WookieConstants.PARAM_KEY_REQUEST_ID, URLEncoder.encode(WookieConstants.PARAM_VALUE_ADD_PARTICIPANT, "UTF8")); - - InputStream is = requestWookieServlet(url, apiKey, WookieConstants.PARAM_VALUE_ADD_PARTICIPANT, params); - String xml = convertStreamToString(is); - return xml; + // Making the request and getting the response code + int responseCode = WookieUtil.getResponseCodeExternalServer(url, params); + + // Checking the response code + if (responseCode == HttpServletResponse.SC_OK || responseCode == HttpServletResponse.SC_CREATED) { + return true; + } else { + return false; + } } /** @@ -236,15 +243,15 @@ * @throws Exception * @throws Exception */ - public static String addProperty(String url, String apiKey, String widgetIdentifier, String userId, + public static boolean addProperty(String url, String apiKey, String widgetIdentifier, String userId, String sharedDataKey, String participantId, String participantDisplayName, String participantThumbnailURL, String propertyName, String propertyValue) throws Exception { if (url == null || apiKey == null || widgetIdentifier == null || userId == null || sharedDataKey == null) { throw new WookieException("Parameters missing in addProperty call"); } HashMap params = new HashMap(); - params.put(WookieConstants.PARAM_KEY_WIDGIT_ID, URLEncoder.encode(widgetIdentifier, "UTF8")); + params.put(WookieConstants.PARAM_KEY_WIDGET_ID, URLEncoder.encode(widgetIdentifier, "UTF8")); params.put(WookieConstants.PARAM_KEY_USER_ID, URLEncoder.encode(userId, "UTF8")); params.put(WookieConstants.PARAM_KEY_SHARED_DATA_KEY, URLEncoder.encode(sharedDataKey, "UTF8")); params.put(WookieConstants.PARAM_KEY_PARTICIPANT_ID, URLEncoder.encode(participantId, "UTF8")); @@ -256,78 +263,109 @@ } params.put(WookieConstants.PARAM_KEY_PROPERTY_NAME, URLEncoder.encode(propertyName, "UTF8")); params.put(WookieConstants.PARAM_KEY_PROPERTY_VALUE, URLEncoder.encode(propertyValue, "UTF8")); + params.put(WookieConstants.PARAM_KEY_API_KEY, URLEncoder.encode(apiKey, "UTF8")); + params.put(WookieConstants.PARAM_KEY_REQUEST_ID, URLEncoder.encode(WookieConstants.PARAM_VALUE_SET_PERSONAL_PROPERTY, "UTF8")); - InputStream is = requestWookieServlet(url, apiKey, WookieConstants.PARAM_VALUE_SET_PERSONAL_PROPERTY, params); - String xml = convertStreamToString(is); - return xml; + // Making the request and getting the response code + int responseCode = WookieUtil.getResponseCodeExternalServer(url, params); + + // Checking the response code + if (responseCode == HttpServletResponse.SC_OK || responseCode == HttpServletResponse.SC_CREATED) { + return true; + } else { + return false; + } } + /** - * All requests to the wooke servlet go through this method + * Makes a request to the specified url with the specified parameters and returns the response string * - * @param url - * @param apiKey - * @param requestId + * @param urlStr * @param params * @return - * @throws Exception + * @throws ToolException + * @throws IOException */ - public static InputStream requestWookieServlet(String url, String apiKey, String requestId, - HashMap params) throws Exception { - // Adding required params to hashmap - params.put(WookieConstants.PARAM_KEY_API_KEY, URLEncoder.encode(apiKey, "UTF8")); - params.put(WookieConstants.PARAM_KEY_REQUEST_ID, URLEncoder.encode(requestId, "UTF8")); - - return WebUtil.getResponseInputStreamFromExternalServer(url, params); + public static String getResponseStringFromExternalServer(String urlStr, HashMap params) + throws Exception { + InputStream is = getResponseInputStreamFromExternalServer(urlStr, params); + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + StringBuilder sb = new StringBuilder(); + String line = null; + while ((line = reader.readLine()) != null) { + sb.append(line + "\n"); + } + is.close(); + return sb.toString(); } - + + /** - * Checks the resulting xml for "completed" + * Makes a request to the specified url. Returns true if the expected status response code is returned * - * @param xml + * @param urlStr + * @param params * @return - * @throws ParserConfigurationException + * @throws ToolException * @throws IOException - * @throws SAXException - * @throws Exception */ - private static boolean checkCompleted(String xml) throws ParserConfigurationException, SAXException, IOException { - - if (xml != null) { - - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - DocumentBuilder db = dbf.newDocumentBuilder(); - Document document = db.parse(new InputSource(new StringReader(xml))); - NodeList result = document.getElementsByTagName(WookieConstants.XML_MESSAGE); - - if (result != null) { - Node resultNode = result.item(0); - if (resultNode != null && resultNode.getTextContent().equals(WookieConstants.XML_COMPLETED)) { - return true; - } - } - logger.error("Recieved bad result when calling wookie server: " + xml); - } - - return false; + public static int getResponseCodeExternalServer(String urlStr, HashMap params) + throws Exception { + URLConnection conn = getResponseUrlConnectionFromExternalServer(urlStr, params); + HttpURLConnection httpConn = (HttpURLConnection) conn; + + return httpConn.getResponseCode(); } - + + /** - * Get a string response from an input stream + * Makes a request to the specified url with the specified parameters and returns the response input stream * - * @param is + * @param urlStr + * @param params * @return + * @throws ToolException * @throws IOException */ - private static String convertStreamToString(InputStream is) throws IOException { - BufferedReader reader = new BufferedReader(new InputStreamReader(is)); - StringBuilder sb = new StringBuilder(); - String line = null; - while ((line = reader.readLine()) != null) { - sb.append(line + "\n"); + public static InputStream getResponseInputStreamFromExternalServer(String urlStr, HashMap params) + throws Exception { + URLConnection conn = getResponseUrlConnectionFromExternalServer(urlStr, params); + InputStream is = conn.getInputStream(); + return is; + } + + + /** + * Makes a request to the specified url with the specified parameters and returns the response URLConnection + * + * @param urlStr + * @param params + * @return + * @throws ToolException + * @throws IOException + */ + public static URLConnection getResponseUrlConnectionFromExternalServer(String urlStr, HashMap params) + throws Exception { + if (!urlStr.contains("?")) { + urlStr += "?"; } - is.close(); - return sb.toString(); + + for (Entry entry : params.entrySet()) { + urlStr += "&" + entry.getKey() + "=" + entry.getValue(); + } + + URL url = new URL(urlStr); + URLConnection conn = url.openConnection(); + if (!(conn instanceof HttpURLConnection)) { + logger.error("Fail to connect to external server though url: " + urlStr); + throw new Exception("Fail to connect to external server though url: " + urlStr); + } + + HttpURLConnection httpConn = (HttpURLConnection) conn; + logger.info("Response code was " + new Integer(httpConn.getResponseCode()).toString() + " for URL: " + urlStr); + + return conn; } } Index: lams_tool_wookie/src/java/org/lamsfoundation/lams/tool/wookie/web/actions/AuthoringAction.java =================================================================== RCS file: /usr/local/cvsroot/lams_tool_wookie/src/java/org/lamsfoundation/lams/tool/wookie/web/actions/AuthoringAction.java,v diff -u -r1.4 -r1.5 --- lams_tool_wookie/src/java/org/lamsfoundation/lams/tool/wookie/web/actions/AuthoringAction.java 31 Jul 2009 04:37:20 -0000 1.4 +++ lams_tool_wookie/src/java/org/lamsfoundation/lams/tool/wookie/web/actions/AuthoringAction.java 5 Aug 2009 00:05:07 -0000 1.5 @@ -230,24 +230,14 @@ String wookieUrl = wookieService.getWookieURL(); String wookieKey = wookieService.getWookieAPIKey(); - String wookieIdentifier = WebUtil.readStrParam(request, WookieConstants.PARAM_KEY_WIDGIT_ID); + String wookieIdentifier = WebUtil.readStrParam(request, WookieConstants.PARAM_KEY_WIDGET_ID); Long toolContentID = new Long(WebUtil.readLongParam(request, AttributeNames.PARAM_TOOL_CONTENT_ID)); - if (wookieUrl == null || wookieKey == null) { - // TODO: Forward to error, citing that wookie is not configured properly - } wookieUrl += WookieConstants.RELATIVE_URL_WIDGET_SERVICE; String returnXML = WookieUtil.getWidget(wookieUrl, wookieKey, wookieIdentifier, getUser(), toolContentID .toString(), true); - // Get object from xml - //XStream xstream = new XStream(); - //WidgetData data = (WidgetData)xstream.fromXML(returnXML); - - // retrieving Wookie with given toolContentID - //Wookie wookie = wookieService.getWookieByContentId(toolContentID); - this.writeResponse(response, "text/xml;charset=utf-8", "utf-8", returnXML); } catch (Exception e) {