public void deploy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String jar = request.getParameter("jar"); File from = new File(Config.CONTEXT.getRealPath("/WEB-INF/felix/undeployed/" + jar)); File to = new File(Config.CONTEXT.getRealPath("/WEB-INF/felix/load/" + jar)); from.renameTo(to); writeSuccess(response, "OSGI Bundle Loaded"); }
public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); FileItemIterator iterator = null; try { iterator = upload.getItemIterator(request); while (iterator.hasNext()) { FileItemStream item = iterator.next(); InputStream in = item.openStream(); if (item.getFieldName().equals("bundleUpload")) { String fname = item.getName(); if (!fname.endsWith(".jar")) { Logger.warn(this, "Cannot deplpy bundle as it is not a JAR"); writeError(response, "Cannot deplpy bundle as it is not a JAR"); break; } File to = new File(Config.CONTEXT.getRealPath("/WEB-INF/felix/load/" + fname)); FileOutputStream out = new FileOutputStream(to); IOUtils.copyLarge(in, out); IOUtils.closeQuietly(out); IOUtils.closeQuietly(in); } } } catch (FileUploadException e) { Logger.error(OSGIBaseAJAX.class, e.getMessage(), e); throw new IOException(e.getMessage(), e); } }
/* * This will return a path whether the file exists or not * (Websphere returns a null if the file does not exist, which throws a lot of NPEs) */ public static String getRealPath(String relativePath) { if (Config.CONTEXT == null) { Logger.fatal(FileUtil.class, "Config.CONTEXT not initialized with a servlet context, dying"); throw new DotStateException("Config.CONTEXT not initialized with a servlet context, dying"); } String ret = Config.CONTEXT.getRealPath(relativePath); if (ret != null) return ret; String base = Config.CONTEXT.getRealPath("/"); base = (base.lastIndexOf(File.separatorChar) == base.length() - 1) ? base.substring(0, base.lastIndexOf(File.separatorChar)) : base; relativePath = relativePath.replace('/', File.separatorChar); return base + relativePath; }
public static void removeContainerFile( Container container, Identifier identifier, boolean EDIT_MODE) { String folderPath = (!EDIT_MODE) ? "live" + java.io.File.separator : "working" + java.io.File.separator; String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT"); if (velocityRootPath.startsWith("/WEB-INF")) { velocityRootPath = Config.CONTEXT.getRealPath(velocityRootPath); } velocityRootPath += java.io.File.separator; String filePath = folderPath + identifier.getInode() + "." + Config.getStringProperty("VELOCITY_CONTAINER_EXTENSION"); java.io.File f = new java.io.File(velocityRootPath + filePath); f.delete(); DotResourceCache vc = CacheLocator.getVeloctyResourceCache(); vc.remove(ResourceManager.RESOURCE_TEMPLATE + filePath); }
public static InputStream buildVelocity( Container container, Identifier identifier, boolean EDIT_MODE) { InputStream result; StringBuilder sb = new StringBuilder(); boolean isDynamic = UtilMethods.isSet(container.getLuceneQuery()); // let's write this puppy out to our file sb.append("#set($SERVER_NAME =\"$host.getHostname()\" )"); sb.append("#set($CONTAINER_IDENTIFIER_INODE = '").append(identifier.getInode()).append("')"); sb.append("#set($CONTAINER_INODE = '").append(container.getInode()).append("')"); sb.append("#set($CONTAINER_MAX_CONTENTLETS = ") .append(container.getMaxContentlets()) .append(")"); Structure st = (Structure) InodeFactory.getInode(container.getStructureInode(), Structure.class); sb.append("#set($CONTAINER_STRUCTURE_NAME = \"") .append((UtilMethods.isSet(st.getName()) ? st.getName() : "")) .append("\")"); sb.append("#set($STATIC_CONTAINER = ") .append(!UtilMethods.isSet(container.getLuceneQuery())) .append(")"); sb.append("#set($SORT_PAGE = \"").append(container.getSortContentletsBy()).append("\")"); sb.append("#set($containerInode = '").append(container.getInode()).append("')"); if (EDIT_MODE) { // Permissions to read/use the container in order to be able to add content to it and reorder // content sb.append("#set($USE_CONTAINER_PERMISSION = $USE_CONTAINER_PERMISSION") .append(identifier.getInode()) .append(")"); // Permissions to edit the container based on write permission ).append( access to the portlet sb.append("#set($EDIT_CONTAINER_PERMISSION = $EDIT_CONTAINER_PERMISSION") .append(identifier.getInode()) .append(")"); // Permissions over the structure to add new contents sb.append("#set($ADD_CONTENT_PERMISSION = $ADD_CONTENT_PERMISSION") .append(identifier.getInode()) .append(")"); } sb.append("#set($CONTENTLETS = $contentletList").append(identifier.getInode()).append(")"); sb.append("#set($CONTAINER_NUM_CONTENTLETS = $totalSize") .append(identifier.getInode()) .append(")"); sb.append("#set($CONTAINER_NAME = \"") .append(UtilMethods.espaceForVelocity(container.getTitle())) .append("\")"); sb.append("#set($CONTAINER_STRUCTURE_NAME = \"") .append(UtilMethods.espaceForVelocity(st.getName())) .append("\")"); if (UtilMethods.isSet(container.getNotes())) sb.append("#set($CONTAINER_NOTES = \"") .append(UtilMethods.espaceForVelocity(container.getNotes())) .append("\")"); else sb.append("#set($CONTAINER_NOTES = \"\")"); /* * isDynamic means that the content list will be pulled from lucene. */ if (isDynamic) { String luceneQuery = container.getLuceneQuery(); sb.append("#set($CONTENTS_PER_PAGE = \"$CONTAINER_MAX_CONTENTLETS\")"); sb.append("#if($request.getParameter(\"cont_") .append(identifier.getInode()) .append("_per_page\"))"); sb.append("#set($CONTENTS_PER_PAGE = $request.getParameter(\"cont_") .append(identifier.getInode()) .append("_per_page\"))"); sb.append("#end"); sb.append("#set($CURRENT_PAGE = \"1\")"); sb.append("#if($request.getParameter(\"cont_") .append(identifier.getInode()) .append("_page\"))"); sb.append("#set($CURRENT_PAGE = $request.getParameter(\"cont_") .append(identifier.getInode()) .append("_page\"))"); sb.append("#end"); sb.append("#set($LUCENE_QUERY = \"").append(luceneQuery).append("\")"); } // if the container needs to get its contentlets if (container.getMaxContentlets() > 0) { sb.append("#if($EDIT_MODE)"); // To edit the look, see WEB-INF/velocity/static/preview/container_controls.vtl sb.append("<div class='dotContainer'>"); sb.append("#end"); // pre loop if it exists if (UtilMethods.isSet(container.getPreLoop())) { sb.append(container.getPreLoop()); } // let's do the search of contentlets using lucene query if (isDynamic) { Structure containerStructure = (Structure) InodeFactory.getInode(container.getStructureInode(), Structure.class); sb.append("#set($contentletResultsMap") .append(identifier.getInode()) .append(" = $contents.searchWithLuceneQuery(\"") .append(containerStructure.getInode()) .append("\", ") .append("\"$LUCENE_QUERY\", ") .append("\"$SORT_PAGE\", ") .append("$CURRENT_PAGE, $CONTENTS_PER_PAGE)) "); sb.append("#set($contentletList") .append(identifier.getInode()) .append(" = $contents.getContentIdentifiersFromLuceneHits($contentletResultsMap") .append(identifier.getInode()) .append(".get(\"assets\")))"); sb.append("#set($HAS_NEXT_PAGE = $contentletResultsMap") .append(identifier.getInode()) .append(".get(\"has_next_page\"))"); sb.append("#set($HAS_PREVIOUS_PAGE = $contentletResultsMap") .append(identifier.getInode()) .append(".get(\"has_previous_page\"))"); sb.append("#set($TOTAL_CONTENTS = $contentletResultsMap") .append(identifier.getInode()) .append(".get(\"total_records_int\"))"); sb.append("#set($TOTAL_PAGES = $contentletResultsMap") .append(identifier.getInode()) .append(".get(\"total_pages_int\"))"); sb.append("#set($CONTENTLETS = $contentletList").append(identifier.getInode()).append(")"); sb.append("#set($CONTAINER_NUM_CONTENTLETS = $totalSize") .append(identifier.getInode()) .append(")"); } sb.append("#foreach ($contentletId in $contentletList") .append(identifier.getInode()) .append(")"); // ##Checking of contentlet is parseable and not throwing errors if (EDIT_MODE) { sb.append("#if($webapi.canParseContent($contentletId,true))"); } // sb.append("\n#if($webapi.canParseContent($contentletId,"+EDIT_MODE+")) "); sb.append(" #set($_show_working_=false) "); // if timemachine future enabled sb.append(" #if($request.session.getAttribute(\"tm_date\")) "); sb.append( " #set($_tmdate=$date.toDate($webapi.parseLong($request.session.getAttribute(\"tm_date\")))) "); sb.append(" #set($_ident=$webapi.findIdentifierById($contentletId)) "); // if the content has expired we rewrite the identifier so it isn't loaded sb.append( " #if($UtilMethods.isSet($_ident.sysExpireDate) && $_tmdate.after($_ident.sysExpireDate))"); sb.append(" #set($contentletId='') "); sb.append(" #end "); // if the content should be published then force to show the working version sb.append( " #if($UtilMethods.isSet($_ident.sysPublishDate) && $_tmdate.after($_ident.sysPublishDate))"); sb.append(" #set($_show_working_=true) "); sb.append(" #end "); sb.append(" #end "); sb.append("#set($CONTENT_INODE = '')"); sb.append(" #if($contentletId != '') "); sb.append(" #getContentDetail($contentletId) "); sb.append(" #end "); sb.append("#if($CONTENT_INODE != '')"); if (!EDIT_MODE) { sb.append( "#set($_hasPermissionToViewContent = $contents.doesUserHasPermission($CONTENT_INODE, 1, $user, true))"); // ##Checking permission to see content sb.append("#if($_hasPermissionToViewContent)"); } String code = container.getCode(); // ### HEADER ### String startTag = "${contentletStart}"; if (!code.contains(startTag)) { sb.append("#if($EDIT_MODE)"); sb.append("<div class=\"dotContentlet\">"); // An empty div is added here because in Internet Explorer, there is a styling issue // http://jira.dotmarketing.net/browse/DOTCMS-1974 sb.append("<div>"); sb.append(" #end "); } else { String headerString = "#if($EDIT_MODE)" + "<div class=\"dotContentlet\">" + "<div>" + "#end "; code = code.replace(startTag, headerString); } // ### END HEADER ### // ### BODY ### String endTag = "${contentletEnd}"; boolean containsEndTag = code.contains(endTag); if (containsEndTag) { String footerString = "#if($EDIT_MODE && ${contentletId.indexOf(\".structure\")}==-1)" + "$velutil.mergeTemplate('static/preview_mode/content_controls.vtl')" + "#end" + "#if($EDIT_MODE)" + "<div class=\"dotClear\"></div></div>" + "#end "; code = code.replace(endTag, footerString); } sb.append("#if($isWidget == true)"); sb.append("$widgetCode"); sb.append(" #else"); sb.append(code); sb.append(" #end "); // The empty div added for styling issue in Internet Explorer is closed here // http://jira.dotmarketing.net/browse/DOTCMS-1974 sb.append("#if($EDIT_MODE)"); sb.append("</div>"); sb.append("#end "); // ### END BODY ### // ### FOOTER ### if (!containsEndTag) { sb.append("#if($EDIT_MODE && ${contentletId.indexOf(\".structure\")}==-1)"); sb.append("#getContentDetail($contentletId)"); sb.append("$velutil.mergeTemplate('static/preview_mode/content_controls.vtl')"); sb.append("#end "); sb.append("#if($EDIT_MODE) "); sb.append("<div class=\"dotClear\"></div></div>"); sb.append("#end "); } // ### END FOOTER ### if (!EDIT_MODE) { // ##End of checking permission to see content sb.append("#end "); } // ##Ends the inner canParse call sb.append("#end "); // ##Case the contentlet is not parseable and throwing errors if (EDIT_MODE) { sb.append("#else "); sb.append("#set($CONTENT_INODE =\"$webapi.getContentInode($contentletId)\")"); sb.append( "#set($EDIT_CONTENT_PERMISSION =\"$webapi.getContentPermissions($contentletId)\")"); sb.append("<div class=\"dotContentlet\">"); sb.append(" Content Parse Error. Check your Content Code. "); sb.append("$velutil.mergeTemplate('static/preview_mode/content_controls.vtl')"); sb.append("<div class=\"dotClear\"></div></div>"); sb.append("#end "); } // ##End of foreach loop sb.append("#end "); // post loop if it exists if (UtilMethods.isSet(container.getPostLoop())) { sb.append(container.getPostLoop()); } // close our container preview mode div sb.append("#if($EDIT_MODE)"); sb.append("$velutil.mergeTemplate('static/preview_mode/container_controls.vtl')"); sb.append("</div>"); sb.append("#end "); } else { sb.append(container.getCode()); } try { String folderPath = (!EDIT_MODE) ? "live" + File.separator : "working" + File.separator; String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT"); if (velocityRootPath.startsWith("/WEB-INF")) { velocityRootPath = Config.CONTEXT.getRealPath(velocityRootPath); } velocityRootPath += File.separator; String filePath = folderPath + identifier.getInode() + "." + Config.getStringProperty("VELOCITY_CONTAINER_EXTENSION"); if (Config.getBooleanProperty("SHOW_VELOCITYFILES", false)) { java.io.BufferedOutputStream tmpOut = new java.io.BufferedOutputStream( new java.io.FileOutputStream( new java.io.File( ConfigUtils.getDynamicVelocityPath() + File.separator + filePath))); // Specify a proper character encoding OutputStreamWriter out = new OutputStreamWriter(tmpOut, UtilMethods.getCharsetConfiguration()); out.write(sb.toString()); out.flush(); out.close(); tmpOut.close(); } } catch (Exception e) { Logger.error(ContentletServices.class, e.toString(), e); } try { result = new ByteArrayInputStream(sb.toString().getBytes("UTF-8")); } catch (UnsupportedEncodingException e1) { result = new ByteArrayInputStream(sb.toString().getBytes()); Logger.error(ContainerServices.class, e1.getMessage(), e1); } return result; }
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (DbConnectionFactory.getDBType().equals(DbConnectionFactory.MSSQL) && LicenseUtil.getLevel() < 299) { request.getRequestDispatcher("/portal/no_license.jsp").forward(request, response); return; } if (DbConnectionFactory.getDBType().equals(DbConnectionFactory.ORACLE) && LicenseUtil.getLevel() < 399) { request.getRequestDispatcher("/portal/no_license.jsp").forward(request, response); return; } if (!LicenseUtil.isASAllowed()) { request.getRequestDispatcher("/portal/no_license.jsp").forward(request, response); return; } Long profileTime = null; if (Config.getBooleanProperty("VELOCITY_PROFILING", false)) { profileTime = Calendar.getInstance().getTimeInMillis(); } try { // Check if the uri is a physical file. Fix for the cases when the // site configure VELOCITY_PAGE_EXTENSION as htm, html or any known // extension. // Example: // /html/js/tinymce/jscripts/tiny_mce/plugins/advlink/link.htm String uri = request.getRequestURI(); uri = URLDecoder.decode(uri, "UTF-8"); File file = new File(Config.CONTEXT.getRealPath(uri)); if (file.exists()) { FileInputStream fileIS = new FileInputStream(file); ServletOutputStream servletOS = response.getOutputStream(); int b; for (; -1 < (b = fileIS.read()); ) { servletOS.write(b); } fileIS.close(); servletOS.flush(); servletOS.close(); return; } // If we are at a directory, e.g. /home // we need to redirect to /home/ String forwardFor = (String) request.getRequestURL().toString(); if (request.getAttribute(Globals.MAPPING_KEY) == null && forwardFor != null && !forwardFor.endsWith("/") && !forwardFor.endsWith("." + Config.getStringProperty("VELOCITY_PAGE_EXTENSION"))) { // The query string parameters should be preserved as well String queryString = request.getQueryString(); response.sendRedirect( forwardFor + "/" + (UtilMethods.isSet(queryString) ? "?" + queryString : "")); return; } HttpSession session = request.getSession(false); boolean ADMIN_MODE = session != null && (session.getAttribute(com.dotmarketing.util.WebKeys.ADMIN_MODE_SESSION) != null); boolean PREVIEW_MODE = ADMIN_MODE && (session.getAttribute(com.dotmarketing.util.WebKeys.PREVIEW_MODE_SESSION) != null); boolean EDIT_MODE = ADMIN_MODE && (session.getAttribute(com.dotmarketing.util.WebKeys.EDIT_MODE_SESSION) != null); String value = request.getHeader("X-Requested-With"); if ((value != null) && value.equals("XMLHttpRequest") && EDIT_MODE && ADMIN_MODE) { ADMIN_MODE = false; } // ### VALIDATE ARCHIVE ### if ((EDIT_MODE || PREVIEW_MODE) && isArchive(request)) { PREVIEW_MODE = true; EDIT_MODE = false; request.setAttribute("archive", true); } // ### END VALIDATE ARCHIVE ### LanguageWebAPI langWebAPI = WebAPILocator.getLanguageWebAPI(); langWebAPI.checkSessionLocale(request); if (PREVIEW_MODE && ADMIN_MODE) { // preview mode has the left hand menu and edit buttons on the // working page Logger.debug(VelocityServlet.class, "VELOCITY SERVLET I'M ON PREVIEW MODE!!!"); doPreviewMode(request, response); } else if (EDIT_MODE && ADMIN_MODE) { // edit mode has the left hand menu and edit buttons on the // working page Logger.debug(VelocityServlet.class, "VELOCITY SERVLET I'M ON EDIT MODE!!!"); doEditMode(request, response); } else if (ADMIN_MODE) { // admin mode has the left hand menu and shows the live page in // the frame Logger.debug(VelocityServlet.class, "VELOCITY SERVLET I'M ON ADMIN MODE!!!"); doAdminMode(request, response); } else { // live mode has no frame and shows the live page Logger.debug(VelocityServlet.class, "VELOCITY SERVLET I'M ON LIVE MODE!!!"); doLiveMode(request, response); } } catch (ResourceNotFoundException rnfe) { // response.sendError(404); request.setAttribute(Constants.SERVE_URL, request.getRequestURI()); request.getRequestDispatcher("/localResourceServlet").forward(request, response); } catch (ParseErrorException pee) { Logger.error(this, "Template Parse Exception : " + pee.toString(), pee); try { response.sendError(500, "Template Parse Exception"); } catch (Throwable t) { Logger.error(this, t.getMessage(), t); PrintWriter out = response.getWriter(); out.println("Template Parse Exception"); out.println("On template:" + request.getRequestURI() + request.getQueryString()); } } catch (MethodInvocationException mie) { Logger.error(this, "MethodInvocationException" + mie.toString(), mie); try { response.sendError(500, "MethodInvocationException Error on template"); } catch (Throwable t) { Logger.error(this, t.getMessage(), t); PrintWriter out = response.getWriter(); out.println( "MethodInvocationException Error on template:" + request.getRequestURI() + request.getQueryString()); } } catch (Exception e) { Logger.error(this, e.toString(), e); try { response.sendError(500, "MethodInvocationException Error on template"); } catch (Throwable t) { Logger.error(this, t.getMessage(), t); PrintWriter out = response.getWriter(); out.println("Error on template:" + request.getRequestURI() + request.getQueryString()); } } finally { // catchall // added finally because of // http://jira.dotmarketing.net/browse/DOTCMS-1334 try { HibernateUtil.commitTransaction(); } catch (Exception e) { Logger.error(this, e.getMessage(), e); } DbConnectionFactory.closeConnection(); } if (profileTime != null) { profileTime = Calendar.getInstance().getTimeInMillis() - profileTime; VelocityProfiler.log( VelocityServlet.class, "VelocityPage time: " + request.getRequestURL() + " " + profileTime + " millis"); } }