Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010 JBoss Inc
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.drools.guvnor.server.files;
 
 
 import java.util.*;
 
 public class FeedServlet extends RepositoryServlet {
 
     private static final String VIEW_URL = "viewUrl";
 
     @Inject @Preferred
     private RulesRepository rulesRepository;
 
     @Inject
     private Identity identity;
 
     @Override
     protected void doGet(final HttpServletRequest request,
                          final HttpServletResponse responsethrows ServletException,
             IOException {
         try {
             String url = request.getRequestURI();
             if (url.contains("feed/package")) {
                 doAuthorizedAction(request,
                         response,
                         new Command() {
                             public void execute() throws Exception {
                                 doPackageFeed(request,
                                         response);
                             }
                         });
             } else if (url.contains("feed/category")) {
                 doAuthorizedAction(request,
                         response,
                         new Command() {
                             public void execute() throws Exception {
                                 doCategoryFeed(request,
                                         response);
                             }
                         });
             } else if (url.contains("feed/discussion")) {
                 doAuthorizedAction(request,
                         response,
                         new Command() {
                             public void execute() throws Exception {
                                 doDiscussionFeed(request,
                                         response);
                             }
                         });
             }
         } catch (AuthorizationException e) {
             response.setHeader("WWW-Authenticate",
                     "BASIC realm=\"users\"");
             response.sendError(.);
         }
 
     }
 
     private void doDiscussionFeed(HttpServletRequest request,
                                   HttpServletResponse responsethrows IOException {
         String assetName = request.getParameter("assetName");
         String packageName = request.getParameter("package");
         AssetItem asset = .loadModule(packageName).loadAsset(assetName);
        List<AtomFeed.AtomEntryentries = new ArrayList<AtomFeed.AtomEntry>();
        entries.add(new AtomFeed.AtomEntry(request,
                asset));
        for (DiscussionRecord dr : drs) {
            entries.add(new AtomFeed.AtomEntry(request,
                    asset,
                    dr));
        }
        AtomFeed feed = new AtomFeed("Discussion of: " + packageName + "/" + assetName,
                Calendar.getInstance(),
                request.getServerName() + "/" + packageName + "/" + assetName,
                request.getParameter(),
                request.getRequestURL().toString(),
                entries,
                "A list of updated discussion content.");
        response.setContentType("application/atom+xml; charset=UTF-8");
        response.getWriter().print(feed.getAtom());
    }
    private void doCategoryFeed(HttpServletRequest request,
                                HttpServletResponse responsethrows IOException {
        String cat = request.getParameter("name");
        String status = request.getParameter("status");
        checkCategoryPermission(cat);
                false,
                0,
                -1);
        Iterator<AssetItemit = pg.assets.iterator();
        List<AtomFeed.AtomEntryentries = new ArrayList<AtomFeed.AtomEntry>();
        buildEntries(request,
                entries,
                it,
                status);
        AtomFeed feed = new AtomFeed("Category: " + cat,
                Calendar.getInstance(),
                request.getServerName() + cat,
                request.getParameter(),
                request.getRequestURL().toString(),
                entries,
                "Guvnor category of items: " + cat);
        response.setContentType("application/atom+xml; charset=UTF-8");
        response.getWriter().print(feed.getAtom());
    }
    void checkCategoryPermission(String cat) {
        .checkPermission(new CategoryPathType(cat),
                ..getName());
    }
    private void doPackageFeed(HttpServletRequest request,
                               HttpServletResponse responsethrows IOException {
        String packageName = request.getParameter("name");
        checkPackageReadPermission(packageName);
        ModuleItem pkg = .loadModule(packageName);
        List<AtomFeed.AtomEntryentries = new ArrayList<AtomFeed.AtomEntry>();
        Iterator<AssetItemit = pkg.getAssets();
        buildEntries(request,
                entries,
                it,
                request.getParameter("status"));
        AtomFeed feed = new AtomFeed("Knowledge package: " + pkg.getName(),
                pkg.getLastModified(),
                pkg.getUUID(),
                request.getParameter(),
                request.getRequestURL().toString(),
                entries,
                pkg.getDescription());
        response.setContentType("application/atom+xml; charset=UTF-8");
        response.getWriter().print(feed.getAtom());
    }
    private void buildEntries(HttpServletRequest request,
                              List<AtomFeed.AtomEntryentries,
                              Iterator<AssetItemit,
                              String status) {
        while (it.hasNext()) {
            AssetItem as = it.next();
            if (!as.isArchived() && !as.getDisabled()) {
                if (status == null || status.equals("*") || as.getStateDescription().equals(status)) {
                    entries.add(new AtomFeed.AtomEntry(request,
                            as));
                }
            }
        }
    }
    void checkPackageReadPermission(String packageName) {
        .checkPermission(new ModuleNameType(packageName),
                ..getName());
    }
    public static class AtomFeed {
        private static final String TEMPLATE = StringUtils.readFileAsString(new InputStreamReader(AtomFeed.class.getResourceAsStream("/atom-feed-template.xml")));
        private final String feedTitle;
        private final String feedUpdated;
        private final String feedId;
        private final String feedAlternate;
        private final String feedSelf;
        private final String subtitle;
        private final Collection<AtomEntryentries;
        public AtomFeed(String title,
                        Calendar whenUpdate,
                        String feedId,
                        String feedAlternate,
                        String feedSelf,
                        Collection<AtomEntryentries,
                        String subtitle) {
            this. = title;
            this. = ISO8601.format(whenUpdate);
            this. = feedId;
            this. = feedAlternate;
            this. = feedSelf;
            this. = entries;
            this. = subtitle;
        }
        public String getAtom() {
            Map<StringAtomFeedm = new HashMap<StringAtomFeed>();
            m.put("feed",
                    this);
            return (String) TemplateRuntime.eval(,
                    m);
        }
        public String getSubtitle() {
            return ;
        }
        @SuppressWarnings("rawtypes")
        public Collection getEntries() {
            return ;
        }
        public String getFeedTitle() {
            return ;
        }
        public String getFeedUpdated() {
            return ;
        }
        public String getFeedId() {
            return ;
        }
        public String getFeedAlternate() {
            return ;
        }
        public String getFeedSelf() {
            return ;
        }
        public static class AtomEntry {
            private final String name;
            private final String webURL;
            private final String id;
            private final String updated;
            private final String published;
            private final String author;
            private final String contributor;
            private final String description;
            private final String checkinComment;
            private final String format;

            
We are creating an entry for each asset.
            public AtomEntry(HttpServletRequest req,
                             AssetItem asset) {
                this. = asset.getName();
                this. = asset.getFormat();
                //Escape & with %26 to make generated XML safe.
                this. = req.getParameter() + "#asset=" + asset.getUUID() + "%26nochrome";
                //Each history version of asset has its unique UUID. &version is not needed here. Plus &version
                //is not being parsed on the server side
                //this.id = asset.getUUID() + "&version=" + asset.getVersionNumber();
                this. = asset.getUUID();
                this. = ISO8601.format(asset.getLastModified());
                this. = ISO8601.format(asset.getCreatedDate());
                this. = asset.getCreator();
                this. = asset.getLastContributor();
                this. = asset.getDescription();
                this. = asset.getCheckinComment();
            }

            
We are creating entries for each discussion record
            public AtomEntry(HttpServletRequest req,
                             AssetItem asset,
                             DiscussionRecord discussionRec) {
                this. = asset.getName();
                this. = asset.getFormat();
                this. = req.getParameter() + "#asset=" + asset.getUUID() + "%26nochrome";
                this. = asset.getUUID();
                Calendar c = Calendar.getInstance();
                c.setTime(new Date(discussionRec.timestamp));
                this. = ISO8601.format(c);
                this. = ISO8601.format(c);
                this. = discussionRec.author;
                this. = asset.getLastContributor();
                this. = "Discussion comment was added by: " + discussionRec.author;
                String noteLines[] = discussionRec.note.split("\\r?\\n");
                String wn = "";
                for (int i = 0; i < noteLines.lengthi++) {
                    wn += noteLines[i];
                    if (i != noteLines.length - 1) {
                        wn += "<br/>";
                    }
                }
                this. = wn;
            }
            public String getName() {
                return ;
            }
            public String getFormat() {
                return ;
            }
            public String getWebURL() {
                return ;
            }
            public String getId() {
                return ;
            }
            public String getUpdated() {
                return ;
            }
            public String getPublished() {
                return ;
            }
            public String getAuthor() {
                return ;
            }
            public String getContributor() {
                return ;
            }
            public String getDescription() {
                return ;
            }
            public String getCheckinComment() {
                return ;
            }
        }
    }
New to GrepCode? Check out our FAQ X