Source Code Cross Referenced for FeedHandler.java in  » Portal » Open-Portal » com » sun » portal » app » blog » handler » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » Open Portal » com.sun.portal.app.blog.handler 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright ? 2006 Sun Microsystems, Inc. All rights reserved.
003:         *
004:         * Sun Microsystems, Inc. has intellectual property rights relating to
005:         * technology embodied in the product that is described in this document.
006:         * In particular, and without limitation, these intellectual property
007:         * rights may include one or more of the U.S. patents listed at
008:         * http://www.sun.com/patents and one or more additional patents or
009:         * pending patent applications in the U.S. and in other countries.
010:         *
011:         * U.S. Government Rights - Commercial software. Government users are subject
012:         * to the Sun Microsystems, Inc. standard license agreement and applicable
013:         * provisions of the FAR and its supplements. Use is subject to license terms.
014:         * This distribution may include materials developed by third parties.
015:         * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
016:         * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
017:         */
018:        package com.sun.portal.app.blog.handler;
019:
020:        import com.sun.portal.app.blog.BlogPortletException;
021:        import com.sun.portal.app.blog.EntrySerializer;
022:        import com.sun.portal.app.blog.Resources;
023:        import com.sun.portal.app.blog.env.AbstractPrefs;
024:        import com.sun.portal.app.blog.env.AbstractSearch;
025:        import com.sun.portal.app.blog.env.Search;
026:        import com.sun.portal.app.blog.env.User;
027:        import com.sun.portal.app.blog.model.EntryBean;
028:        import com.sun.portal.app.blog.model.FeedBean;
029:        import com.sun.portal.app.blog.httpverb.Delete;
030:        import com.sun.portal.app.blog.httpverb.Get;
031:        import com.sun.portal.app.blog.httpverb.Post;
032:        import com.sun.portal.app.blog.httpverb.Put;
033:        import com.sun.portal.app.blog.httpverb.HttpVerb;
034:        import com.sun.portal.app.blog.httpverb.HttpVerbException;
035:        import com.sun.portal.app.blog.model.EditManualBean;
036:        import com.sun.portal.app.blog.model.EditUserBean;
037:        import com.sun.portal.app.blog.model.EditWeblogBean;
038:        import com.sun.syndication.feed.atom.Content;
039:        import com.sun.syndication.feed.atom.Entry;
040:        import com.sun.syndication.feed.atom.Feed;
041:        import com.sun.syndication.feed.atom.Link;
042:        import com.sun.syndication.io.FeedException;
043:        import com.sun.syndication.io.WireFeedInput;
044:        import java.io.IOException;
045:        import java.io.InputStream;
046:        import java.net.MalformedURLException;
047:        import java.net.URL;
048:        import java.text.MessageFormat;
049:        import org.jdom.JDOMException;
050:        import org.xml.sax.InputSource;
051:        import org.apache.roller.webservices.adminapi.sdk.MemberEntry;
052:        import org.apache.roller.webservices.adminapi.sdk.MemberEntrySet;
053:        import org.apache.roller.webservices.adminapi.sdk.UnexpectedRootElementException;
054:        import org.apache.roller.webservices.adminapi.sdk.UserEntry;
055:        import org.apache.roller.webservices.adminapi.sdk.WeblogEntry;
056:        import javax.faces.context.FacesContext;
057:        import java.util.List;
058:        import java.util.Collections;
059:        import javax.faces.context.ExternalContext;
060:        import javax.portlet.PortletMode;
061:        import javax.portlet.PortletRequest;
062:
063:        public class FeedHandler {
064:            public static final class Columns {
065:                public static final String TITLE = "title";
066:                public static final String AUTHOR = "author";
067:                public static final String UPDATED = "updated";
068:            }
069:
070:            private static final AbstractPrefs prefs = AbstractPrefs
071:                    .getInstance();
072:            private static final AbstractSearch search = AbstractSearch
073:                    .getInstance();
074:            private static final User user = User.getInstance();
075:
076:            private static Resources resources = new Resources(
077:                    "com.sun.portal.app.blog.handler.FeedHandler");
078:
079:            private FeedBean feedBean;
080:            private EntryBean entryBean;
081:            private EditManualBean editManualBean;
082:            private EditUserBean editUserBean;
083:            private EditWeblogBean editWeblogBean;
084:            private MessageHandler messageHandler;
085:            private String sortColumn = Columns.UPDATED;
086:            private int firstRow = 0;
087:            private boolean ascending = false;
088:
089:            public boolean isAapp() {
090:                try {
091:                    return prefs.getAappUrl() != null
092:                            && prefs.getAappUrl().length() != 0;
093:                } catch (BlogPortletException bpe) {
094:                    return false;
095:                }
096:            }
097:
098:            public int getPageNumber() throws BlogPortletException {
099:                return getFirstRow() / getPageSize();
100:            }
101:
102:            public int getTotalPages() throws BlogPortletException {
103:                return (getFeedBean().getEntryBeans().size() - 1)
104:                        / getPageSize();
105:            }
106:
107:            public String nextPageAction() throws BlogPortletException {
108:                firstRow = getNextFirstRow();
109:                return "success";
110:            }
111:
112:            public String previousPageAction() throws BlogPortletException {
113:                firstRow = getPreviousFirstRow();
114:                return "success";
115:            }
116:
117:            public int getNextFirstRow() throws BlogPortletException {
118:                int firstRow = getFirstRow();
119:                int size = getFeedBean().getEntryBeans().size();
120:                int pageSize = getPageSize();
121:
122:                int next = firstRow + pageSize;
123:                if (next > size - 1) {
124:                    next = firstRow;
125:                }
126:
127:                return next;
128:            }
129:
130:            public int getFirstDisplayedRow() {
131:                return getFirstRow() + 1;
132:            }
133:
134:            public int getLastDisplayedRow() throws BlogPortletException {
135:                return Math.min(getFirstRow() + getPageSize(), getFeedBean()
136:                        .getEntryBeans().size());
137:            }
138:
139:            public int getPreviousFirstRow() throws BlogPortletException {
140:                return Math.max(getFirstRow() - getPageSize(), 0);
141:            }
142:
143:            public boolean isRenderNext() throws BlogPortletException {
144:                return getFirstRow() != getNextFirstRow();
145:            }
146:
147:            public boolean isRenderPrevious() throws BlogPortletException {
148:                return getFirstRow() != getPreviousFirstRow();
149:            }
150:
151:            public String sortFeedAction() {
152:                String column = getFacesParamValue("column");
153:                if (getSortColumn().equals(column)) {
154:                    setAscending(!isAscending());
155:                }
156:                setSortColumn(column);
157:
158:                // reset paging
159:                setFirstRow(0);
160:
161:                sortFeedBean();
162:
163:                return "success";
164:            }
165:
166:            private void sortFeedBean() {
167:                if (getSortColumn().equalsIgnoreCase(Columns.TITLE)) {
168:                    getFeedBean().sortByTitle(isAscending());
169:                } else if (getSortColumn().equalsIgnoreCase(Columns.AUTHOR)) {
170:                    getFeedBean().sortByAuthor(isAscending());
171:                } else {
172:                    getFeedBean().sortByUpdated(isAscending());
173:                }
174:            }
175:
176:            public String entryAction() {
177:                if (getEntryBean().getId() != null) {
178:                    return editEntryAction();
179:                }
180:                return createEntryAction();
181:            }
182:
183:            public String createEntryAction() {
184:                try {
185:                    String title = getEntryBean().getTitle();
186:                    if (title == null || title.length() == 0) {
187:                        getMessageHandler()
188:                                .addError(
189:                                        resources
190:                                                .get("createEntryAction.failure.noTitle"));
191:                        return "failure";
192:                    }
193:                    String content = getEntryBean().getContent();
194:                    if (content == null || content.length() == 0) {
195:                        getMessageHandler()
196:                                .addError(
197:                                        resources
198:                                                .get("createEntryAction.failure.noContent"));
199:                        return "failure";
200:                    }
201:
202:                    Entry entry = getEntryBean().toEntry();
203:                    InputStream contentStream = EntrySerializer
204:                            .toInputStream(entry);
205:                    HttpVerb post = new Post(new URL(getFeedBean().getUrl()),
206:                            getFeedBean().getUserName(), getFeedBean()
207:                                    .getPassword(), contentStream,
208:                            "application/atom+xml; utf8");
209:                    int status = post.execute();
210:                    if (!post.success()) {
211:                        getMessageHandler()
212:                                .addError(
213:                                        resources
214:                                                .get("createEntryAction.failure.postFailed"),
215:                                        "HTTP status code: " + status);
216:                        return "failure";
217:                    }
218:                    if (!initFeedBean()) {
219:                        return "failure";
220:                    }
221:
222:                    Entry newEntry = EntrySerializer.toEntry(post
223:                            .getResponseBody());
224:                    indexEntry(newEntry);
225:
226:                    getMessageHandler().addInfo(
227:                            resources.get("createEntryAction.success"));
228:                    return "success";
229:                } catch (IOException ioe) {
230:                    getMessageHandler().addError(
231:                            resources.get("createEntryAction.exception"),
232:                            ioe.getMessage());
233:                    return "failure";
234:                } catch (HttpVerbException ve) {
235:                    getMessageHandler().addError(
236:                            resources.get("createEntryAction.exception"),
237:                            ve.getMessage());
238:                    return "failure";
239:                } catch (FeedException fe) {
240:                    getMessageHandler().addError(
241:                            resources.get("createEntryAction.exception"),
242:                            fe.getMessage());
243:                    return "failure";
244:                } catch (JDOMException je) {
245:                    getMessageHandler().addError(
246:                            resources.get("createEntryAction.exception"),
247:                            je.getMessage());
248:                    return "failure";
249:                } catch (BlogPortletException bpe) {
250:                    getMessageHandler().addError(
251:                            resources.get("createEntryAction.exception"),
252:                            bpe.getMessage());
253:                    return "failure";
254:                }
255:            }
256:
257:            private void indexEntry(Entry entry) throws BlogPortletException {
258:                String entryLink = getEntryLink(entry);
259:                String title = entry.getTitle();
260:                String content = ((Content) entry.getContents().get(0))
261:                        .getValue();
262:
263:                // if we can't get the link or the title, we cannot index
264:                // feed is not required to give us those
265:                if (entryLink != null && entryLink.length() > 0
266:                        && title != null && title.length() > 0) {
267:                    insertSearch(entryLink, title, content);
268:                }
269:            }
270:
271:            private void reindexEntry(Entry entry) throws BlogPortletException {
272:                String entryLink = getEntryLink(entry);
273:                String title = entry.getTitle();
274:                String content = ((Content) entry.getContents().get(0))
275:                        .getValue();
276:
277:                // if we can't get the link or the title, we cannot index
278:                // feed is not required to give us those
279:                if (entryLink != null && entryLink.length() > 0
280:                        && title != null && title.length() > 0) {
281:                    updateSearch(entryLink, title, content);
282:                }
283:            }
284:
285:            private void deindexEntry(String entryLink)
286:                    throws BlogPortletException {
287:                // if we can't get the link or the title, we cannot index
288:                // feed is not required to give us those
289:                if (entryLink != null && entryLink.length() > 0) {
290:                    deleteSearch(entryLink);
291:                }
292:            }
293:
294:            private void insertSearch(String url, String title, String content)
295:                    throws BlogPortletException {
296:                AbstractSearch srch = search.getNewInstance(prefs);
297:                srch.setPortletRequest(getPortletRequest());
298:                srch.insert(url, title, content);
299:            }
300:
301:            private void updateSearch(String url, String title, String content)
302:                    throws BlogPortletException {
303:                AbstractSearch srch = search.getNewInstance(prefs);
304:                srch.setPortletRequest(getPortletRequest());
305:                srch.update(url, title, content);
306:            }
307:
308:            private void deleteSearch(String url) throws BlogPortletException {
309:                AbstractSearch srch = search.getNewInstance(prefs);
310:                srch.setPortletRequest(getPortletRequest());
311:                srch.delete(url);
312:            }
313:
314:            private String getEntryLink(Entry entry) {
315:                List altLinks = entry.getAlternateLinks();
316:                if (altLinks == null || altLinks.size() == 0) {
317:                    return null;
318:                }
319:
320:                Link link = (Link) altLinks.get(0);
321:                return link.getHref();
322:            }
323:
324:            public String editEntryAction() {
325:                // verify that current user is author
326:                // that UI should not allow us to get here unless this is the case,
327:                // but add an extra check here just in case
328:                if (!getEntryBean().isWritable()) {
329:                    getMessageHandler().addError(
330:                            resources.get("editEntryAction.failure.notAuthor"));
331:                    return "failure";
332:                }
333:
334:                try {
335:                    // get title, verify it's non-null'
336:                    String title = getEntryBean().getTitle();
337:                    if (title == null || title.length() == 0) {
338:                        getMessageHandler()
339:                                .addError(
340:                                        resources
341:                                                .get("editEntryAction.failure.noTitle"));
342:                        return "failure";
343:                    }
344:                    // get content, verify it's non-null
345:                    String content = getEntryBean().getContent();
346:                    if (content == null || content.length() == 0) {
347:                        getMessageHandler()
348:                                .addError(
349:                                        resources
350:                                                .get("editEntryAction.failure.noContent"));
351:                        return "failure";
352:                    }
353:
354:                    Entry entry = getEntryBean().toEntry();
355:                    InputStream contentStream = EntrySerializer
356:                            .toInputStream(entry);
357:                    String editLink = entryBean.getEditLink();
358:                    if (editLink == null || editLink.length() == 0) {
359:                        getMessageHandler()
360:                                .addError(
361:                                        resources
362:                                                .get("editEntryAction.failure.noEditLinkt"));
363:                        return "failure";
364:                    }
365:                    HttpVerb put = new Put(new URL(entryBean.getEditLink()),
366:                            getFeedBean().getUserName(), getFeedBean()
367:                                    .getPassword(), contentStream,
368:                            "application/atom+xml; utf8");
369:                    int status = put.execute();
370:                    if (!put.success()) {
371:                        getMessageHandler()
372:                                .addError(
373:                                        resources
374:                                                .get("editEntryAction.failure.putFailed"),
375:                                        "HTTP status code: " + status);
376:                        return "failure";
377:                    }
378:                    if (!initFeedBean()) {
379:                        return "failure";
380:                    }
381:
382:                    Entry newEntry = EntrySerializer.toEntry(put
383:                            .getResponseBody());
384:                    reindexEntry(newEntry);
385:
386:                    getMessageHandler().addInfo(
387:                            resources.get("editEntryAction.success"));
388:                    return "success";
389:                } catch (IOException ioe) {
390:                    getMessageHandler().addError(
391:                            resources.get("editEntryAction.exception"),
392:                            ioe.getMessage());
393:                    return "failure";
394:                } catch (HttpVerbException ve) {
395:                    getMessageHandler().addError(
396:                            resources.get("editEntryAction.exception"),
397:                            ve.getMessage());
398:                    return "failure";
399:                } catch (FeedException fe) {
400:                    getMessageHandler().addError(
401:                            resources.get("editEntryAction.exception"),
402:                            fe.getMessage());
403:                    return "failure";
404:                } catch (JDOMException je) {
405:                    getMessageHandler().addError(
406:                            resources.get("editEntryAction.exception"),
407:                            je.getMessage());
408:                    return "failure";
409:                } catch (BlogPortletException bpe) {
410:                    getMessageHandler().addError(
411:                            resources.get("editEntryAction.exception"),
412:                            bpe.getMessage());
413:                    return "failure";
414:                }
415:
416:            }
417:
418:            public String editEntryLinkAction() {
419:                String id = getFacesParamValue("entry.id");
420:                EntryBean entryBean = getFeedBean().getEntryBean(id);
421:                if (entryBean == null) {
422:                    getMessageHandler()
423:                            .addError(
424:                                    resources
425:                                            .get("editEntryButtonAction.failure.unknownId"),
426:                                    "ID: " + id);
427:                    return "failure";
428:                }
429:                // verify that current user is author
430:                // that UI should not allow us to get here unless this is the case,
431:                // but add an extra check here just in case
432:                if (!entryBean.isWritable()) {
433:                    getMessageHandler()
434:                            .addError(
435:                                    resources
436:                                            .get("editEntryButtonAction.failure.notAuthor"));
437:                    return "failure";
438:                }
439:
440:                getEntryBean().populate(entryBean);
441:                return "editEntry";
442:            }
443:
444:            public String createEntryLinkAction() {
445:                getEntryBean().clear();
446:                return "createEntry";
447:            }
448:
449:            public String deleteEntryLinkAction() {
450:                String id = getFacesParamValue("entry.id");
451:                EntryBean entryBean = getFeedBean().getEntryBean(id);
452:                if (entryBean == null) {
453:                    getMessageHandler()
454:                            .addError(
455:                                    resources
456:                                            .get("deleteEntryButtonAction.failure.unknownId"),
457:                                    "ID: " + id);
458:                    return "failure";
459:                }
460:                // verify that current user is author
461:                // that UI should not allow us to get here unless this is the case,
462:                // but add an extra check here just in case
463:                if (!entryBean.isWritable()) {
464:                    getMessageHandler()
465:                            .addError(
466:                                    resources
467:                                            .get("deleteEntryButtonAction.failure.notAuthor"));
468:                    return "failure";
469:                }
470:                try {
471:                    HttpVerb delete = new Delete(new URL(entryBean
472:                            .getEditLink()), getFeedBean().getUserName(),
473:                            getFeedBean().getPassword());
474:                    int status = delete.execute();
475:                    if (!delete.success()) {
476:                        getMessageHandler()
477:                                .addError(
478:                                        resources
479:                                                .get("deleteEntryButtonAction.failure.deleteFailed"),
480:                                        "HTTP status code: " + status);
481:                        return "failure";
482:                    }
483:                    if (!initFeedBean()) {
484:                        // initFeedBean() adds error msg
485:                        return "failure";
486:                    }
487:
488:                    deindexEntry(id);
489:
490:                    getMessageHandler().addInfo(
491:                            resources.get("deleteEntryButtonAction.success"));
492:                    return "success";
493:                } catch (IOException ioe) {
494:                    getMessageHandler().addError(
495:                            resources.get("deleteEntryButtonAction.exception"),
496:                            ioe.getMessage());
497:                    return "failure";
498:                } catch (HttpVerbException ve) {
499:                    getMessageHandler().addError(
500:                            resources.get("deleteEntryButtonAction.exception"),
501:                            ve.getMessage());
502:                    return "failure";
503:                } catch (BlogPortletException bpe) {
504:                    getMessageHandler().addError(
505:                            resources.get("deleteEntryButtonAction.exception"),
506:                            bpe.getMessage());
507:                    return "failure";
508:                }
509:
510:            }
511:
512:            private static String formatMessage(String res, Object[] args) {
513:                String pattern = resources.get(res);
514:                String msg = MessageFormat.format(pattern, args);
515:
516:                return msg;
517:            }
518:
519:            private boolean userCanEdit() throws BlogPortletException {
520:                if (isManualConfigMode()) {
521:                    // we can't check. assume we can edit
522:                    return true;
523:                }
524:
525:                try {
526:                    MemberEntry me = new MemberEntry(prefs.getHandle(), prefs
527:                            .getAppUserName(), prefs.getAappUrl());
528:                    Get get = new Get(new URL(me.getHref()), prefs
529:                            .getAappUserName(), prefs.getAappUserPassword());
530:                    int status = get.execute();
531:                    if (!get.success()) {
532:                        String msg = formatMessage("userCanEdit.noEdit",
533:                                new Object[] { prefs.getAppUserName(),
534:                                        prefs.getHandle() });
535:                        getMessageHandler().addError(msg,
536:                                "HTTP status code: " + status);
537:                        return false;
538:                    }
539:                    InputStream bodyStream = get.getResponseBody();
540:                    MemberEntrySet mes = new MemberEntrySet(bodyStream, prefs
541:                            .getAappUrl());
542:                    if (mes.getEntries() == null
543:                            || mes.getEntries().length == 0) {
544:                        String msg = formatMessage("userCanEdit.noEdit",
545:                                new Object[] { prefs.getAppUserName(),
546:                                        prefs.getHandle() });
547:                        getMessageHandler().addError(msg);
548:                        return false;
549:                    }
550:                    me = (MemberEntry) mes.getEntries()[0];
551:                    String permission = me.getPermission();
552:                    if (!permission.equals(MemberEntry.Permissions.ADMIN)
553:                            && !permission
554:                                    .equals(MemberEntry.Permissions.AUTHOR)) {
555:                        String msg = formatMessage("userCanEdit.noEdit",
556:                                new Object[] { prefs.getAppUserName(),
557:                                        prefs.getHandle() });
558:                        getMessageHandler().addError(msg);
559:                        return false;
560:                    }
561:
562:                    return true;
563:                } catch (IOException ioe) {
564:                    String msg = formatMessage("userCanEdit.exception",
565:                            new Object[] { prefs.getAppUserName(),
566:                                    prefs.getHandle() });
567:                    getMessageHandler().addError(msg, ioe.getMessage());
568:                    return false;
569:                } catch (HttpVerbException ve) {
570:                    String msg = formatMessage("userCanEdit.exception",
571:                            new Object[] { prefs.getAppUserName(),
572:                                    prefs.getHandle() });
573:                    getMessageHandler().addError(msg, ve.getMessage());
574:                    return false;
575:                } catch (JDOMException je) {
576:                    String msg = formatMessage("userCanEdit.exception",
577:                            new Object[] { prefs.getAppUserName(),
578:                                    prefs.getHandle() });
579:                    getMessageHandler().addError(msg, je.getMessage());
580:                    return false;
581:                } catch (UnexpectedRootElementException uree) {
582:                    String msg = formatMessage("userCanEdit.exception",
583:                            new Object[] { prefs.getAppUserName(),
584:                                    prefs.getHandle() });
585:                    getMessageHandler().addError(msg, uree.getMessage());
586:                    return false;
587:                }
588:            }
589:
590:            private boolean userExists() throws BlogPortletException {
591:                if (isManualConfigMode()) {
592:                    // we can't check. assume we can edit
593:                    return true;
594:                }
595:
596:                try {
597:                    UserEntry ue = new UserEntry(prefs.getAppUserName(), prefs
598:                            .getAappUrl());
599:                    Get get = new Get(new URL(ue.getHref()), prefs
600:                            .getAappUserName(), prefs.getAappUserPassword());
601:                    int status = get.execute();
602:                    if (!get.success()) {
603:                        String msg = formatMessage("userExists.noUser",
604:                                new Object[] { prefs.getAppUserName() });
605:                        getMessageHandler().addError(msg,
606:                                "HTTP status code: " + status);
607:                        return false;
608:                    }
609:
610:                    return true;
611:                } catch (IOException ioe) {
612:                    String msg = formatMessage("userExists.exception",
613:                            new Object[] { prefs.getAppUserName() });
614:                    getMessageHandler().addError(msg, ioe.getMessage());
615:                    return false;
616:                } catch (HttpVerbException ve) {
617:                    String msg = formatMessage("userExists.exception",
618:                            new Object[] { prefs.getAppUserName() });
619:                    getMessageHandler().addError(msg, ve.getMessage());
620:                    return false;
621:                }
622:            }
623:
624:            private boolean weblogExists() throws BlogPortletException {
625:                if (isManualConfigMode()) {
626:                    // we can't check. assume we can edit
627:                    return true;
628:                }
629:
630:                try {
631:                    WeblogEntry we = new WeblogEntry(prefs.getHandle(), prefs
632:                            .getAappUrl());
633:                    Get get = new Get(new URL(we.getHref()), prefs
634:                            .getAappUserName(), prefs.getAappUserPassword());
635:                    int status = get.execute();
636:                    if (!get.success()) {
637:                        String msg = formatMessage("weblogExists.noWeblog",
638:                                new Object[] { prefs.getHandle() });
639:                        getMessageHandler().addError(msg,
640:                                "HTTP status code: " + status);
641:                        return false;
642:                    }
643:
644:                    return true;
645:                } catch (IOException ioe) {
646:                    String msg = formatMessage("weblogExists.exception",
647:                            new Object[] { prefs.getHandle() });
648:                    getMessageHandler().addError(msg, ioe.getMessage());
649:                    return false;
650:                } catch (HttpVerbException ve) {
651:                    String msg = formatMessage("weblogExists.exception",
652:                            new Object[] { prefs.getHandle() });
653:                    getMessageHandler().addError(msg, ve.getMessage());
654:                    return false;
655:                }
656:            }
657:
658:            public boolean getStatus() throws BlogPortletException {
659:                long refreshInterval = prefs.getRefreshInterval();
660:                if (refreshInterval != -1) {
661:                    if (System.currentTimeMillis() > (getFeedBean()
662:                            .getRefreshed() + refreshInterval * 1000)) {
663:                        getFeedBean().setInitialized(false);
664:                    }
665:                }
666:
667:                if (!getFeedBean().isInitialized()) {
668:                    initFeedBean();
669:                }
670:                return false;
671:            }
672:
673:            public FeedBean getFeedBean() {
674:                return feedBean;
675:            }
676:
677:            public List getPagedEntryBeans() throws BlogPortletException {
678:                List all = getFeedBean().getEntryBeans();
679:                if (all == null) {
680:                    return Collections.EMPTY_LIST;
681:                }
682:                List paged = all.subList(getFirstRow(), Math.min(getFirstRow()
683:                        + getPageSize(), getFeedBean().getEntryBeans().size()));
684:                return paged;
685:            }
686:
687:            public void setFeedBean(FeedBean feedBean) {
688:                this .feedBean = feedBean;
689:            }
690:
691:            public boolean initFeedBean() {
692:                try {
693:                    // clear out fields and set as not initialized
694:                    getFeedBean().clear();
695:
696:                    getFeedBean().setUrl(prefs.getAppEntriesUrl());
697:                    getFeedBean().setUserName(prefs.getAppUserName());
698:                    getFeedBean().setPassword(prefs.getAppUserPassword());
699:
700:                    // populate / clear the edit beans
701:
702:                    getEditManualBean().setAppEntriesUrl(
703:                            prefs.getAppEntriesUrl());
704:                    getEditManualBean().setAppUserName(prefs.getAppUserName());
705:                    getEditManualBean().setAppUserPassword("");
706:
707:                    getEditUserBean().clear();
708:
709:                    getEditWeblogBean().clear();
710:
711:                    // verify that we have a url, user, password set
712:                    if (getFeedBean().getUrl() == null
713:                            || getFeedBean().getUrl().length() == 0) {
714:                        getMessageHandler().addError(
715:                                resources.get("initFeedBean.noUrl"));
716:                    } else if (getFeedBean().getUserName() == null
717:                            || getFeedBean().getUserName().length() == 0) {
718:                        getMessageHandler().addError(
719:                                resources.get("initFeedBean.noUserName"));
720:                    } else if (getFeedBean().getPassword() == null
721:                            || getFeedBean().getPassword().length() == 0) {
722:                        getMessageHandler().addError(
723:                                resources.get("initFeedBean.noUserPassword"));
724:                    } else if (!weblogExists()) {
725:                        // method has set alert
726:                    } else if (!userExists()) { // verify user exists
727:                        // method has set alert
728:                    } else if (!userCanEdit()) { // verify user has permission to edit weblog
729:                        // method has set alert
730:                    } else {
731:                        // we have url, user, password
732:                        // user exists, and can edit the weblog and therefore use
733:                        // the APP endpoint
734:                        Get get = new Get(new URL(getFeedBean().getUrl()),
735:                                getFeedBean().getUserName(), getFeedBean()
736:                                        .getPassword());
737:                        int status = get.execute();
738:                        if (!get.success()) {
739:                            String msg = formatMessage(
740:                                    "initFeedBean.getFailed", new Object[] {
741:                                            getFeedBean().getUrl(),
742:                                            getFeedBean().getUserName() });
743:                            getMessageHandler().addError(msg,
744:                                    "HTTP status code: " + status);
745:                        } else {
746:                            InputSource src = new InputSource(get
747:                                    .getResponseBody());
748:                            WireFeedInput input = new WireFeedInput();
749:
750:                            Feed feed = (Feed) input.build(src);
751:                            getFeedBean().populate(feed);
752:
753:                            // sort the new feed bean
754:                            sortFeedBean();
755:
756:                            // reset to first page
757:                            setFirstRow(0);
758:
759:                            // we're done - success
760:                            getFeedBean().setInitialized(true);
761:                        }
762:                    }
763:                } catch (MalformedURLException mfue) {
764:                    getMessageHandler().addError(
765:                            resources.get("initFeedBean.exception"),
766:                            mfue.getMessage());
767:                } catch (FeedException fe) {
768:                    getMessageHandler().addError(
769:                            resources.get("initFeedBean.exception"),
770:                            fe.getMessage());
771:                } catch (IOException ioe) {
772:                    getMessageHandler().addError(
773:                            resources.get("initFeedBean.exception"),
774:                            ioe.getMessage());
775:                } catch (HttpVerbException ve) {
776:                    getMessageHandler().addError(
777:                            resources.get("initFeedBean.exception"),
778:                            ve.getMessage());
779:                } catch (BlogPortletException bpe) {
780:                    getMessageHandler().addError(
781:                            resources.get("initFeedBean.exception"),
782:                            bpe.getMessage());
783:                }
784:
785:                return getFeedBean().isInitialized();
786:            }
787:
788:            public EntryBean getEntryBean() {
789:                return entryBean;
790:            }
791:
792:            public void setEntryBean(EntryBean entryBean) {
793:                this .entryBean = entryBean;
794:            }
795:
796:            public MessageHandler getMessageHandler() {
797:                return messageHandler;
798:            }
799:
800:            public void setMessageHandler(MessageHandler messageHandler) {
801:                this .messageHandler = messageHandler;
802:            }
803:
804:            public boolean isManualConfigMode() {
805:                boolean mode = !isWizardConfigMode()
806:                        && !isWizardUserOnlyConfigMode();
807:                return mode;
808:            }
809:
810:            public boolean isWizardConfigMode() {
811:                try {
812:                    // also, ensure that the AAPP endpoint is configured
813:                    boolean mode = isAapp()
814:                            && prefs.getConfigMode().toLowerCase().equals(
815:                                    "wizard");
816:                    return mode;
817:                } catch (BlogPortletException bpe) {
818:                    throw new RuntimeException(bpe);
819:                }
820:            }
821:
822:            public boolean isWizardUserOnlyConfigMode() {
823:                try {
824:                    // also, ensure that the AAPP endpoint is configured
825:                    boolean mode = isAapp()
826:                            && prefs.getConfigMode().toLowerCase().equals(
827:                                    "wizard-user-only");
828:                    return mode;
829:                } catch (BlogPortletException bpe) {
830:                    throw new RuntimeException(bpe);
831:                }
832:            }
833:
834:            private boolean isEditAllowed() {
835:                FacesContext fc = FacesContext.getCurrentInstance();
836:                ExternalContext ec = fc.getExternalContext();
837:                PortletRequest pr = (PortletRequest) ec.getRequest();
838:
839:                boolean allowed = pr.isPortletModeAllowed(PortletMode.EDIT);
840:                return allowed;
841:            }
842:
843:            public boolean isAdminConfigMode() {
844:                //boolean mode = isManualConfigMode() && prefs.getConfigMode().toLowerCase().equals("admin");
845:                boolean mode = isManualConfigMode() && !isEditAllowed();
846:                return mode;
847:            }
848:
849:            private boolean isEmpty(String s) {
850:                if (s == null || s.trim().length() == 0) {
851:                    return true;
852:                }
853:                return false;
854:            }
855:
856:            public static String getFacesParamValue(String name) {
857:                return (String) FacesContext.getCurrentInstance()
858:                        .getExternalContext().getRequestParameterMap()
859:                        .get(name);
860:            }
861:
862:            public EditManualBean getEditManualBean() {
863:                return editManualBean;
864:            }
865:
866:            public void setEditManualBean(EditManualBean editManualBean) {
867:                this .editManualBean = editManualBean;
868:            }
869:
870:            public EditUserBean getEditUserBean() {
871:                return editUserBean;
872:            }
873:
874:            public void setEditUserBean(EditUserBean editUserBean) {
875:                this .editUserBean = editUserBean;
876:            }
877:
878:            public EditWeblogBean getEditWeblogBean() {
879:                return editWeblogBean;
880:            }
881:
882:            public void setEditWeblogBean(EditWeblogBean editWeblogBean) {
883:                this .editWeblogBean = editWeblogBean;
884:            }
885:
886:            public String getSortColumn() {
887:                return sortColumn;
888:            }
889:
890:            public void setSortColumn(String sortColumn) {
891:                this .sortColumn = sortColumn;
892:            }
893:
894:            public int getPageSize() throws BlogPortletException {
895:                return prefs.getPageSize();
896:            }
897:
898:            public int getFirstRow() {
899:                return firstRow;
900:            }
901:
902:            public void setFirstRow(int firstRow) {
903:                this .firstRow = firstRow;
904:            }
905:
906:            public boolean isAscending() {
907:                return ascending;
908:            }
909:
910:            public void setAscending(boolean ascending) {
911:                this .ascending = ascending;
912:            }
913:
914:            private PortletRequest getPortletRequest() {
915:                ExternalContext econtext = FacesContext.getCurrentInstance()
916:                        .getExternalContext();
917:                PortletRequest preq = (PortletRequest) econtext.getRequest();
918:
919:                return preq;
920:            }
921:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.