001 /** 002 * 003 * Copyright 2004 Protique Ltd 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 **/ 018 package org.codehaus.activesoap; 019 020 import org.codehaus.activesoap.handler.QNameHandler; 021 import org.codehaus.activesoap.handler.Policy; 022 import org.codehaus.activesoap.handler.DefaultHandlerRegistry; 023 import org.codehaus.activesoap.util.XMLStreamFactory; 024 import org.codehaus.activesoap.util.NullXMLStreamWriter; 025 026 import javax.xml.stream.XMLStreamReader; 027 import javax.xml.stream.XMLStreamWriter; 028 import javax.xml.stream.XMLStreamException; 029 import java.io.Reader; 030 import java.io.Writer; 031 032 /** 033 * A pure RESTful service which processes inbound XML using some {@link Handler} 034 * instances without using the SOAP protocol. 035 * 036 * @version $Revision: 1.12 $ 037 */ 038 public class RestService { 039 private HandlerRegistry handlerRegistry = new DefaultHandlerRegistry(); 040 private Handler rootHandler; 041 private XMLStreamFactory streamFactory = new XMLStreamFactory(); 042 043 public RestService() { 044 this(new DefaultHandlerRegistry()); 045 } 046 047 public RestService(HandlerRegistry handlerRegistry) { 048 this.handlerRegistry = handlerRegistry; 049 this.rootHandler = new QNameHandler(handlerRegistry); 050 } 051 052 053 /** 054 * Creates a new MessageExchange from an input and output stream 055 */ 056 public MessageExchange createMessageExchange(XMLStreamReader in, XMLStreamWriter out) { 057 if (out == null) { 058 out = NullXMLStreamWriter.getInstance(); 059 } 060 return new MessageExchange(this, in, out); 061 } 062 063 /** 064 * Creates a new MessageExchange from an input and output stream 065 */ 066 public MessageExchange createMessageExchange(Reader request, Writer response) throws XMLStreamException { 067 XMLStreamReader in = getStreamFactory().getInputFactory().createXMLStreamReader(request); 068 XMLStreamWriter out = getStreamFactory().getOutputFactory().createXMLStreamWriter(response); 069 MessageExchange exchange = createMessageExchange(in, out); 070 return exchange; 071 } 072 073 /** 074 * Performs a in invocation using the given message exchange 075 */ 076 public void invoke(MessageExchange exchange) throws Exception { 077 getRootHandler().invoke(exchange); 078 } 079 080 /** 081 * Performs a SOAP RPC operation, returning the response 082 */ 083 public void invoke(Reader request, Writer response) throws Exception { 084 MessageExchange exchange = createMessageExchange(request, response); 085 invoke(exchange); 086 } 087 088 /** 089 * Performs a SOAP RPC operation, returning the response 090 */ 091 public void invoke(XMLStreamReader in, XMLStreamWriter out) throws Exception { 092 getRootHandler().invoke(createMessageExchange(in, out)); 093 } 094 095 096 public void addPolicy(Policy policy) { 097 policy.addPolicy(getHandlerRegistry()); 098 } 099 100 public void removePolicy(Policy policy) { 101 policy.removePolicy(getHandlerRegistry()); 102 } 103 104 // Properties 105 //------------------------------------------------------------------------- 106 public HandlerRegistry getHandlerRegistry() { 107 return handlerRegistry; 108 } 109 110 public Handler getRootHandler() { 111 return rootHandler; 112 } 113 114 public void setRootHandler(Handler rootHandler) { 115 this.rootHandler = rootHandler; 116 } 117 118 public XMLStreamFactory getStreamFactory() { 119 return streamFactory; 120 } 121 122 public void setStreamFactory(XMLStreamFactory streamFactory) { 123 this.streamFactory = streamFactory; 124 } 125 126 public boolean isRepairingNamespace() { 127 return streamFactory.isRepairingNamespace(); 128 } 129 130 public void setRepairingNamespace(boolean flag) { 131 streamFactory.setRepairingNamespace(flag); 132 } 133 }