Changes for page Development-Tips and Tricks
Last modified by Johann Werner on 2011/11/30 09:44
From version 5.1
edited by smmccraw
on 2007/07/08 09:46
on 2007/07/08 09:46
Change comment:
There is no comment for this version
To version 9.1
edited by Johann Werner
on 2011/11/30 09:44
on 2011/11/30 09:44
Change comment:
There is no comment for this version
Summary
-
Page properties (3 modified, 0 added, 0 removed)
Details
- Page properties
-
- Title
-
... ... @@ -1,1 +1,1 @@ 1 - Programming__WebObjects-Web Applications-Development-Tips and Tricks1 +Development-Tips and Tricks - Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. smmccraw1 +XWiki.jw - Content
-
... ... @@ -1,53 +1,54 @@ 1 -== URL's 1 +== URL's == 2 2 3 3 * wocontext.request().uri() = the URL currently being requested 4 4 5 5 There are several different URL's associated with your application, all of which can be retrieved from various methods on WOApplication. Here is a quick cheat sheet of them: 6 6 7 -* WOApplication.application().baseURL() = /WebObjects 8 8 * WOApplication.application().applicationBaseURL() = /WebObjects 9 -* WOApplication.application().cgiAdaptorURL() = http:~/~/hostname/cgi-bin/WebObjects 10 -* WOApplication.application().directConnectURL() = http:~/~/hostname:port/cgi-bin/WebObjects/MyApplication.woa 8 +* WOApplication.application().baseURL() = /WebObjects 9 +* WOApplication.application().cgiAdaptorURL() = [[http://hostname/cgi-bin/WebObjects]] 10 +* WOApplication.application().directConnectURL() = [[http://hostname:port/cgi-bin/WebObjects/MyApplication.woa]] 11 11 * WOApplication.application().frameworksBaseURL() = /WebObjects/Frameworks 12 -* WOApplication.application().servletConnectURL() = http:~/~/hostname/cgi-bin/WebObjects/MyApplication.woa 13 -* WOApplication.application().webserverConnectURL() = http:~/~/hostname/cgi-bin/WebObjects/MyApplication.woa/-port 12 +* WOApplication.application().host() = hostname 13 +* WOApplication.application().servletConnectURL() = [[http://hostname/cgi-bin/WebObjects/MyApplication.woa]] 14 +* WOApplication.application().webserverConnectURL() = [[http://hostname/cgi-bin/WebObjects/MyApplication.woa/-port]] 14 14 15 -== Browser IP 16 +== Browser IP == 16 16 17 -{{ panel}}18 +{{code}} 18 18 19 - 20 - 21 - 22 - 23 - 24 - 25 - 26 - 27 - 28 - 29 - 30 - 31 - 32 - 33 - 34 - 35 - 36 - 37 - 38 - 39 - 40 - 20 +/** Returns the IP address of the client. 21 + * This should return accurate information whether in direct connect or webserver deployment mode. 22 + * If performance caching is turned on on OS X server, this method will correctly use pc-remote-addr 23 + * @return The IP address as a string. 24 + */ 25 + public static String clientIP(WORequest request) { 26 + Object ipAddress = request.headerForKey("pc-remote-addr"); 27 + if (ipAddress == null) { 28 + ipAddress = request.headerForKey("remote_addr"); 29 + if( ipAddress == null ) { 30 + ipAddress = request.headerForKey("remote_host"); 31 + if( ipAddress == null ) { 32 + ipAddress = request._remoteAddress(); 33 + if( ipAddress == null ) { 34 + ipAddress = request._originatingAddress(); 35 + if( ipAddress != null ) ipAddress = ((InetAddress)ipAddress).getHostAddress(); 36 + } 37 + } 38 + } 39 + } 40 + return ipAddress == null ? "<address unknown>" : ipAddress.toString(); 41 + } 41 41 42 -{{/ panel}}43 +{{/code}} 43 43 44 -== NSArray 45 +== NSArray == 45 45 46 46 It's in the docs, but NSArray's implementation of KeyValueCoding is not really what I was expecting. To get an object at a specific numeric index of an NSArray, you'd use the 47 47 48 48 {{panel}} 49 49 50 - 51 +objectAtIndex() 51 51 52 52 {{/panel}} 53 53 ... ... @@ -55,27 +55,25 @@ 55 55 56 56 {{panel}} 57 57 58 - 59 +NSArray.valueForKey(String key) 59 59 60 60 {{/panel}} 61 61 62 62 return? 63 63 64 -Well, first read the docs: file: ~/~//OSX/Developer/Documentation/WebObjects/Reference/com/webobjects/foundation/NSArray.html#valueForKey(java.lang.String)65 +Well, first read the docs: [[file:///OSX/Developer/Documentation/WebObjects/Reference/com/webobjects/foundation/NSArray.html#valueForKey(java.lang.String]]) 65 65 66 66 It turns out that calling valueForKey on an array is the same as calling valueForKey for each element of that array. So if you have an NSArray of Users, calling valueForKey("email"); will return an NSArray of email addresses. calling valueForKey("documents"); will return an NSArray of NSArrays containing document objects. In hindsight (and from looking at the way WOBuilder handles key paths for arrays) this is kind of obvious. But I think the real lesson here is that it is easy to ignore the docs towards the end of an alphabetical page... 67 67 68 -== HTML-friendly String Truncating 69 +== HTML-friendly String Truncating == 69 69 70 -{{ panel}}71 +{{code}} 71 71 72 - import org.apache.commons.lang.*; //From Apache 73 - import org.clapper.util.text.*; // From http://www.clapper.org/ 74 - 75 - public static String stripHTMLTagsAndConcatenate(String htmlString, int numberOfChar) { 76 - return (StringUtils.substringBeforeLast(StringUtils.abbreviate((HTMLUtil.stripHTMLTags(htmlString)), numberOfChar), " ")) + "..."; 77 - } 73 +import org.apache.commons.lang.*; //From Apache 74 + import org.clapper.util.text.*; // From http://www.clapper.org/ 78 78 79 -{{/panel}} 76 + public static String stripHTMLTagsAndConcatenate(String htmlString, int numberOfChar) { 77 + return (StringUtils.substringBeforeLast(StringUtils.abbreviate((HTMLUtil.stripHTMLTags(htmlString)), numberOfChar), " ")) + "..."; 78 + } 80 80 81 - Category:WebObjects80 +{{/code}}