1   /*
2    *  Copyright 2006 Simon Raess
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.beep4j.integration;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  
22  import net.sf.beep4j.CloseChannelCallback;
23  import net.sf.beep4j.Message;
24  import net.sf.beep4j.MessageBuilder;
25  import net.sf.beep4j.Reply;
26  import net.sf.beep4j.ext.ChannelHandlerAdapter;
27  
28  public class EchoProfileHandler extends ChannelHandlerAdapter {
29  	
30  	public static final String PROFILE = "http://www.iserver.ch/profiles/echo";
31  	
32  	public void closeRequested(CloseChannelCallback callback) {
33  		callback.closeAccepted();
34  	}
35  
36  	public void messageReceived(Message message, Reply handler) {
37  		InputStream stream = message.getInputStream();
38  		MessageBuilder builder = handler.createMessageBuilder();
39  		OutputStream os = builder.getOutputStream();
40  		writeTo(stream, os);
41  		handler.sendRPY(builder.getMessage());
42  	}
43  	
44  	private void writeTo(InputStream is, OutputStream os) {
45  		int i;
46  		
47  		try {
48  			while ((i = is.read()) != -1) {
49  				os.write(i);
50  			}
51  		
52  			os.close();
53  		} catch (IOException e) {
54  			// TODO: change this
55  			throw new RuntimeException(e);
56  		}
57  	}
58  
59  }