2014年6月30日 星期一

[ Java 常見問題 ] Stackoverflow : how to encode URL to avoid special characters in java

來源自 這裡
Question:
I need java code to encode URL to avoid special characters such as spaces and % and & ...etc

Answer:
URL construction is tricky because different parts of the URL have different rules for what characters are allowed: for example, the plus sign is reserved in the query component of a URL because it represents a space, but in the path component of the URL, a plus sign has no special meaning and spaces are encoded as "%20".

RFC 2396 explains (in section 2.4.2) that a complete URL is always in its encoded form: you take the strings for the individual components (scheme, authority, path, etc.), encode each according to its own rules, and then combine them into the complete URL string. Trying to build a complete unencoded URL string and then encode it separately leads to subtle bugs, like spaces in the path being incorrectly changed to plus signs (which an RFC-compliant server will interpret as real plus signs, not encoded spaces).

In Java, the correct way to build a URL is with the URI class. Use one of the multi-argument constructors that takes the URL components as separate strings,


and it'll escape each component correctly according to that component's rules. The toASCIIString() method gives you a properly-escaped and encoded string that you can send to a server. To decode a URL, construct a URI object using the single-string constructor and then use the accessor methods (such as getPath()) to retrieve the decoded components.

Don't use the URLEncoder class! Despite the name, that class actually does HTML form encoding, not URL encoding. It's not correct to concatenate unencoded strings to make an "unencoded" URL and then pass it through a URLEncoder. Doing so will result in problems (particularly the aforementioned one regarding spaces and plus signs in the path). Below is the sample code:
  1. URI uri1 = new URI("http""abc.com.tw:8080""/path/john .html""name=john&age=18""");  
  2. System.out.printf("%s\n", uri1.toASCIIString()); // Encode  
  3. // Decode  
  4. System.out.printf("\t[Info] Scheme='%s'\n", uri1.getScheme());  
  5. System.out.printf("\t[Info] Port='%d'\n", uri1.getPort());  
  6. System.out.printf("\t[Info] Host='%s'\n", uri1.getHost());  
  7. System.out.printf("\t[Info] Path='%s'\n", uri1.getPath());  
  8. System.out.printf("\t[Info] Query='%s'\n", uri1.getQuery());  
  9. System.out.printf("\t[Info] Fragment='%s'\n", uri1.getFragment());  
  10. URI uri2 = new URI("https://tw-mg31.mail.yahoo.com/neo/launch?.rand=60qu1ck5a44dr");  
  11. System.out.printf("%s\n", uri2.toASCIIString()); // Encode  
  12. // Decode  
  13. System.out.printf("\t[Info] Scheme='%s'\n", uri2.getScheme());  
  14. System.out.printf("\t[Info] Port='%d'\n", uri2.getPort());  
  15. System.out.printf("\t[Info] Host='%s'\n", uri2.getHost());  
  16. System.out.printf("\t[Info] Path='%s'\n", uri2.getPath());  
  17. System.out.printf("\t[Info] Query='%s'\n", uri2.getQuery());  
  18. System.out.printf("\t[Info] Fragment='%s'\n", uri2.getFragment());  
輸出結果:


2014年6月16日 星期一

[ InAction Note ] Ch1. Introduction - Getting started with ActiveMQ

Preface: 
Getting started with ActiveMQ isn’t difficult. You simply need to start up the broker and make sure that it’s capable of accepting connections and sending messages. ActiveMQ comes with some simple examples that will help you with this task, but first we need to install Java and download ActiveMQ. 

In this section, you’ll download and install ActiveMQ, examine the ActiveMQ directory, and start up ActiveMQ for the first time. (Assume Java SE is ready

Downloading ActiveMQ: 
ActiveMQ is available from the Apache ActiveMQ website at the following URL: http://activemq.apache.org/download.html. Click on the link to the 5.4.1 release and you’ll find both tarball and zip formats available (the tarball is for Linux and Unix; the zip is for Windows). Once you’ve downloaded one of the archives, expand it and you’re ready to move along. Once you get to this point, you should have the Java SE all set up and working correctly, and you’re ready to take a peek at the ActiveMQ directory. 

Examining the ActiveMQ directory: 
From the command line, move into the apache-activemq-5.x.x directory and enter the command shown here. 
- Listing 1.2 List the contents of the ActiveMQ directory 
 

The contents of the directory are fairly straightforward: 
- LICENSE 
A file required by the Apache Software Foundation (ASF) for legal purposes; contains the licenses of all libraries used by ActiveMQ.

- NOTICE 
Another ASF-required file for legal purposes; it contains copyright information of all libraries used by ActiveMQ.

- README.txt 
A file containing some URLs to documentation to get new users started with ActiveMQ.

- WebConsole-README.txt 
Contains information about using the ActiveMQ web console.

- activemq-all-5.4.1.jar 
A jar file that contains all of ActiveMQ; it’s placed here for convenience if you need to grab it and use it.

- bin 
The bin directory contains binary/executable files for ActiveMQ; the startup scripts live in this directory.

- conf 
The conf directory holds all the configuration information for ActiveMQ.

- data 
The data directory is where the log files and message persistence data is stored.

- docs 
Contains a simple index.html file referring to the ActiveMQ website.

- example 
The ActiveMQ examples; these are what we’ll use shortly to test out ActiveMQ quickly.

- lib 
The lib directory holds all the libraries needed by ActiveMQ.

- user-guide.html 
A brief guide to starting up ActiveMQ and running the examples.

- webapps 
The webapps directory holds the ActiveMQ web console and some other web-related demos.

The next task is to start up ActiveMQ and verify it using the examples. 

Starting up ActiveMQ: 
After downloading and expanding the archive, ActiveMQ is ready for use. The binary distribution provides a basic configuration to get you started easily and that’s what we’ll use in the examples. So start up ActiveMQ now as shown next. 
- Listing 1.3 Start up ActiveMQ 
$ ./bin/activemq console
INFO: Using default configuration
(you can configure options in one of these file: /etc/default/activemq /home/nlg/.activemqrc)

INFO: Invoke the following command to create a configuration file
./bin/activemq setup [ /etc/default/activemq | /home/nlg/.activemqrc ]
...
Loading message broker from: xbean:activemq.xml
INFO | Refreshing org.apache.activemq.xbean.XBeanBrokerFactory$1@d09ad3: startup date [Wed Jun 26 14:36:58 CST 2013]; root of context hierarchy
INFO | PListStore:[/root/ActiveMQ/apache-activemq-5.8.0/data/localhost/tmp_storage] started
INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[/root/ActiveMQ/apache-activemq-5.8.0/data/kahadb]
INFO | Apache ActiveMQ 5.8.0 (localhost, ID:nlg-System-Product-Name-56894-1372228619131-0:1) is starting
INFO | Listening for connections at: tcp://nlg-System-Product-Name:61616?maximumConnect...eformat.maxFrameSize=104857600
INFO | Connector openwire Started
INFO | Listening for connections at: amqp://nlg-System-Product-Name:5672?maximumConnect...eformat.maxFrameSize=104857600
INFO | Connector amqp Started
INFO | Apache ActiveMQ 5.8.0 (localhost, ID:nlg-System-Product-Name-56894-1372228619131-0:1) started
INFO | For help or more information please see: http://activemq.apache.org
INFO | Web console type: embedded
INFO | ActiveMQ WebConsole initialized.
INFO | Initializing Spring FrameworkServlet 'dispatcher'
INFO | jolokia-agent: No access restrictor found at classpath:/jolokia-access.xml, access to all MBeans is allowed

This command starts up the ActiveMQ broker and some of its connectors to expose it to clients via a few protocols, namely, TCP, SSL, STOMP, and XMPP. Just be aware that ActiveMQ has started and is available to clients via TCP on port 61616. This is all configurable and will be discussed later in chapter 4. For now, the preceding output tells you that ActiveMQ is up and running and ready for use. Now it’s ready to begin handling some messages. The best way to begin sending and receiving messages is by using some of the examples that come with ActiveMQ. The next section walks you through this in a step-by-step manner. 

Running your first examples with ActiveMQ: 
The previous section walked you through starting up ActiveMQ in one terminal. For verification of this, you should open two more terminals to run the ActiveMQ examples. In the second terminal, move into the example directory and look at its contents as shown in the following listing. 
- Listing 1.4 List the contents of the ActiveMQ example directory 
 

The example directory contains a few different items. Here’s a quick description of each item in that directory: 
- build.xml 
An Ant build configuration for use with the Java examples.

- conf 
The conf directory holds configuration information for use with the Java examples.

- perfharness 
The perfharness directory contains a script for running the IBM JMS performance harness against ActiveMQ.

- ruby 
The ruby directory contains some examples of using ActiveMQ with Ruby and the STOMP connector.

- src 
The src directory is where the Java examples live; this directory is used by the build.xml.

- transactions 
The transactions directory holds an ActiveMQ implementation of the TransactedExample from Sun’s JMS Tutorial.

Using the second terminal, start up a JMS consumer as shown here. 
- Listing 1.5 Start up the ActiveMQ consumer example 
 

The command compiles the Java examples and starts up a simple JMS consumer. As you can see from the output, this consumer is 
* Connecting to the broker using the TCP protocol (tcp://localhost:61616)
* Watching a queue named TEST.FOO
* Using nondurable subscription
* Waiting to receive 2000 messages before shutting down

Basically, the JMS consumer is connected to ActiveMQ and waiting for messages. Now you can send some messages to the TEST.FOO destination. 

In the third terminal, move into the example directory and start up a JMS producer as shown below. This will immediately begin to send messages. 
- Listing 1.6 Start up the ActiveMQ producer example 
[example]$ ant producer
Buildfile: build.xml
init:
compile:
producer:
[echo] Running producer against server at $url =
tcp://localhost:61616 for subject $subject = TEST.FOO
[java] Connecting to URL: tcp://localhost:61616
[java] Publishing a Message with size 1000 to queue: TEST.FOO
[java] Using non-persistent messages
[java] Sleeping between publish 0 ms
[java] Running 1 parallel threads
[java] [Thread-2] Sending message: 'Message: 0 sent at: Thu Oct 14
21:24:07 MDT 2010 ...'
[java] [Thread-2] Sending message: 'Message: 1 sent at: Thu Oct 14
21:24:07 MDT 2010 ...'
[java] [Thread-2] Sending message: 'Message: 2 sent at: Thu Oct 14
21:24:07 MDT 2010 ...'

Although the output has been truncated for readability, the command starts up a simple JMS producer and you can see from the output that it 
* Connects to the broker using the TCP connector (tcp://localhost:61616)
* Publishes messages to a queue named TEST.FOO
* Uses nonpersistent messages
* Doesn’t sleep between publishing messages

Once the JMS producer is connected, it then sends 2,000 messages and shuts down. This is the number of messages the consumer is waiting to consume before it shuts down. So as the messages are being sent by the producer in terminal three, flip back to terminal two and watch the JMS consumer as it consumes those messages. Here’s the output you’ll see in terminal two: 
...
[java] [Thread-1] Received: 'Message: 1998 sent at: Wed Jun 26 14:53:08 CST 201...' (length 1000)
[java] [Thread-1] Received: 'Message: 1999 sent at: Wed Jun 26 14:53:08 CST 201...' (length 1000)
[java] [Thread-1] Closing connection
[java] All threads completed their work

BUILD SUCCESSFUL
Total time: 5 minutes 16 seconds

At this time, both the consumer and the producer should be shut down, but the ActiveMQ broker is still running in the first terminal. Take a look at the first terminal again and you’ll see that ActiveMQ appears to not have budged at all. This is because the default logging configuration doesn’t output anything beyond what’s absolutely necessary. If you’d like to tweak the logging configuration to output more information as messages are sent and received, you can do so. Logging will be covered further in chapter 14. 

Through the use of the Java examples that come with ActiveMQ, it has been proven that the broker is up and running and can mediate messages. This doesn’t seem like much but it’s an important first step. If you were able to successfully run the Java examples, then you know that you have no networking problems on the machine you’re using and you know that ActiveMQ is behaving properly. If you were unable to successfully run the Java examples, then you’ll need to troubleshoot the situation. If you need some help, heading over to the ActiveMQ mailing lists is the best way to find help. These examples are just to get you started but can be used to test many scenarios. Throughout the rest of the book, some different examples surrounding a couple of common use cases will be used to demonstrate ActiveMQ and its features. These examples are explained further in chapter 3.

[ In Action ] Working with builders - Building object trees with NodeBuilder

Preface: 
We start the more detailed explanation of builders with the same example we used in section 7.5.1 to demonstrate GPath: modeling Invoices with LineItems andProducts. We will build a runtime structure of nodes rather than specialized business objects and watch the building process closely. You will learn not only about how NodeBuilder works, but also how the general principle of builders is applied in Groovy. We will then consider how the declarative style of builder use can be freely mixed with normal logic. Builders can be used without any special knowledge, but in order to understand how they work, it is a prerequisite to know about pretended and relayedmethods (section 7.6) and closure scoping (section 5.5). 

Based on our invoice example from section 7.5.1, we set out to build a runtime object structure as depicted in figure 8.1. 
 

In listing 7.23, we built this runtime structure with three defined classes InvoiceLineItem, and Product and through calling their default constructors in a nested manner.

NodeBuilder in action—a closer look at builder code 
Listing 8.4 shows how to implement the invoice example using a NodeBuilder. The NodeBuilder can replace all three of our classes, assuming that we’re just treating them as data storage types (that is, we don’t need to add methods for business logic or other behavior). Also added is a final GPath expression to prove that we can still walk conveniently through the object graph. This is the same query we used in section 7.5.1. Note how the tree structure from figure 8.1 is reflected in the code! 
- Listing 8.4 Invoice example with NodeBuilder 
  1. def builder = new NodeBuilder()                         // 1) Builder creation  
  2. def ulcDate = new Date(107,0,1)  
  3. def invoices = builder.invoices{                        // 2) Root node creation  
  4.     invoice(date: ulcDate){                             // 3) Invoice creation  
  5.         item(count:5){  
  6.             product(name:'ULC', dollar:1499)  
  7.         }  
  8.         item(count:1){  
  9.             product(name:'Visual Editor', dollar:499)  
  10.         }  
  11.     }  
  12.     invoice(date: new Date(106,1,2)){  
  13.         item(count:4) {  
  14.             product(name:'Visual Editor', dollar:499)  
  15.         }  
  16.     }  
  17. }  
  18.   
  19. // 4) GPath query  
  20. def soldAt = invoices.grep {  
  21.     it.item.product.any{ it.'@name' == 'ULC' }  
  22. }.'@date'  
  23. assert soldAt == [ulcDate]  
We make a new instance of the NodeBuilder for later use at (1), and then we call the invoices method on the NodeBuilder instance at (2). This is a pretended method: The NodeBuilder intercepts the method call. It constructs a node based on the name of the intercepted method name and returns it into the invoices variable. Before the node is constructed, the trailing closure is called to construct its nested nodes. To make this possible, the BuilderSupport that NodeBuilder inherits from sets the closure’s delegate to the NodeBuilder instance. 

The invoice method call is relayed to the NodeBuilder instance in (3), because it is the current closure’s delegate. This method also takes a map as a parameter. The content of this map describes the attributes of the constructed node. 

Finally, we need to adapt the GPath a little to use it in (4). First, we’ve broken it into multiple lines to allow proper typesetting in the book. Second, node attributes are no longer accessible as properties but as map entries. Therefore, product.name now becomes product['@name'] or, even shorter, product.'@name'

The additional at-sign is used for denoting attributes in analogy to XPath attribute conventions. A third change is that through the general handling mechanism of nodes, item.product is now a list of products, not a single one. 

Understanding the builder concept: 
From the previous example, we extract the following general rules: 
* Nodes are constructed from pretended method calls on the builder.
* Method names determine node names.
* When a map is passed as a method argument, it determines the node’s attributes. Generally speaking, each key/value pair in the map is used to call the field’s setter method named by the key with the value. This refinement will later be used with SwingBuilder to register event listeners.
* Nesting of nodes is done with closures. Closures relay method calls to the builder.

This concept is an implementation of the Builder pattern (GOF). Instead of programming how some tree-like structure is built, only the result, the what, is specified. Thehow is left to the builder. Note that only simple attribute names can be declared in the attribute map without enclosing them in single or double quotes. Similarly, node names are constructed from method names, so if you need names that aren’t valid Groovy identifiers—such as x.y or x-yyou will again need to use quotes. So far, we have done pretty much the same as we did with hand-made classes, but without writing the extra code. This is already a useful advantage, but there is more to come. 

Smart building with logic: 
With builders, you can mix declarative style and Groovy logic, as listing 8.5 shows. We create nested invoices in a loop for three consecutive days, with sales of the product growing each day. To assess the result, we use a pretty-printing facility available for nodes. 
- Listing 8.5 Using logic inside the NodeBuilder 
  1. System.setProperty("user.timezone","CET")  
  2. def builder = new NodeBuilder()  
  3. def invoices = builder.invoices {  
  4.     for(day in 1..3) {  
  5.         invoice(date: new Date(107,0,day)){  
  6.             item(count:day){  
  7.                 product(name:'ULC', dollar:1499)  
  8.             }  
  9.         }  
  10.     }  
  11. }  
  12. def writer = new StringWriter()  
  13. invoices.print(new PrintWriter(writer))  
  14. def result = writer.toString().replaceAll("\r","")  
  15. assert "\n"+result == """  
  16. invoices() {  
  17.   invoice(date:Mon Jan 01 00:00:00 CET 2007) {  
  18.     item(count:1) {  
  19.       product(name:'ULC', dollar:1499)  
  20.     }  
  21.   }  
  22.   invoice(date:Tue Jan 02 00:00:00 CET 2007) {  
  23.     item(count:2) {  
  24.       product(name:'ULC', dollar:1499)  
  25.     }  
  26.   }  
  27.   invoice(date:Wed Jan 03 00:00:00 CET 2007) {  
  28.     item(count:3) {  
  29.       product(name:'ULC', dollar:1499)  
  30.     }  
  31.   }  
  32. }  
  33. """   
Nodes as constructed with the NodeBuilder have some interesting methods, as listed in table 8.1. Note that these methods being present on the nodes doesn’t prevent you from having nodes of the same name (such as a node called iterator)—you build child nodes by calling methods on the NodeBuilder , not on the nodes themselves. For a complete and up-to-date description, look at Node ’s API documentation at http://groovy.codehaus.org/apidocs/groovy/util/Node.html
 

Nodes are used throughout the Groovy library for transparently storing tree-like structures. You will see further usages with XmlParser in section 12.1.2

With this in mind, you may want to have some fun by typing: 
  1. println invoices.depthFirst()*.name()  
That’s all there is to NodeBuilder. It makes a representative example for all builders in the sense that whenever you use a builder, you create a builder instance and call methods on it with attached nested closures that result in an object tree. 

[ Python 文章收集 ] Timing and Profiling in IPython

Source From  Here   Preface   Timing and profiling code is all sorts of useful, and it’s also just good ol’ fashioned fun ( and sometimes s...