Java Code Examples for org.springframework.extensions.webscripts.Status#setCode()

The following examples show how to use org.springframework.extensions.webscripts.Status#setCode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UserCalendarEntriesGet.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req,
      Status status, Cache cache) 
{
   Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
   
   // Site is optional
   SiteInfo site = null;
   String siteName = templateVars.get("site");
   if (siteName != null)
   {
      site = siteService.getSite(siteName);
      
      // MNT-3053 fix, siteName was provided in request but it doesn't exists or user has no permissions to access it.
      if (site == null)
      {
          status.setCode(HttpServletResponse.SC_NOT_FOUND, "Site '" + siteName + "' does not exist or user has no permissions to access it.");
          return null;
      }
   }
   
   return executeImpl(site, null, req, null, status, cache);
}
 
Example 2
Source File: AlfrescoModelsDiff.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void setExceptionResponse(WebScriptRequest req, Status responseStatus, String responseMessage, int statusCode, Exception e)
{
    String message = responseMessage + req;

    if (logger.isDebugEnabled())
    {
        logger.warn(message, e);
    }
    else
    {
        logger.warn(message);
    }

    responseStatus.setCode(statusCode, message);
    responseStatus.setException(e);
}
 
Example 3
Source File: Login.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status)
{
    // extract username and password
    String username = req.getParameter("u");
    if (username == null || username.length() == 0)
    {
        throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Username not specified");
    }
    String password = req.getParameter("pw");
    if (password == null)
    {
        throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Password not specified");
    }

    try
    {
        return login(username, password);
    }
    catch(WebScriptException e)
    {
        status.setCode(e.getStatus());
        status.setMessage(e.getMessage());
        status.setRedirect(true);
        return null;
    }
}
 
Example 4
Source File: BulkImportStopWebScript.java    From alfresco-bulk-import with Apache License 2.0 6 votes vote down vote up
/**
 * @see org.springframework.extensions.webscripts.DeclarativeWebScript#executeImpl(org.springframework.extensions.webscripts.WebScriptRequest, org.springframework.extensions.webscripts.Status, org.springframework.extensions.webscripts.Cache)
 */
@Override
protected Map<String, Object> executeImpl(final WebScriptRequest request, final Status status, final Cache cache)
{
    Map<String, Object> result = new HashMap<>();

    cache.setNeverCache(true);
    
    if (importer.getStatus().inProgress())
    {
        result.put("result", "stop requested");
        importer.stop();
        status.setCode(Status.STATUS_ACCEPTED, "Stop requested.");
        status.setRedirect(true);  // Make sure the custom 202 status template is used (why this is needed at all is beyond me...)
    }
    else
    {
        result.put("result", "no imports in progress");
        status.setCode(Status.STATUS_BAD_REQUEST, "No bulk imports are in progress.");
    }
    
    return(result);
}
 
Example 5
Source File: BulkImportResumeWebScript.java    From alfresco-bulk-import with Apache License 2.0 6 votes vote down vote up
/**
 * @see DeclarativeWebScript#executeImpl(WebScriptRequest, Status, Cache)
 */
@Override
protected Map<String, Object> executeImpl(final WebScriptRequest request, final Status status, final Cache cache)
{
    Map<String, Object> result = new HashMap<>();

    cache.setNeverCache(true);
    
    if (importer.getStatus().isPaused())
    {
        result.put("result", "resume requested");
        importer.resume();
        status.setCode(Status.STATUS_ACCEPTED, "Resume requested.");
        status.setRedirect(true);  // Make sure the custom 202 status template is used (why this is needed at all is beyond me...)
    }
    else
    {
        result.put("result", "no imports in progress");
        status.setCode(Status.STATUS_BAD_REQUEST, "No bulk imports are in progress.");
    }
    
    return(result);
}
 
Example 6
Source File: ReplicationDefinitionDelete.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected Map<String, Object> buildModel(ReplicationModelBuilder modelBuilder, 
                                         WebScriptRequest req, Status status, Cache cache)
{
    // Which definition did they ask for?
    String replicationDefinitionName = 
       req.getServiceMatch().getTemplateVars().get("replication_definition_name");
    ReplicationDefinition replicationDefinition =
       replicationService.loadReplicationDefinition(replicationDefinitionName);
   
    // Does it exist?
    if(replicationDefinition == null) {
       throw new WebScriptException(
             Status.STATUS_NOT_FOUND, 
             "No Replication Definition found with that name"
       );
    }
    
    // Delete it
    replicationService.deleteReplicationDefinition(replicationDefinition);
    
    // Report that we have deleted it
    status.setCode(Status.STATUS_NO_CONTENT);
    status.setMessage("Replication Definition deleted");
    status.setRedirect(true);
    return null;
}
 
Example 7
Source File: CalendarEntryDelete.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected Map<String, Object> executeImpl(SiteInfo site, String eventName,
      WebScriptRequest req, JSONObject json, Status status, Cache cache) 
{
   CalendarEntry entry = calendarService.getCalendarEntry(
         site.getShortName(), eventName);
   
   if (entry == null)
   {
      status.setCode(Status.STATUS_NOT_FOUND);
      return null;
   }
   
   // Special case for "deleting" an instance of a recurring event 
   if (req.getParameter("date") != null && entry.getRecurrenceRule() != null)
   {
      // Have an ignored event generated
      createIgnoreEvent(req, entry);
      
      // Mark as ignored
      status.setCode(Status.STATUS_NO_CONTENT, "Recurring entry ignored");
      return null;
   }
   
   // Delete the calendar entry
   calendarService.deleteCalendarEntry(entry);
   
   // Record this in the activity feed
   addActivityEntry("deleted", entry, site, req, json);

   // All done
   status.setCode(Status.STATUS_NO_CONTENT, "Entry deleted");
   return null;
}
 
Example 8
Source File: LoginTicketDelete.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status)
{
    // retrieve ticket from request and current ticket
    String ticket = req.getExtensionPath();
    if (ticket == null || ticket.length() == 0)
    {
        throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Ticket not specified");
    }
    
    // construct model for ticket
    Map<String, Object> model = new HashMap<String, Object>(1, 1.0f);
    model.put("ticket",  ticket);
    
    try
    {
        String ticketUser = ticketComponent.validateTicket(ticket);

        // do not go any further if tickets are different
        if (!AuthenticationUtil.getFullyAuthenticatedUser().equals(ticketUser))
        {
            status.setCode(HttpServletResponse.SC_NOT_FOUND);
            status.setMessage("Ticket not found");
        }
        else
        {
            // delete the ticket
            authenticationService.invalidateTicket(ticket);
            status.setMessage("Deleted Ticket " + ticket);
        }
    }
    catch(AuthenticationException e)
    {
        status.setCode(HttpServletResponse.SC_NOT_FOUND);
        status.setMessage("Ticket not found");
    }

    status.setRedirect(true);
    return model;
}
 
Example 9
Source File: LoginTicket.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status)
{
    // retrieve ticket from request and current ticket
    String ticket = req.getExtensionPath();
    if (ticket == null || ticket.length() == 0)
    {
        throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Ticket not specified");
    }
    
    // construct model for ticket
    Map<String, Object> model = new HashMap<String, Object>(1, 1.0f);
    model.put("ticket",  ticket);
    
    try
    {
        String ticketUser = ticketComponent.validateTicket(ticket);
        
        String currentUser = AuthenticationUtil.getFullyAuthenticatedUser();

        // do not go any further if tickets are different 
        // or the user is not fully authenticated
        if (currentUser == null || !currentUser.equals(ticketUser))
        {
            status.setRedirect(true);
            status.setCode(HttpServletResponse.SC_NOT_FOUND);
            status.setMessage("Ticket not found");
        }
    }
    catch (AuthenticationException e)
    {
        status.setRedirect(true);
        status.setCode(HttpServletResponse.SC_NOT_FOUND);
        status.setMessage("Ticket not found");
    }
    
    return model;
}
 
Example 10
Source File: RunningActionDelete.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected Map<String, Object> buildModel(
      RunningActionModelBuilder modelBuilder, WebScriptRequest req,
      Status status, Cache cache) {
   // Which action did they ask for?
   String actionTrackingId = 
      req.getServiceMatch().getTemplateVars().get("action_tracking_id");

   // Check it exists
   ExecutionSummary action = 
      getSummaryFromKey(actionTrackingId);
   if(action == null) {
      throw new WebScriptException(
            Status.STATUS_NOT_FOUND, 
            "No Running Action found with that tracking id"
      );
   }
   
   ExecutionDetails details =
      actionTrackingService.getExecutionDetails(action);
   if(details == null) {
      throw new WebScriptException(
            Status.STATUS_NOT_FOUND, 
            "No Running Action found with that tracking id"
      );
   }
   
   // Request the cancel
   actionTrackingService.requestActionCancellation(action);
   
   // Report it as having been cancelled
   status.setCode(Status.STATUS_NO_CONTENT);
   status.setMessage("Action cancellation requested");
   status.setRedirect(true);
   return null;
}
 
Example 11
Source File: BulkImportPauseWebScript.java    From alfresco-bulk-import with Apache License 2.0 5 votes vote down vote up
/**
 * @see DeclarativeWebScript#executeImpl(WebScriptRequest, Status, Cache)
 */
@Override
protected Map<String, Object> executeImpl(final WebScriptRequest request, final Status status, final Cache cache)
{
    Map<String, Object> result = new HashMap<>();

    cache.setNeverCache(true);
    
    if (importer.getStatus().inProgress())
    {
        if (!importer.getStatus().isPaused())
        {
            result.put("result", "pause requested");
            importer.pause();
            status.setCode(Status.STATUS_ACCEPTED, "Pause requested.");
            status.setRedirect(true);  // Make sure the custom 202 status template is used (why this is needed at all is beyond me...)
        }
        else
        {
            result.put("result", "import is already paused");
            status.setCode(Status.STATUS_BAD_REQUEST, "Bulk import is already paused.");
        }
    }
    else
    {
        result.put("result", "no imports in progress");
        status.setCode(Status.STATUS_BAD_REQUEST, "No bulk imports are in progress.");
    }
    
    return(result);
}
 
Example 12
Source File: SiteFeedRetrieverWebScript.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status)
{
    // retrieve requested format
    String format = req.getFormat();
    if (format == null || format.length() == 0)
    {
        format = getDescription().getDefaultFormat();
    }
    
    String extensionPath = req.getExtensionPath();
    String[] extParts = extensionPath == null ? new String[1] : extensionPath.split("/");
    
    String siteId = null;
    if (extParts.length == 1)
    {
       siteId = extParts[0];
    }
    else
    {
        throw new AlfrescoRuntimeException("Unexpected extension: " + extensionPath);
    }
    
    // map feed collection format to feed entry format (if not the same), eg.
    //     atomfeed -> atomentry
    //     atom     -> atomentry
    if (format.equals("atomfeed") || format.equals("atom"))
    {
       format = "atomentry";
    }
    
    Map<String, Object> model = new HashMap<String, Object>();
    
    try
    {
        List<String> feedEntries = activityService.getSiteFeedEntries(siteId);
        
        
        if (format.equals(FeedTaskProcessor.FEED_FORMAT_JSON))
        { 
            model.put("feedEntries", feedEntries);
            model.put("siteId", siteId);
        }
        else
        {
            List<Map<String, Object>> activityFeedModels = new ArrayList<Map<String, Object>>();
            try
            { 
                for (String feedEntry : feedEntries)
                {
                    activityFeedModels.add(JSONtoFmModel.convertJSONObjectToMap(feedEntry));
                }
            }
            catch (JSONException je)
            {    
                throw new AlfrescoRuntimeException("Unable to get user feed entries: " + je.getMessage());
            }
            
            model.put("feedEntries", activityFeedModels);
            model.put("siteId", siteId);
        }
    }
    catch (AccessDeniedException ade)
    {
        // implies that site either does not exist or is private (and current user is not admin or a member) - hence return 401 (unauthorised)
        String currentUser = AuthenticationUtil.getFullyAuthenticatedUser();
        status.setCode(Status.STATUS_UNAUTHORIZED);
        logger.warn("Unable to get site feed entries for '" + siteId + "' (site does not exist or is private) - currently logged in as '" + currentUser +"'");
        
        model.put("feedEntries", null);
        model.put("siteId", "");
    }
    
    return model;
}
 
Example 13
Source File: LinksPost.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
protected Map<String, Object> executeImpl(SiteInfo site, String linkName,
      WebScriptRequest req, JSONObject json, Status status, Cache cache) 
{
   final ResourceBundle rb = getResources();
   Map<String, Object> model = new HashMap<String, Object>();
   
   // Get the new link details from the JSON
   String title;
   String description;
   String url;
   boolean internal;
   List<String> tags;

   // Fetch the main properties
   title = getOrNull(json, "title");
   description = getOrNull(json, "description");
   url = getOrNull(json, "url");
   
   // Handle internal / not internal
   internal = json.containsKey("internal");
   
   // Do the tags
   tags = getTags(json);
   
   
   // Create the link
   LinkInfo link;
   try
   {
      link = linksService.createLink(site.getShortName(), title, description, url, internal);
   }
   catch (AccessDeniedException e)
   {
      String message = "You don't have permission to create a link";
      
      status.setCode(Status.STATUS_FORBIDDEN);
      status.setMessage(message);
      model.put(PARAM_MESSAGE, rb.getString(MSG_ACCESS_DENIED));
      return model;
   }
   
   // Set the tags if required
   if (tags != null && tags.size() > 0)
   {
      link.getTags().addAll(tags);
      linksService.updateLink(link);
   }
   
   // Generate an activity for the change
   addActivityEntry("created", link, site, req, json);

   
   // Build the model
   model.put(PARAM_MESSAGE, link.getSystemName()); // Really!
   model.put(PARAM_ITEM, renderLink(link));
   model.put("node", link.getNodeRef());
   model.put("link", link);
   model.put("site", site);
   model.put("siteId", site.getShortName());
   
   // All done
   return model;
}
 
Example 14
Source File: BulkImportUIWebScript.java    From alfresco-bulk-import with Apache License 2.0 4 votes vote down vote up
/**
 * @see org.springframework.extensions.webscripts.DeclarativeWebScript#executeImpl(org.springframework.extensions.webscripts.WebScriptRequest, org.springframework.extensions.webscripts.Status, org.springframework.extensions.webscripts.Cache)
 */
@Override
protected Map<String, Object> executeImpl(final WebScriptRequest request, final Status status, final Cache cache)
{
    Map<String, Object> result = null;
    
    if (importer.getStatus().inProgress())
    {
        // If an import is already in progress, redirect to the status Web Script
        status.setCode(Status.STATUS_MOVED_TEMPORARILY);
        status.setRedirect(true);
        status.setLocation(request.getServiceContextPath() + WEB_SCRIPT_URI_BULK_IMPORT_STATUS);
    }
    else
    {
        // Construct the list of available bulk import sources
        Map<String, BulkImportSource> bulkImportSources = importer.getBulkImportSources();
        
        if (bulkImportSources != null && bulkImportSources.size() > 0)
        {
            List<Map<String, String>> sources = new ArrayList<Map<String, String>>(bulkImportSources.size());
            
            for (final String beanId : bulkImportSources.keySet())
            {
                BulkImportSource    source      = bulkImportSources.get(beanId);
                Map<String, String> sourceAsMap = new HashMap<String, String>();
                
                sourceAsMap.put("name",               source.getName());
                sourceAsMap.put("description",        source.getDescription());
                sourceAsMap.put("beanId",             beanId);
                sourceAsMap.put("configWebScriptURI", source.getConfigWebScriptURI());
                
                sources.add(sourceAsMap);
            }
            
            result = new HashMap<>();
            result.put("sources", sources);
        }
    }
    
    return(result);
}
 
Example 15
Source File: DeleteSpaceWebScript.java    From alfresco-bulk-import with Apache License 2.0 4 votes vote down vote up
/**
 * @see org.springframework.extensions.webscripts.DeclarativeWebScript#executeImpl(org.springframework.extensions.webscripts.WebScriptRequest, org.springframework.extensions.webscripts.Status, org.springframework.extensions.webscripts.Cache)
 */
@Override
protected Map<String, Object> executeImpl(final WebScriptRequest request, final Status status, final Cache cache)
{
    Map<String, Object> result        = null;
    String              targetPath    = null;
    NodeRef             targetNodeRef = null;
    
    cache.setNeverCache(true);
    
    try
    {
        targetPath = request.getParameter(PARAMETER_TARGET_PATH);

        if (targetPath == null || targetPath.trim().length() == 0)
        {
            throw new RuntimeException("Error: parameter '" + PARAMETER_TARGET_PATH + "' was not provided.");
        }
        
        targetPath    = targetPath.trim();
        targetNodeRef = convertPathToNodeRef(serviceRegistry, targetPath);
        
        fastDeleteSpace(targetPath, targetNodeRef);
        
    }
    catch (final WebScriptException wse)
    {
        throw wse;
    }
    catch (final FileNotFoundException fnfe)
    {
        throw new WebScriptException(Status.STATUS_NOT_FOUND, "The path " + targetPath + " does not exist.", fnfe);
    }
    catch (final Throwable t)
    {
        throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, buildTextMessage(t), t);
    }
    
    // If successful, redirect back to the delete GET Web Script
    status.setCode(Status.STATUS_MOVED_TEMPORARILY);
    status.setRedirect(true);
    status.setLocation(request.getServiceContextPath() + WEB_SCRIPT_URI_DELETE_SPACE);
    
    return(result);
}