Source Code Cross Referenced for MockRequest.java in  » Web-Framework » rife-1.6.1 » com » uwyn » rife » test » 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 » Web Framework » rife 1.6.1 » com.uwyn.rife.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
0003:         * Distributed under the terms of either:
0004:         * - the common development and distribution license (CDDL), v1.0; or
0005:         * - the GNU Lesser General Public License, v2.1 or later
0006:         * $Id: MockRequest.java 3669 2007-02-26 13:51:23Z gbevin $
0007:         */
0008:        package com.uwyn.rife.test;
0009:
0010:        import java.util.*;
0011:
0012:        import com.uwyn.rife.config.RifeConfig;
0013:        import com.uwyn.rife.engine.Request;
0014:        import com.uwyn.rife.engine.RequestMethod;
0015:        import com.uwyn.rife.engine.StateStore;
0016:        import com.uwyn.rife.engine.UploadedFile;
0017:        import com.uwyn.rife.engine.exceptions.MultipartFileTooBigException;
0018:        import com.uwyn.rife.engine.exceptions.MultipartInvalidUploadDirectoryException;
0019:        import com.uwyn.rife.engine.exceptions.MultipartRequestException;
0020:        import com.uwyn.rife.test.MockHeaders;
0021:        import com.uwyn.rife.tools.StringUtils;
0022:        import java.io.BufferedOutputStream;
0023:        import java.io.File;
0024:        import java.io.FileOutputStream;
0025:        import java.io.IOException;
0026:        import java.io.InputStream;
0027:        import javax.servlet.RequestDispatcher;
0028:        import javax.servlet.http.Cookie;
0029:        import javax.servlet.http.HttpServletRequest;
0030:        import javax.servlet.http.HttpSession;
0031:
0032:        /**
0033:         * Provides a {@link Request} implementation that is suitable for testing a
0034:         * web application outside of a servlet container.
0035:         *
0036:         * @author Geert Bevin (gbevin[remove] at uwyn dot com)
0037:         * @version $Revision: 3669 $
0038:         * @since 1.1
0039:         */
0040:        public class MockRequest implements  Request {
0041:            private static final String SESSIONID_NOT_CHECKED = "not checked",
0042:                    SESSIONID_URL = "url", SESSIONID_COOKIE = "cookie",
0043:                    SESSIONID_NONE = "none";
0044:
0045:            private RequestMethod mRequestMethod = RequestMethod.GET;
0046:            private Map<String, String[]> mParameters = new HashMap<String, String[]>();
0047:            private Map<String, UploadedFile[]> mFiles;
0048:            private Map<String, Object> mAttributes;
0049:            private String mCharacterEncoding;
0050:            private String mContentType;
0051:            private MockHeaders mHeaders = new MockHeaders();
0052:            private List<Locale> mLocales;
0053:            private File mUploadDirectory;
0054:
0055:            private MockConversation mMockConversation;
0056:            private MockResponse mMockResponse;
0057:            private MockSession mSession;
0058:            private String mRequestedSessionId;
0059:            private String mSessionIdState = SESSIONID_NOT_CHECKED;
0060:
0061:            private String mProtocol = "HTTP/1.1";
0062:            private String mRemoteAddr = "127.0.0.1";
0063:            private String mRemoteUser;
0064:            private String mRemoteHost = "localhost";
0065:            private boolean mSecure = false;
0066:
0067:            public void init(StateStore stateStore) {
0068:                String parameter_name = null;
0069:                String[] parameter_values = null;
0070:                for (Map.Entry<String, String[]> entry : mParameters.entrySet()) {
0071:                    parameter_name = entry.getKey();
0072:                    if (StringUtils.doesUrlValueNeedDecoding(parameter_name)) {
0073:                        parameter_name = StringUtils
0074:                                .decodeUrlValue(parameter_name);
0075:                    }
0076:
0077:                    parameter_values = entry.getValue();
0078:                    for (int i = 0; i < parameter_values.length; i++) {
0079:                        if (StringUtils
0080:                                .doesUrlValueNeedDecoding(parameter_values[i])) {
0081:                            parameter_values[i] = StringUtils
0082:                                    .decodeUrlValue(parameter_values[i]);
0083:                        }
0084:                    }
0085:
0086:                    mParameters.put(parameter_name, parameter_values);
0087:                }
0088:
0089:                Map<String, String[]> parameters = stateStore
0090:                        .restoreParameters(this );
0091:                if (parameters != null) {
0092:                    mParameters = parameters;
0093:                }
0094:            }
0095:
0096:            void setMockConversation(MockConversation conversation) {
0097:                mMockConversation = conversation;
0098:            }
0099:
0100:            void setMockResponse(MockResponse response) {
0101:                mMockResponse = response;
0102:            }
0103:
0104:            public RequestMethod getMethod() {
0105:                return mRequestMethod;
0106:            }
0107:
0108:            /**
0109:             * Sets the method of this request.
0110:             * <p>The method defaults to {@link RequestMethod#GET}.
0111:             *
0112:             * @param method the method that will be used by this request
0113:             * @see #getMethod
0114:             * @see #method
0115:             * @since 1.1
0116:             */
0117:            public void setMethod(RequestMethod method) {
0118:                if (null == method)
0119:                    throw new IllegalArgumentException("method can't be null");
0120:
0121:                mRequestMethod = method;
0122:            }
0123:
0124:            /**
0125:             * Sets the method of this request.
0126:             *
0127:             * @param method the method that will be used by this request
0128:             * @return this <code>MockRequest</code> instance
0129:             * @see #getMethod
0130:             * @see #setMethod
0131:             * @since 1.1
0132:             */
0133:            public MockRequest method(RequestMethod method) {
0134:                setMethod(method);
0135:
0136:                return this ;
0137:            }
0138:
0139:            /**
0140:             * Checks whether a named parameter is present in this request.
0141:             *
0142:             * @param name the name of the parameter to check
0143:             * @return <code>true</code> if the parameter is present; or
0144:             * <p><code>false</code> otherwise
0145:             * @see #getParameters
0146:             * @see #setParameters
0147:             * @see #setParameter(String, String[])
0148:             * @see #setParameter(String, String)
0149:             * @since 1.1
0150:             */
0151:            public boolean hasParameter(String name) {
0152:                return mParameters.containsKey(name);
0153:            }
0154:
0155:            /**
0156:             * Retrieves all the parameters of this request.
0157:             *
0158:             * @return a <code>Map</code> of the parameters with the names as the keys
0159:             * and their value arrays as the values
0160:             * @see #hasParameter
0161:             * @see #setParameters
0162:             * @see #setParameter(String, String[])
0163:             * @see #setParameter(String, String)
0164:             * @since 1.1
0165:             */
0166:            public Map<String, String[]> getParameters() {
0167:                return mParameters;
0168:            }
0169:
0170:            /**
0171:             * Sets a map of parameters in this request.
0172:             *
0173:             * @param parameters a <code>Map</code> of the parameters that will be set
0174:             * with the names as the keys and their value arrays as the values
0175:             * @see #hasParameter
0176:             * @see #getParameters
0177:             * @see #setParameter(String, String[])
0178:             * @see #setParameter(String, String)
0179:             * @since 1.1
0180:             */
0181:            public void setParameters(Map<String, String[]> parameters) {
0182:                if (null == parameters) {
0183:                    return;
0184:                }
0185:
0186:                for (Map.Entry<String, String[]> parameter : parameters
0187:                        .entrySet()) {
0188:                    setParameter(parameter.getKey(), parameter.getValue());
0189:                }
0190:            }
0191:
0192:            /**
0193:             * Sets a map of parameters in this request.
0194:             *
0195:             * @param parameters a <code>Map</code> of the parameters that will be set
0196:             * with the names as the keys and their value arrays as the values
0197:             * @return this <code>MockRequest</code> instance
0198:             * @see #hasParameter
0199:             * @see #getParameters
0200:             * @see #setParameters
0201:             * @see #setParameter(String, String[])
0202:             * @see #setParameter(String, String)
0203:             * @since 1.1
0204:             */
0205:            public MockRequest parameters(Map<String, String[]> parameters) {
0206:                setParameters(parameters);
0207:
0208:                return this ;
0209:            }
0210:
0211:            /**
0212:             * Sets a parameter in this request.
0213:             *
0214:             * @param name the name of the parameter
0215:             * @param values the value array of the parameter
0216:             * @see #hasParameter
0217:             * @see #getParameters
0218:             * @see #setParameters
0219:             * @see #setParameter(String, String)
0220:             * @since 1.1
0221:             */
0222:            public void setParameter(String name, String[] values) {
0223:                if (null == name)
0224:                    throw new IllegalArgumentException("name can't be null");
0225:                if (0 == name.length())
0226:                    throw new IllegalArgumentException("name can't be empty");
0227:                if (null == values)
0228:                    throw new IllegalArgumentException("values can't be null");
0229:
0230:                mParameters.put(name, values);
0231:                if (mFiles != null) {
0232:                    mFiles.remove(name);
0233:                }
0234:            }
0235:
0236:            /**
0237:             * Sets a parameter in this request.
0238:             *
0239:             * @param name the name of the parameter
0240:             * @param values the value array of the parameter
0241:             * @return this <code>MockRequest</code> instance
0242:             * @see #hasParameter
0243:             * @see #getParameters
0244:             * @see #setParameters
0245:             * @see #setParameter(String, String[])
0246:             * @see #setParameter(String, String)
0247:             * @since 1.1
0248:             */
0249:            public MockRequest parameter(String name, String[] values) {
0250:                setParameter(name, values);
0251:
0252:                return this ;
0253:            }
0254:
0255:            /**
0256:             * Sets a parameter in this request.
0257:             *
0258:             * @param name the name of the parameter
0259:             * @param value the value of the parameter
0260:             * @see #hasParameter
0261:             * @see #getParameters
0262:             * @see #setParameters
0263:             * @see #setParameter(String, String[])
0264:             * @since 1.1
0265:             */
0266:            public void setParameter(String name, String value) {
0267:                if (null == name)
0268:                    throw new IllegalArgumentException("name can't be null");
0269:                if (0 == name.length())
0270:                    throw new IllegalArgumentException("name can't be empty");
0271:                if (null == value)
0272:                    throw new IllegalArgumentException("value can't be null");
0273:
0274:                setParameter(name, new String[] { value });
0275:            }
0276:
0277:            /**
0278:             * Sets a parameter in this request.
0279:             *
0280:             * @param name the name of the parameter
0281:             * @param value the value of the parameter
0282:             * @return this <code>MockRequest</code> instance
0283:             * @see #hasParameter
0284:             * @see #getParameters
0285:             * @see #setParameters
0286:             * @see #setParameter(String, String[])
0287:             * @see #setParameter(String, String)
0288:             * @since 1.1
0289:             */
0290:            public MockRequest parameter(String name, String value) {
0291:                setParameter(name, value);
0292:
0293:                return this ;
0294:            }
0295:
0296:            public Map<String, UploadedFile[]> getFiles() {
0297:                return mFiles;
0298:            }
0299:
0300:            public boolean hasFile(String name) {
0301:                if (null == name)
0302:                    throw new IllegalArgumentException("name can't be null");
0303:                if (0 == name.length())
0304:                    throw new IllegalArgumentException("name can't be empty");
0305:
0306:                if (null == getFiles()) {
0307:                    return false;
0308:                }
0309:
0310:                if (!getFiles().containsKey(name)) {
0311:                    return false;
0312:                }
0313:
0314:                UploadedFile[] uploaded_files = getFiles().get(name);
0315:
0316:                if (0 == uploaded_files.length) {
0317:                    return false;
0318:                }
0319:
0320:                for (UploadedFile uploaded_file : uploaded_files) {
0321:                    if (uploaded_file != null
0322:                            && uploaded_file.getName() != null) {
0323:                        return true;
0324:                    }
0325:                }
0326:
0327:                return false;
0328:            }
0329:
0330:            public UploadedFile getFile(String name) {
0331:                if (null == name)
0332:                    throw new IllegalArgumentException("name can't be null");
0333:                if (0 == name.length())
0334:                    throw new IllegalArgumentException("name can't be empty");
0335:
0336:                if (null == getFiles()) {
0337:                    return null;
0338:                }
0339:
0340:                UploadedFile[] files = getFiles().get(name);
0341:                if (null == files) {
0342:                    return null;
0343:                }
0344:
0345:                return files[0];
0346:            }
0347:
0348:            public UploadedFile[] getFiles(String name) {
0349:                if (null == name)
0350:                    throw new IllegalArgumentException("name can't be null");
0351:                if (0 == name.length())
0352:                    throw new IllegalArgumentException("name can't be empty");
0353:
0354:                if (null == getFiles()) {
0355:                    return null;
0356:                }
0357:
0358:                return getFiles().get(name);
0359:            }
0360:
0361:            private void checkUploadDirectory() {
0362:                mUploadDirectory = new File(RifeConfig.Engine
0363:                        .getFileUploadPath());
0364:                mUploadDirectory.mkdirs();
0365:
0366:                if (!mUploadDirectory.exists()
0367:                        || !mUploadDirectory.isDirectory()
0368:                        || !mUploadDirectory.canWrite()) {
0369:                    throw new MultipartInvalidUploadDirectoryException(
0370:                            mUploadDirectory);
0371:                }
0372:            }
0373:
0374:            /**
0375:             * Sets a file in this request.
0376:             *
0377:             * @param name the parameter name of the file
0378:             * @param file the file specification that will be uploaded
0379:             * @see #hasFile
0380:             * @see #getFile
0381:             * @see #getFiles
0382:             * @see #setFiles(Map)
0383:             * @see #setFiles(String, MockFileUpload[])
0384:             * @since 1.1
0385:             */
0386:            public void setFile(String name, MockFileUpload file) {
0387:                if (null == name)
0388:                    throw new IllegalArgumentException("name can't be null");
0389:                if (0 == name.length())
0390:                    throw new IllegalArgumentException("name can't be empty");
0391:                if (null == file)
0392:                    throw new IllegalArgumentException("file can't be null");
0393:
0394:                setFiles(name, new MockFileUpload[] { file });
0395:            }
0396:
0397:            /**
0398:             * Sets a map of files in this request.
0399:             *
0400:             * @param files a <code>Map</code> of the files that will be set with the
0401:             * names as the keys and their file upload specifications as the values
0402:             * @see #hasFile
0403:             * @see #getFile
0404:             * @see #getFiles
0405:             * @see #setFile(String, MockFileUpload)
0406:             * @see #setFiles(String, MockFileUpload[])
0407:             * @since 1.1
0408:             */
0409:            public void setFiles(Map<String, MockFileUpload[]> files) {
0410:                if (null == files || 0 == files.size()) {
0411:                    return;
0412:                }
0413:
0414:                for (Map.Entry<String, MockFileUpload[]> file : files
0415:                        .entrySet()) {
0416:                    setFiles(file.getKey(), file.getValue());
0417:                }
0418:            }
0419:
0420:            /**
0421:             * Sets files in this request.
0422:             *
0423:             * @param name the parameter name of the file
0424:             * @param files the file specifications that will be uploaded
0425:             * @see #hasFile
0426:             * @see #getFile
0427:             * @see #getFiles
0428:             * @see #setFile(String, MockFileUpload)
0429:             * @see #setFiles(Map)
0430:             * @since 1.1
0431:             */
0432:            public void setFiles(String name, MockFileUpload[] files) {
0433:                if (null == name)
0434:                    throw new IllegalArgumentException("name can't be null");
0435:                if (0 == name.length())
0436:                    throw new IllegalArgumentException("name can't be empty");
0437:                if (null == files)
0438:                    throw new IllegalArgumentException("files can't be null");
0439:
0440:                checkUploadDirectory();
0441:
0442:                if (null == mFiles) {
0443:                    mFiles = new HashMap<String, UploadedFile[]>();
0444:                }
0445:
0446:                UploadedFile[] uploaded_files = new UploadedFile[files.length];
0447:                for (int i = 0; i < files.length; i++) {
0448:                    UploadedFile uploaded_file = new UploadedFile(files[i]
0449:                            .getFileName(), files[i].getContentType());
0450:
0451:                    try {
0452:                        File tmp_file = File.createTempFile("upl", ".tmp",
0453:                                mUploadDirectory);
0454:                        FileOutputStream output_stream = new FileOutputStream(
0455:                                tmp_file);
0456:                        BufferedOutputStream output = new BufferedOutputStream(
0457:                                output_stream, 8 * 1024); // 8K
0458:
0459:                        InputStream input_stream = files[i].getInputStream();
0460:                        long downloaded_size = 0;
0461:
0462:                        byte[] buffer = new byte[1024];
0463:                        int return_value = -1;
0464:
0465:                        return_value = input_stream.read(buffer);
0466:                        while (-1 != return_value) {
0467:                            output.write(buffer, 0, return_value);
0468:
0469:                            // increase size count
0470:                            if (output != null
0471:                                    && RifeConfig.Engine
0472:                                            .getFileUploadSizeCheck()) {
0473:                                downloaded_size += return_value;
0474:
0475:                                if (downloaded_size > RifeConfig.Engine
0476:                                        .getFileuploadSizeLimit()) {
0477:                                    uploaded_file.setSizeExceeded(true);
0478:                                    output.close();
0479:                                    output = null;
0480:                                    tmp_file.delete();
0481:                                    tmp_file = null;
0482:                                    if (RifeConfig.Engine
0483:                                            .getFileUploadSizeException()) {
0484:                                        throw new MultipartFileTooBigException(
0485:                                                name,
0486:                                                RifeConfig.Engine
0487:                                                        .getFileuploadSizeLimit());
0488:                                    }
0489:                                }
0490:                            }
0491:
0492:                            return_value = input_stream.read(buffer);
0493:                        }
0494:
0495:                        if (output != null) {
0496:                            output.flush();
0497:                            output.close();
0498:                            output_stream.close();
0499:                        }
0500:
0501:                        if (tmp_file != null) {
0502:                            uploaded_file.setTempFile(tmp_file);
0503:                        }
0504:
0505:                        uploaded_files[i] = uploaded_file;
0506:                    } catch (IOException e) {
0507:                        throw new MultipartRequestException(e);
0508:                    }
0509:                }
0510:
0511:                mFiles.put(name, uploaded_files);
0512:                mParameters.remove(name);
0513:            }
0514:
0515:            /**
0516:             * Sets a file in this request.
0517:             *
0518:             * @param name the parameter name of the file
0519:             * @param file the file specification that will be uploaded
0520:             * @return this <code>MockRequest</code> instance
0521:             * @see #hasFile
0522:             * @see #getFile
0523:             * @see #getFiles
0524:             * @see #setFile(String, MockFileUpload)
0525:             * @see #setFiles(Map)
0526:             * @see #setFiles(String, MockFileUpload[])
0527:             * @since 1.1
0528:             */
0529:            public MockRequest file(String name, MockFileUpload file) {
0530:                setFile(name, file);
0531:
0532:                return this ;
0533:            }
0534:
0535:            /**
0536:             * Sets a map of files in this request.
0537:             *
0538:             * @param files a <code>Map</code> of the files that will be set with the
0539:             * names as the keys and their file upload specifications as the values
0540:             * @return this <code>MockRequest</code> instance
0541:             * @see #hasFile
0542:             * @see #getFile
0543:             * @see #getFiles
0544:             * @see #setFile(String, MockFileUpload)
0545:             * @see #setFiles(Map)
0546:             * @see #setFiles(String, MockFileUpload[])
0547:             * @since 1.1
0548:             */
0549:            public MockRequest files(Map<String, MockFileUpload[]> files) {
0550:                setFiles(files);
0551:
0552:                return this ;
0553:            }
0554:
0555:            /**
0556:             * Sets files in this request.
0557:             *
0558:             * @param name the parameter name of the file
0559:             * @param files the file specifications that will be uploaded
0560:             * @return this <code>MockRequest</code> instance
0561:             * @see #hasFile
0562:             * @see #getFile
0563:             * @see #getFiles
0564:             * @see #setFile(String, MockFileUpload)
0565:             * @see #setFiles(Map)
0566:             * @see #setFiles(String, MockFileUpload[])
0567:             * @since 1.1
0568:             */
0569:            public MockRequest files(String name, MockFileUpload[] files) {
0570:                setFiles(name, files);
0571:
0572:                return this ;
0573:            }
0574:
0575:            public String getServerRootUrl(int port) {
0576:                StringBuilder server_root = new StringBuilder();
0577:                server_root.append(getScheme());
0578:                server_root.append("://");
0579:                server_root.append(getServerName());
0580:                if (port <= -1) {
0581:                    port = getServerPort();
0582:                }
0583:                if (port != 80) {
0584:                    server_root.append(":");
0585:                    server_root.append(port);
0586:                }
0587:                return server_root.toString();
0588:            }
0589:
0590:            public boolean hasCookie(String name) {
0591:                return mMockConversation.hasCookie(name);
0592:            }
0593:
0594:            public Cookie getCookie(String name) {
0595:                return mMockConversation.getCookie(name);
0596:            }
0597:
0598:            public Cookie[] getCookies() {
0599:                return mMockConversation.getCookies();
0600:            }
0601:
0602:            public Object getAttribute(String name) {
0603:                if (null == mAttributes) {
0604:                    return null;
0605:                }
0606:
0607:                return mAttributes.get(name);
0608:            }
0609:
0610:            public boolean hasAttribute(String name) {
0611:                if (null == mAttributes) {
0612:                    return false;
0613:                }
0614:
0615:                return mAttributes.containsKey(name);
0616:            }
0617:
0618:            public Enumeration getAttributeNames() {
0619:                if (null == mAttributes) {
0620:                    return Collections.enumeration(new ArrayList<String>());
0621:                }
0622:
0623:                return Collections.enumeration(mAttributes.keySet());
0624:            }
0625:
0626:            public void removeAttribute(String name) {
0627:                if (null == name)
0628:                    throw new IllegalArgumentException("name can't be null");
0629:                if (0 == name.length())
0630:                    throw new IllegalArgumentException("name can't be empty");
0631:
0632:                if (null == mAttributes) {
0633:                    return;
0634:                }
0635:
0636:                mAttributes.remove(name);
0637:            }
0638:
0639:            public void setAttribute(String name, Object object) {
0640:                if (null == name)
0641:                    throw new IllegalArgumentException("name can't be null");
0642:                if (0 == name.length())
0643:                    throw new IllegalArgumentException("name can't be empty");
0644:
0645:                if (null == mAttributes) {
0646:                    mAttributes = new HashMap<String, Object>();
0647:                }
0648:
0649:                mAttributes.put(name, object);
0650:            }
0651:
0652:            public String getCharacterEncoding() {
0653:                return mCharacterEncoding;
0654:            }
0655:
0656:            /**
0657:             * Set the character encoding of this request.
0658:             *
0659:             * @param encoding the name of the character encoding
0660:             * @since 1.1
0661:             */
0662:            public void setCharacterEncoding(String encoding) {
0663:                if (null == encoding)
0664:                    throw new IllegalArgumentException("encoding can't be null");
0665:                if (0 == encoding.length())
0666:                    throw new IllegalArgumentException(
0667:                            "encoding can't be empty");
0668:
0669:                mCharacterEncoding = encoding;
0670:            }
0671:
0672:            /**
0673:             * Set the character encoding of this request.
0674:             *
0675:             * @param encoding the name of the character encoding
0676:             * @return this <code>MockRequest</code> instance
0677:             * @since 1.1
0678:             */
0679:            public MockRequest characterEncoding(String encoding) {
0680:                setCharacterEncoding(encoding);
0681:
0682:                return this ;
0683:            }
0684:
0685:            public String getContentType() {
0686:                return mContentType;
0687:            }
0688:
0689:            /**
0690:             * Set the content type of this request.
0691:             *
0692:             * @param type the content type
0693:             * @since 1.1
0694:             */
0695:            public void setContentType(String type) {
0696:                if (null == type)
0697:                    throw new IllegalArgumentException("type can't be null");
0698:                if (0 == type.length())
0699:                    throw new IllegalArgumentException("type can't be empty");
0700:
0701:                mContentType = type;
0702:            }
0703:
0704:            /**
0705:             * Set the content type of this request.
0706:             *
0707:             * @param type the content type
0708:             * @return this <code>MockRequest</code> instance
0709:             * @since 1.1
0710:             */
0711:            public MockRequest contentType(String type) {
0712:                setContentType(type);
0713:
0714:                return this ;
0715:            }
0716:
0717:            public long getDateHeader(String name) {
0718:                return mHeaders.getDateHeader(name);
0719:            }
0720:
0721:            public String getHeader(String name) {
0722:                return mHeaders.getHeader(name);
0723:            }
0724:
0725:            public Enumeration getHeaderNames() {
0726:                return Collections.enumeration(mHeaders.getHeaderNames());
0727:            }
0728:
0729:            public Enumeration getHeaders(String name) {
0730:                return Collections.enumeration(mHeaders.getHeaders(name));
0731:            }
0732:
0733:            public int getIntHeader(String name) {
0734:                return mHeaders.getIntHeader(name);
0735:            }
0736:
0737:            /**
0738:             * Adds a request header with the given name and value. This method allows
0739:             * request headers to have multiple values.
0740:             *
0741:             * @param name the name of the header to set
0742:             * @param value the additional header value
0743:             * @return this <code>MockRequest</code> instance
0744:             * @since 1.1
0745:             */
0746:            public MockRequest addHeader(String name, String value) {
0747:                if (null == name)
0748:                    throw new IllegalArgumentException("name can't be null");
0749:                if (0 == name.length())
0750:                    throw new IllegalArgumentException("name can't be empty");
0751:                if (null == value)
0752:                    throw new IllegalArgumentException("value can't be null");
0753:
0754:                mHeaders.addHeader(name, value);
0755:
0756:                return this ;
0757:            }
0758:
0759:            /**
0760:             * Adds a request header with the given name and date-value. The date is
0761:             * specified in terms of milliseconds since the epoch. This method allows
0762:             * request headers to have multiple values.
0763:             *
0764:             * @param name the name of the header to set
0765:             * @param value the additional date value
0766:             * @return this <code>MockRequest</code> instance
0767:             * @since 1.1
0768:             */
0769:            public MockRequest addDateHeader(String name, long value) {
0770:                if (null == name)
0771:                    throw new IllegalArgumentException("name can't be null");
0772:                if (0 == name.length())
0773:                    throw new IllegalArgumentException("name can't be empty");
0774:
0775:                mHeaders.addDateHeader(name, value);
0776:
0777:                return this ;
0778:            }
0779:
0780:            /**
0781:             * Adds a request header with the given name and integer value. This
0782:             * method allows request headers to have multiple values.
0783:             *
0784:             * @param name the name of the header to set
0785:             * @param value the additional integer value
0786:             * @return this <code>MockRequest</code> instance
0787:             * @since 1.1
0788:             */
0789:            public MockRequest addIntHeader(String name, int value) {
0790:                if (null == name)
0791:                    throw new IllegalArgumentException("name can't be null");
0792:                if (0 == name.length())
0793:                    throw new IllegalArgumentException("name can't be empty");
0794:
0795:                mHeaders.addIntHeader(name, value);
0796:
0797:                return this ;
0798:            }
0799:
0800:            /**
0801:             * Checks whether a certain request header is present.
0802:             *
0803:             * @param name the name of the header to check
0804:             * @return <code>true</code> if the header was present; or
0805:             * <p><code>false</code> otherwise
0806:             * @since 1.1
0807:             */
0808:            public boolean containsHeader(String name) {
0809:                return mHeaders.containsHeader(name);
0810:            }
0811:
0812:            /**
0813:             * Sets a request header with the given name and date-value. The date is
0814:             * specified in terms of milliseconds since the epoch. If the header had
0815:             * already been set, the new value overwrites the previous one. The {@link
0816:             * #containsHeader} method can be used to test for the presence of a
0817:             * header before setting its value.
0818:             *
0819:             * @param name the name of the header to set
0820:             * @param value the assigned date value
0821:             * @since 1.1
0822:             */
0823:            public void setDateHeader(String name, long value) {
0824:                if (null == name)
0825:                    throw new IllegalArgumentException("name can't be null");
0826:                if (0 == name.length())
0827:                    throw new IllegalArgumentException("name can't be empty");
0828:
0829:                mHeaders.setDateHeader(name, value);
0830:            }
0831:
0832:            /**
0833:             * Sets a request header with the given name and date-value.
0834:             *
0835:             * @param name the name of the header to set
0836:             * @param value the assigned date value
0837:             * @see #setDateHeader
0838:             * @since 1.1
0839:             */
0840:            public MockRequest dateHeader(String name, long value) {
0841:                setDateHeader(name, value);
0842:
0843:                return this ;
0844:            }
0845:
0846:            /**
0847:             * Sets a request header with the given name and value. If the header had
0848:             * already been set, the new value overwrites the previous one. The {@link
0849:             * #containsHeader} method can be used to test for the presence of a
0850:             * header before setting its value.
0851:             *
0852:             * @param name the name of the header to set
0853:             * @param value the header value
0854:             * @since 1.1
0855:             */
0856:            public void setHeader(String name, String value) {
0857:                if (null == name)
0858:                    throw new IllegalArgumentException("name can't be null");
0859:                if (0 == name.length())
0860:                    throw new IllegalArgumentException("name can't be empty");
0861:                if (null == value)
0862:                    throw new IllegalArgumentException("value can't be null");
0863:
0864:                mHeaders.setHeader(name, value);
0865:            }
0866:
0867:            /**
0868:             * Sets a request header with the given name and value.
0869:             *
0870:             * @param name the name of the header to set
0871:             * @param value the header value
0872:             * @see #setDateHeader
0873:             * @since 1.1
0874:             */
0875:            public MockRequest header(String name, String value) {
0876:                setHeader(name, value);
0877:
0878:                return this ;
0879:            }
0880:
0881:            /**
0882:             * Sets a request header with the given name and integer value. If the
0883:             * header had already been set, the new value overwrites the previous one.
0884:             * The containsHeader method can be used to test for the presence of a
0885:             * header before setting its value.
0886:             *
0887:             * @param name the name of the header to set
0888:             * @param value the assigned integer value
0889:             * @since 1.1
0890:             */
0891:            public void setIntHeader(String name, int value) {
0892:                if (null == name)
0893:                    throw new IllegalArgumentException("name can't be null");
0894:                if (0 == name.length())
0895:                    throw new IllegalArgumentException("name can't be empty");
0896:
0897:                mHeaders.setIntHeader(name, value);
0898:            }
0899:
0900:            /**
0901:             * Sets a request header with the given name and integer value.
0902:             *
0903:             * @param name the name of the header to set
0904:             * @param value the assigned integer value
0905:             * @see #setDateHeader
0906:             * @since 1.1
0907:             */
0908:            public MockRequest intHeader(String name, int value) {
0909:                setIntHeader(name, value);
0910:
0911:                return this ;
0912:            }
0913:
0914:            /**
0915:             * Removes a request header with the given name.
0916:             *
0917:             * @param name the name of the header to remove
0918:             * @since 1.1
0919:             */
0920:            public void removeHeader(String name) {
0921:                mHeaders.removeHeader(name);
0922:            }
0923:
0924:            public Locale getLocale() {
0925:                if (null == mLocales || 0 == mLocales.size()) {
0926:                    return Locale.getDefault();
0927:                }
0928:
0929:                return mLocales.get(0);
0930:            }
0931:
0932:            public Enumeration getLocales() {
0933:                if (null == mLocales) {
0934:                    return Collections.enumeration(new ArrayList() {
0935:                        {
0936:                            add(Locale.getDefault());
0937:                        }
0938:                    });
0939:                }
0940:
0941:                return Collections.enumeration(mLocales);
0942:            }
0943:
0944:            /**
0945:             * Adds a {@link Locale} to this request.
0946:             *
0947:             * @param locale the locale to add
0948:             * @since 1.1
0949:             */
0950:            public void addLocale(Locale locale) {
0951:                if (null == locale) {
0952:                    return;
0953:                }
0954:
0955:                if (null == mLocales) {
0956:                    mLocales = new ArrayList<Locale>();
0957:                }
0958:
0959:                mLocales.add(locale);
0960:            }
0961:
0962:            /**
0963:             * Adds a {@link Locale} to this request.
0964:             *
0965:             * @param locale the locale to add
0966:             * @return this <code>MockRequest</code> instance
0967:             * @since 1.1
0968:             */
0969:            public MockRequest locale(Locale locale) {
0970:                addLocale(locale);
0971:
0972:                return this ;
0973:            }
0974:
0975:            public String getProtocol() {
0976:                return mProtocol;
0977:            }
0978:
0979:            /**
0980:             * Set the protocol of this request.
0981:             * <p>The default protocol is <code>"HTTP/1.1"</code>.
0982:             *
0983:             * @param protocol the protocol to set
0984:             * @since 1.1
0985:             */
0986:            public void setProtocol(String protocol) {
0987:                if (null == protocol)
0988:                    throw new IllegalArgumentException("protocol can't be null");
0989:                if (0 == protocol.length())
0990:                    throw new IllegalArgumentException(
0991:                            "protocol can't be empty");
0992:
0993:                mProtocol = protocol;
0994:            }
0995:
0996:            /**
0997:             * Set the protocol of this request.
0998:             *
0999:             * @param protocol the protocol to set
1000:             * @return this <code>MockRequest</code> instance
1001:             * @since 1.1
1002:             */
1003:            public MockRequest protocol(String protocol) {
1004:                setProtocol(protocol);
1005:
1006:                return this ;
1007:            }
1008:
1009:            public String getRemoteAddr() {
1010:                return mRemoteAddr;
1011:            }
1012:
1013:            /**
1014:             * Set the remote address of this request.
1015:             * <p>The default remote address is "<code>127.0.0.1"</code>.
1016:             *
1017:             * @param remoteAddr the remote address to set
1018:             * @since 1.1
1019:             */
1020:            public void setRemoteAddr(String remoteAddr) {
1021:                if (null == remoteAddr)
1022:                    throw new IllegalArgumentException(
1023:                            "remoteAddr can't be null");
1024:                if (0 == remoteAddr.length())
1025:                    throw new IllegalArgumentException(
1026:                            "remoteAddr can't be empty");
1027:
1028:                mRemoteAddr = remoteAddr;
1029:            }
1030:
1031:            /**
1032:             * Set the remote address of this request.
1033:             *
1034:             * @param remoteAddr the remote address to set
1035:             * @return this <code>MockRequest</code> instance
1036:             * @since 1.1
1037:             */
1038:            public MockRequest remoteAddr(String remoteAddr) {
1039:                setRemoteAddr(remoteAddr);
1040:
1041:                return this ;
1042:            }
1043:
1044:            public String getRemoteUser() {
1045:                return mRemoteUser;
1046:            }
1047:
1048:            /**
1049:             * Set the remote user of this request.
1050:             * <p>The default remote user is <code>null</code>.
1051:             *
1052:             * @param remoteUser the remote user to set
1053:             * @since 1.1
1054:             */
1055:            public void setRemoteUser(String remoteUser) {
1056:                if (null == remoteUser)
1057:                    throw new IllegalArgumentException(
1058:                            "remoteUser can't be null");
1059:                if (0 == remoteUser.length())
1060:                    throw new IllegalArgumentException(
1061:                            "remoteUser can't be empty");
1062:
1063:                mRemoteUser = remoteUser;
1064:            }
1065:
1066:            /**
1067:             * Set the remote user of this request.
1068:             *
1069:             * @param remoteUser the remote user to set
1070:             * @return this <code>MockRequest</code> instance
1071:             * @since 1.1
1072:             */
1073:            public MockRequest remoteUser(String remoteUser) {
1074:                setRemoteUser(remoteUser);
1075:
1076:                return this ;
1077:            }
1078:
1079:            public String getRemoteHost() {
1080:                return mRemoteHost;
1081:            }
1082:
1083:            /**
1084:             * Set the remote host of this request.
1085:             * <p>The default remote host is "<code>localhost</code>".
1086:             *
1087:             * @param remoteHost the remote host to set
1088:             * @since 1.1
1089:             */
1090:            public void setRemoteHost(String remoteHost) {
1091:                if (null == remoteHost)
1092:                    throw new IllegalArgumentException(
1093:                            "remoteHost can't be null");
1094:                if (0 == remoteHost.length())
1095:                    throw new IllegalArgumentException(
1096:                            "remoteHost can't be empty");
1097:
1098:                mRemoteHost = remoteHost;
1099:            }
1100:
1101:            /**
1102:             * Set the remote host of this request.
1103:             *
1104:             * @param remoteHost the remote host to set
1105:             * @return this <code>MockRequest</code> instance
1106:             * @since 1.1
1107:             */
1108:            public MockRequest remoteHost(String remoteHost) {
1109:                setRemoteHost(remoteHost);
1110:
1111:                return this ;
1112:            }
1113:
1114:            public String getScheme() {
1115:                return mMockConversation.getScheme();
1116:            }
1117:
1118:            public String getServerName() {
1119:                return mMockConversation.getServerName();
1120:            }
1121:
1122:            public int getServerPort() {
1123:                return mMockConversation.getServerPort();
1124:            }
1125:
1126:            public String getContextPath() {
1127:                return mMockConversation.getContextPath();
1128:            }
1129:
1130:            public boolean isSecure() {
1131:                return mSecure;
1132:            }
1133:
1134:            /**
1135:             * Set whether this request is secure.
1136:             * <p>A request is not secure by default.
1137:             *
1138:             * @param secure <code>true</code> if this request is secure; or
1139:             * <p><code>false</code> otherwise
1140:             * @since 1.1
1141:             */
1142:            public void setSecure(boolean secure) {
1143:                mSecure = secure;
1144:            }
1145:
1146:            /**
1147:             * Set whether this request is secure.
1148:             *
1149:             * @param secure <code>true</code> if this request is secure; or
1150:             * <p><code>false</code> otherwise
1151:             * @return this <code>MockRequest</code> instance
1152:             * @since 1.1
1153:             */
1154:            public MockRequest secure(boolean secure) {
1155:                setSecure(secure);
1156:
1157:                return this ;
1158:            }
1159:
1160:            public HttpSession getSession(boolean create) {
1161:                if (mSession != null && mSession.isValid()) {
1162:                    return mSession;
1163:                }
1164:
1165:                mSession = null;
1166:
1167:                String id = getRequestedSessionId();
1168:
1169:                if (id != null) {
1170:                    mSession = mMockConversation.getSession(id);
1171:                    if (null == mSession && !create) {
1172:                        return null;
1173:                    }
1174:                }
1175:
1176:                if (mSession == null && create) {
1177:                    mSession = newSession();
1178:                }
1179:
1180:                return mSession;
1181:            }
1182:
1183:            public HttpSession getSession() {
1184:                HttpSession session = getSession(true);
1185:                return session;
1186:            }
1187:
1188:            void setRequestedSessionId(String pathParams) {
1189:                mRequestedSessionId = null;
1190:
1191:                // try cookies first
1192:                Cookie[] cookies = getCookies();
1193:                if (cookies != null && cookies.length > 0) {
1194:                    for (int i = 0; i < cookies.length; i++) {
1195:                        if (MockConversation.SESSION_ID_COOKIE
1196:                                .equalsIgnoreCase(cookies[i].getName())) {
1197:                            if (mRequestedSessionId != null) {
1198:                                // Multiple jsessionid cookies. Probably due to
1199:                                // multiple paths and/or domains. Pick the first
1200:                                // known session or the last defined cookie.
1201:                                if (mMockConversation
1202:                                        .getSession(mRequestedSessionId) != null) {
1203:                                    break;
1204:                                }
1205:                            }
1206:
1207:                            mRequestedSessionId = cookies[i].getValue();
1208:                            mSessionIdState = SESSIONID_COOKIE;
1209:                        }
1210:                    }
1211:                }
1212:
1213:                // check if there is a url encoded session param.
1214:                if (pathParams != null
1215:                        && pathParams
1216:                                .startsWith(MockConversation.SESSION_ID_URL)) {
1217:                    String id = pathParams
1218:                            .substring(MockConversation.SESSION_ID_URL.length() + 1);
1219:
1220:                    if (null == mRequestedSessionId) {
1221:                        mRequestedSessionId = id;
1222:                        mSessionIdState = SESSIONID_URL;
1223:                    }
1224:                }
1225:
1226:                if (null == mRequestedSessionId) {
1227:                    mSessionIdState = SESSIONID_NONE;
1228:                }
1229:            }
1230:
1231:            String getRequestedSessionId() {
1232:                return mRequestedSessionId;
1233:            }
1234:
1235:            MockSession newSession() {
1236:                MockSession session = mMockConversation.newHttpSession();
1237:                Cookie cookie = new Cookie(MockConversation.SESSION_ID_COOKIE,
1238:                        session.getId());
1239:                cookie.setPath("/");
1240:                cookie.setMaxAge(-1);
1241:
1242:                mMockResponse.addCookie(cookie);
1243:
1244:                return session;
1245:            }
1246:
1247:            boolean isRequestedSessionIdFromCookie() {
1248:                return SESSIONID_COOKIE.equals(mSessionIdState);
1249:            }
1250:
1251:            public RequestDispatcher getRequestDispatcher(String url) {
1252:                return null;
1253:            }
1254:
1255:            public HttpServletRequest getHttpServletRequest() {
1256:                return null;
1257:            }
1258:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.