/** Returns the list of ids the user is trying to remote publish. */ private List<String> getIdsToPush( List<String> assetIds, String _contentFilterDate, SimpleDateFormat dateFormat) throws ParseException, DotDataException { List<String> ids = new ArrayList<String>(); for (String _assetId : assetIds) { if (_assetId != null && !_assetId.trim().isEmpty()) { if (ids.contains(_assetId)) { continue; } // check for the categories if (_assetId.contains("user_") || _assetId.contains("users_")) { // Trying to publish users // If we are trying to push users a filter date must be available if (_assetId.contains("users_")) { Date filteringDate = dateFormat.parse(_contentFilterDate); // Get users where createdate >= ? List<String> usersIds = APILocator.getUserAPI().getUsersIdsByCreationDate(filteringDate, 0, -1); if (usersIds != null) { for (String id : usersIds) { ids.add("user_" + id); } } } else { ids.add(_assetId); } } else if (_assetId.equals("CAT")) { ids.add(_assetId); } else if (_assetId.contains(".jar")) { // Check for OSGI jar bundles ids.add(_assetId); } else { // if the asset is a folder put the inode instead of the identifier try { Folder folder = null; try { folder = APILocator.getFolderAPI().find(_assetId, getUser(), false); } catch (DotSecurityException e) { Logger.error( getClass(), "User: "******" does not have permission to access folder. Folder identifier: " + _assetId); } catch (DotDataException e) { Logger.info(getClass(), "FolderAPI.find(): Identifier is null"); } if (folder != null && UtilMethods.isSet(folder.getInode())) { ids.add(_assetId); } else { // if the asset is not a folder and has identifier, put it, if not, put the inode Identifier iden = APILocator.getIdentifierAPI().findFromInode(_assetId); if (!ids.contains(iden.getId())) { // Multiples languages have the same identifier ids.add(iden.getId()); } } } catch (DotStateException e) { ids.add(_assetId); } } } } return ids; }
/** * Updates the assets in the given bundle with the publish/expire dates and destination * environments and set them ready to be pushed * * @param request HttpRequest * @param response HttpResponse * @throws WorkflowActionFailureException If fails trying to Publish the bundle contents */ public void pushBundle(HttpServletRequest request, HttpServletResponse response) throws WorkflowActionFailureException, IOException { response.setContentType("text/plain"); try { PublisherAPI publisherAPI = PublisherAPI.getInstance(); // Read the form values String bundleId = request.getParameter("assetIdentifier"); String _contentPushPublishDate = request.getParameter("remotePublishDate"); String _contentPushPublishTime = request.getParameter("remotePublishTime"); String _contentPushExpireDate = request.getParameter("remotePublishExpireDate"); String _contentPushExpireTime = request.getParameter("remotePublishExpireTime"); String _iWantTo = request.getParameter("iWantTo"); String whoToSendTmp = request.getParameter("whoToSend"); List<String> whereToSend = Arrays.asList(whoToSendTmp.split(",")); List<Environment> envsToSendTo = new ArrayList<Environment>(); // Lists of Environments to push to for (String envId : whereToSend) { Environment e = APILocator.getEnvironmentAPI().findEnvironmentById(envId); if (e != null && APILocator.getPermissionAPI() .doesUserHavePermission(e, PermissionAPI.PERMISSION_USE, getUser())) { envsToSendTo.add(e); } } if (envsToSendTo.isEmpty()) { response.sendError(HttpServletResponse.SC_FORBIDDEN); return; } // Put the selected environments in session in order to have the list of the last selected // environments request.getSession().setAttribute(WebKeys.SELECTED_ENVIRONMENTS, envsToSendTo); // Clean up the selected bundle request.getSession().removeAttribute(WebKeys.SELECTED_BUNDLE); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-H-m"); Date publishDate = dateFormat.parse(_contentPushPublishDate + "-" + _contentPushPublishTime); Bundle bundle = APILocator.getBundleAPI().getBundleById(bundleId); APILocator.getBundleAPI().saveBundleEnvironments(bundle, envsToSendTo); if (_iWantTo.equals(RemotePublishAjaxAction.DIALOG_ACTION_PUBLISH)) { bundle.setPublishDate(publishDate); APILocator.getBundleAPI().updateBundle(bundle); publisherAPI.publishBundleAssets(bundle.getId(), publishDate); } else if (_iWantTo.equals(RemotePublishAjaxAction.DIALOG_ACTION_EXPIRE)) { if ((!"".equals(_contentPushExpireDate.trim()) && !"".equals(_contentPushExpireTime.trim()))) { Date expireDate = dateFormat.parse(_contentPushExpireDate + "-" + _contentPushExpireTime); bundle.setExpireDate(expireDate); APILocator.getBundleAPI().updateBundle(bundle); publisherAPI.unpublishBundleAssets(bundle.getId(), expireDate); } } else if (_iWantTo.equals(RemotePublishAjaxAction.DIALOG_ACTION_PUBLISH_AND_EXPIRE)) { if ((!"".equals(_contentPushExpireDate.trim()) && !"".equals(_contentPushExpireTime.trim()))) { Date expireDate = dateFormat.parse(_contentPushExpireDate + "-" + _contentPushExpireTime); bundle.setPublishDate(publishDate); bundle.setExpireDate(expireDate); APILocator.getBundleAPI().updateBundle(bundle); publisherAPI.publishAndExpireBundleAssets( bundle.getId(), publishDate, expireDate, getUser()); } } } catch (Exception e) { Logger.error(RemotePublishAjaxAction.class, e.getMessage(), e); response.sendError( HttpStatus.SC_INTERNAL_SERVER_ERROR, "Error Push Publishing Bundle: " + e.getMessage()); } }
/** * Generates an Unpublish bundle for a given bundle id operation (publish/unpublish) * * @param bundleId The Bundle id of the Bundle we want to generate * @param operation Download for publish or un-publish * @return The generated requested Bundle file * @throws DotPublisherException If fails retrieving the Bundle contents * @throws DotDataException If fails finding the system user * @throws DotPublishingException If fails initializing the Publisher * @throws IllegalAccessException If fails creating new Bundlers instances * @throws InstantiationException If fails creating new Bundlers instances * @throws DotBundleException If fails generating the Bundle * @throws IOException If fails compressing the all the Bundle contents into the final Bundle file */ @SuppressWarnings("unchecked") private Map<String, Object> generateBundle( String bundleId, PushPublisherConfig.Operation operation) throws DotPublisherException, DotDataException, DotPublishingException, IllegalAccessException, InstantiationException, DotBundleException, IOException { PushPublisherConfig pconf = new PushPublisherConfig(); PublisherAPI pubAPI = PublisherAPI.getInstance(); List<PublishQueueElement> tempBundleContents = pubAPI.getQueueElementsByBundleId(bundleId); List<PublishQueueElement> assetsToPublish = new ArrayList<PublishQueueElement>(); // all assets but contentlets for (PublishQueueElement c : tempBundleContents) { if (!c.getType().equals("contentlet")) { assetsToPublish.add(c); } } pconf.setDownloading(true); pconf.setOperation(operation); // all types of assets in the queue but contentlets are passed here, which are passed through // lucene queries pconf.setAssets(assetsToPublish); // Queries creation pconf.setLuceneQueries(PublisherUtil.prepareQueries(tempBundleContents)); pconf.setId(bundleId); pconf.setUser(APILocator.getUserAPI().getSystemUser()); // BUNDLERS List<Class<IBundler>> bundlers = new ArrayList<Class<IBundler>>(); List<IBundler> confBundlers = new ArrayList<IBundler>(); Publisher publisher = new PushPublisher(); publisher.init(pconf); // Add the bundles for this publisher for (Class clazz : publisher.getBundlers()) { if (!bundlers.contains(clazz)) { bundlers.add(clazz); } } // Create a new bundle id for this generated bundle String newBundleId = UUID.randomUUID().toString(); pconf.setId(newBundleId); File bundleRoot = BundlerUtil.getBundleRoot(pconf); // Run bundlers BundlerUtil.writeBundleXML(pconf); for (Class<IBundler> c : bundlers) { IBundler bundler = c.newInstance(); confBundlers.add(bundler); bundler.setConfig(pconf); BundlerStatus bundlerStatus = new BundlerStatus(bundler.getClass().getName()); // Generate the bundler bundler.generate(bundleRoot, bundlerStatus); } pconf.setBundlers(confBundlers); // Compressing bundle ArrayList<File> list = new ArrayList<File>(); list.add(bundleRoot); File bundle = new File(bundleRoot + File.separator + ".." + File.separator + pconf.getId() + ".tar.gz"); Map<String, Object> bundleData = new HashMap<String, Object>(); bundleData.put("id", newBundleId); bundleData.put("file", PushUtils.compressFiles(list, bundle, bundleRoot.getAbsolutePath())); return bundleData; }
/** * Adds to an specific given bundle a given asset. <br> * If the given bundle does not exist a new onw will be created with that name * * @param request HttpRequest * @param response HttpResponse */ public void addToBundle(HttpServletRequest request, HttpServletResponse response) throws IOException { PublisherAPI publisherAPI = PublisherAPI.getInstance(); String _assetId = request.getParameter("assetIdentifier"); String _contentFilterDate = request.getParameter("remoteFilterDate"); String bundleName = request.getParameter("bundleName"); String bundleId = request.getParameter("bundleSelect"); try { Bundle bundle; if (bundleId == null || bundleName.equals(bundleId)) { // if the user has a unsent bundle with that name just add to it bundle = null; for (Bundle b : APILocator.getBundleAPI() .getUnsendBundlesByName(getUser().getUserId(), bundleName, 1000, 0)) { if (b.getName().equalsIgnoreCase(bundleName)) { bundle = b; } } if (bundle == null) { bundle = new Bundle(bundleName, null, null, getUser().getUserId()); APILocator.getBundleAPI().saveBundle(bundle); } } else { bundle = APILocator.getBundleAPI().getBundleById(bundleId); } // Put the selected bundle in session in order to have last one selected request.getSession().setAttribute(WebKeys.SELECTED_BUNDLE, bundle); List<String> ids; if (_assetId.startsWith("query_")) { // Support for lucene queries String luceneQuery = _assetId.replace("query_", ""); List<String> queries = new ArrayList<String>(); queries.add(luceneQuery); ids = PublisherUtil.getContentIds(queries); } else { String[] _assetsIds = _assetId.split(","); // Support for multiple ids in the assetIdentifier parameter List<String> assetsIds = Arrays.asList(_assetsIds); ids = getIdsToPush(assetsIds, _contentFilterDate, new SimpleDateFormat("yyyy-MM-dd-H-m")); } Map<String, Object> responseMap = publisherAPI.saveBundleAssets(ids, bundle.getId(), getUser()); // If we have errors lets return them in order to feedback the user if (responseMap != null && !responseMap.isEmpty()) { // Error messages JSONArray jsonErrors = new JSONArray((ArrayList) responseMap.get("errorMessages")); // Prepare the Json response JSONObject jsonResponse = new JSONObject(); jsonResponse.put("errorMessages", jsonErrors.toArray()); jsonResponse.put("errors", responseMap.get("errors")); jsonResponse.put("total", responseMap.get("total")); // And send it back to the user response.getWriter().println(jsonResponse.toString()); } } catch (Exception e) { Logger.error(RemotePublishAjaxAction.class, e.getMessage(), e); response.sendError( HttpStatus.SC_INTERNAL_SERVER_ERROR, "Error Adding content to Bundle: " + e.getMessage()); } }
/** * Send to the publisher queue a list of assets for a given Operation (Publish/Unpublish) and * {@link Environment Environment} * * @param request HttpRequest * @param response HttpResponse * @throws WorkflowActionFailureException If fails adding the content for Publish * @see com.dotcms.publisher.business.PublisherQueueJob * @see Environment */ public void publish(HttpServletRequest request, HttpServletResponse response) throws IOException, WorkflowActionFailureException { try { PublisherAPI publisherAPI = PublisherAPI.getInstance(); // Read the form values String _assetId = request.getParameter("assetIdentifier"); String _contentPushPublishDate = request.getParameter("remotePublishDate"); String _contentPushPublishTime = request.getParameter("remotePublishTime"); String _contentPushExpireDate = request.getParameter("remotePublishExpireDate"); String _contentPushExpireTime = request.getParameter("remotePublishExpireTime"); String _contentFilterDate = request.getParameter("remoteFilterDate"); String _iWantTo = request.getParameter("iWantTo"); String whoToSendTmp = request.getParameter("whoToSend"); String forcePushStr = request.getParameter("forcePush"); boolean forcePush = (forcePushStr != null && forcePushStr.equals("true")); List<String> whereToSend = Arrays.asList(whoToSendTmp.split(",")); List<Environment> envsToSendTo = new ArrayList<Environment>(); // Lists of Environments to push to for (String envId : whereToSend) { Environment e = APILocator.getEnvironmentAPI().findEnvironmentById(envId); if (e != null) { envsToSendTo.add(e); } } // Put the selected environments in session in order to have the list of the last selected // environments request.getSession().setAttribute(WebKeys.SELECTED_ENVIRONMENTS, envsToSendTo); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-H-m"); Date publishDate = dateFormat.parse(_contentPushPublishDate + "-" + _contentPushPublishTime); List<String> ids; if (_assetId.startsWith("query_")) { // Support for lucene queries String luceneQuery = _assetId.replace("query_", ""); List<String> queries = new ArrayList<String>(); queries.add(luceneQuery); ids = PublisherUtil.getContentIds(queries); } else { String[] _assetsIds = _assetId.split(","); // Support for multiple ids in the assetIdentifier parameter List<String> assetsIds = Arrays.asList(_assetsIds); ids = getIdsToPush(assetsIds, _contentFilterDate, dateFormat); } // Response map with the status of the addContents operation (error messages and counts ) Map<String, Object> responseMap = null; if (_iWantTo.equals(RemotePublishAjaxAction.DIALOG_ACTION_PUBLISH) || _iWantTo.equals(RemotePublishAjaxAction.DIALOG_ACTION_PUBLISH_AND_EXPIRE)) { Bundle bundle = new Bundle(null, publishDate, null, getUser().getUserId(), forcePush); APILocator.getBundleAPI().saveBundle(bundle, envsToSendTo); responseMap = publisherAPI.addContentsToPublish(ids, bundle.getId(), publishDate, getUser()); } if (_iWantTo.equals(RemotePublishAjaxAction.DIALOG_ACTION_EXPIRE) || _iWantTo.equals(RemotePublishAjaxAction.DIALOG_ACTION_PUBLISH_AND_EXPIRE)) { if ((!"".equals(_contentPushExpireDate.trim()) && !"".equals(_contentPushExpireTime.trim()))) { Date expireDate = dateFormat.parse(_contentPushExpireDate + "-" + _contentPushExpireTime); Bundle bundle = new Bundle(null, publishDate, expireDate, getUser().getUserId(), forcePush); APILocator.getBundleAPI().saveBundle(bundle, envsToSendTo); responseMap = publisherAPI.addContentsToUnpublish(ids, bundle.getId(), expireDate, getUser()); } } // If we have errors lets return them in order to feedback the user if (responseMap != null && !responseMap.isEmpty()) { // Error messages JSONArray jsonErrors = new JSONArray((ArrayList) responseMap.get("errorMessages")); // Prepare the Json response JSONObject jsonResponse = new JSONObject(); jsonResponse.put("errorMessages", jsonErrors.toArray()); jsonResponse.put("errors", responseMap.get("errors")); jsonResponse.put("total", responseMap.get("total")); jsonResponse.put("bundleId", responseMap.get("bundleId")); // And send it back to the user response.getWriter().println(jsonResponse.toString()); } } catch (Exception e) { Logger.error(RemotePublishAjaxAction.class, e.getMessage(), e); response.sendError( HttpStatus.SC_INTERNAL_SERVER_ERROR, "Error Publishing Bundle: " + e.getMessage()); } }