Wiki source code of Development-Tips and Tricks
Last modified by Johann Werner on 2011/11/30 09:44
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
7.1 | 1 | == URL's == |
![]() |
3.1 | 2 | |
3 | * wocontext.request().uri() = the URL currently being requested | ||
4 | |||
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 | |||
![]() |
9.1 | 7 | * WOApplication.application().applicationBaseURL() = /WebObjects |
![]() |
3.1 | 8 | * WOApplication.application().baseURL() = /WebObjects |
![]() |
10.1 | 9 | * WOApplication.application().cgiAdaptorURL() = [[http:~~/~~/hostname/cgi-bin/WebObjects>>url:http://hostname/cgi-bin/WebObjects||shape="rect"]] |
10 | * WOApplication.application().directConnectURL() = [[http:~~/~~/hostname:port/cgi-bin/WebObjects/MyApplication.woa>>url:http://hostname:port/cgi-bin/WebObjects/MyApplication.woa||shape="rect"]] | ||
![]() |
3.1 | 11 | * WOApplication.application().frameworksBaseURL() = /WebObjects/Frameworks |
![]() |
9.1 | 12 | * WOApplication.application().host() = hostname |
![]() |
10.1 | 13 | * WOApplication.application().servletConnectURL() = [[http:~~/~~/hostname/cgi-bin/WebObjects/MyApplication.woa>>url:http://hostname/cgi-bin/WebObjects/MyApplication.woa||shape="rect"]] |
14 | * WOApplication.application().webserverConnectURL() = [[http:~~/~~/hostname/cgi-bin/WebObjects/MyApplication.woa/-port>>url:http://hostname/cgi-bin/WebObjects/MyApplication.woa/-port||shape="rect"]] | ||
![]() |
3.1 | 15 | |
![]() |
7.1 | 16 | == Browser IP == |
![]() |
3.1 | 17 | |
![]() |
7.1 | 18 | {{code}} |
![]() |
3.1 | 19 | |
![]() |
7.1 | 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 | } | ||
![]() |
3.1 | 42 | |
![]() |
7.1 | 43 | {{/code}} |
![]() |
3.1 | 44 | |
![]() |
7.1 | 45 | == NSArray == |
![]() |
3.1 | 46 | |
47 | 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 | ||
48 | |||
49 | {{panel}} | ||
![]() |
7.1 | 50 | objectAtIndex() |
![]() |
3.1 | 51 | {{/panel}} |
52 | |||
53 | method. So what does | ||
54 | |||
55 | {{panel}} | ||
![]() |
7.1 | 56 | NSArray.valueForKey(String key) |
![]() |
3.1 | 57 | {{/panel}} |
58 | |||
59 | return? | ||
60 | |||
![]() |
10.1 | 61 | Well, first read the docs: [[file:~~/~~//OSX/Developer/Documentation/WebObjects/Reference/com/webobjects/foundation/NSArray.html#valueForKey(java.lang.String>>url:file:///OSX/Developer/Documentation/WebObjects/Reference/com/webobjects/foundation/NSArray.html#valueForKey(java.lang.String||shape="rect"]]) |
![]() |
3.1 | 62 | |
63 | 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... | ||
64 | |||
![]() |
7.1 | 65 | == HTML-friendly String Truncating == |
![]() |
3.1 | 66 | |
![]() |
7.1 | 67 | {{code}} |
![]() |
3.1 | 68 | |
![]() |
7.1 | 69 | import org.apache.commons.lang.*; //From Apache |
70 | import org.clapper.util.text.*; // From http://www.clapper.org/ | ||
![]() |
3.1 | 71 | |
![]() |
7.1 | 72 | public static String stripHTMLTagsAndConcatenate(String htmlString, int numberOfChar) { |
73 | return (StringUtils.substringBeforeLast(StringUtils.abbreviate((HTMLUtil.stripHTMLTags(htmlString)), numberOfChar), " ")) + "..."; | ||
74 | } | ||
![]() |
3.1 | 75 | |
![]() |
7.1 | 76 | {{/code}} |