View Javadoc

1   /*
2      Licensed under the Apache License, Version 2.0 (the "License");
3      you may not use this file except in compliance with the License.
4      You may obtain a copy of the License at
5   
6        http://www.apache.org/licenses/LICENSE-2.0
7   
8      Unless required by applicable law or agreed to in writing, software
9      distributed under the License is distributed on an "AS IS" BASIS,
10     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11     See the License for the specific language governing permissions and
12     limitations under the License.
13  */
14  package uk.nhs.interoperability.service;
15  
16  import uk.nhs.interoperability.capabilities.DirectoryOfServices;
17  import uk.nhs.interoperability.infrastructure.ITKAddress;
18  import uk.nhs.interoperability.infrastructure.ITKServiceImpl;
19  import uk.nhs.interoperability.transport.ITKTransportRoute;
20  import uk.nhs.interoperability.transport.ITKTransportRouteImpl;
21  import uk.nhs.interoperability.util.ITKDirectoryProperties;
22  import uk.nhs.interoperability.util.ITKServiceProperties;
23  import uk.nhs.interoperability.util.Logger;
24  
25  /**
26   * The Class ITKSimpleDOS.
27   *
28   * @author Michael Odling-Smee
29   * @author Nicholas Jones
30   * @since 0.1
31   */
32  public class ITKSimpleDOS implements DirectoryOfServices {
33  
34  	/** The Constant IS_SUPPORTED. */
35  	private static final String IS_SUPPORTED = "supported";
36  	
37  	/** The Constant SUPPORTS_ASYNC. */
38  	private static final String SUPPORTS_ASYNC = "supportsAsync";
39  	
40  	/** The Constant SUPPORTS_SYNC. */
41  	private static final String SUPPORTS_SYNC = "supportsSync";
42  	
43  	/** The Constant IS_BASE64. */
44  	private static final String IS_BASE64 = "isBase64";
45  	
46  	/** The Constant MIME_TYPE. */
47  	private static final String MIME_TYPE = "mimeType";
48  
49  	/** The Constant CHANNELID. */
50  	private static final String CHANNELID = "channelid";
51  	
52  	/** The Constant TIME_TO_LIVE. */
53  	private static final String TIME_TO_LIVE = "TimeToLive";
54  	
55  	/** The Constant WRAPPER_TYPE. */
56  	private static final String WRAPPER_TYPE = "WrapperType";
57  	
58  	/** The Constant DESTINATION_TYPE. */
59  	private static final String DESTINATION_TYPE = "DestinationType";
60  	
61  	/** The Constant EXCEPTION_TO. */
62  	private static final String EXCEPTION_TO = "ExceptionTo";
63  	
64  	/** The Constant DEFAULT. */
65  	private static final String DEFAULT = "DEFAULT";
66  	
67  	/** The Constant REPLY_TO. */
68  	private static final String REPLY_TO = "ReplyTo";
69  	
70  	/** The Constant ROUTE_TYPE. */
71  	private static final String ROUTE_TYPE = "RouteType";
72  	
73  	/** The Constant PHYSICAL_DESTINATION. */
74  	private static final String PHYSICAL_DESTINATION = "PhysicalDestination";
75  
76  	/* (non-Javadoc)
77  	 * @see uk.nhs.interoperability.capabilities.DirectoryOfServices#resolveDestination(java.lang.String, uk.nhs.interoperability.infrastructure.ITKAddress)
78  	 */
79  	@Override
80  	public ITKTransportRoute resolveDestination(String serviceId, ITKAddress address) {
81  		
82  		ITKTransportRoute route = null;
83  		
84  		String svc = serviceId;
85  		String add = address.getURI();
86  		String channelKey = svc + "." + add + "." + CHANNELID;
87  		Logger.trace("Channel Key:"+channelKey);
88  		
89  		String channelId = ITKDirectoryProperties.getProperty(channelKey);
90  		
91  		if (null!=channelId){
92  			Logger.trace("Channel Id:"+channelId);
93  			String physicalDestination = getDirectoryProperty(channelId,PHYSICAL_DESTINATION);
94  			String routeType = getDirectoryProperty(channelId,ROUTE_TYPE);
95  			String replyTo = getDirectoryProperty(channelId,REPLY_TO);
96  			String exceptionTo = getDirectoryProperty(channelId,EXCEPTION_TO);
97  			String destinationType = getDirectoryProperty(channelId,DESTINATION_TYPE);
98  			String wrapperType = getDirectoryProperty(channelId,WRAPPER_TYPE);
99  			String timeToLive = getDirectoryProperty(channelId,TIME_TO_LIVE);
100 
101 			// time to live
102 			int ttl = 30*60;
103 			if (null!=timeToLive){
104 				ttl = Integer.parseInt(timeToLive);
105 			}
106 			route = new ITKTransportRouteImpl(routeType,physicalDestination,
107 											  replyTo,exceptionTo,destinationType,wrapperType,ttl);
108 		}
109 		
110 		return route;
111 	}
112 
113 	/**
114 	 * Gets the directory property.
115 	 *
116 	 * @param channelId the channel id
117 	 * @param propertyName the property name
118 	 * @return the directory property
119 	 */
120 	private String getDirectoryProperty(String channelId, String propertyName){
121 		String propertyValue = ITKDirectoryProperties.getProperty(channelId+"."+propertyName);
122 		if (propertyValue==null){
123 			propertyValue = ITKDirectoryProperties.getProperty(DEFAULT+"."+propertyName);
124 		}
125 		if (propertyValue==null){
126 			propertyValue = "";
127 		}
128 		return propertyValue;
129 	}
130 
131 	/* (non-Javadoc)
132 	 * @see uk.nhs.interoperability.capabilities.DirectoryOfServices#getService(java.lang.String)
133 	 */
134 	public ITKService getService(String serviceId) {
135 		
136 		ITKServiceImpl service = new ITKServiceImpl(serviceId);
137 
138 		boolean isSupported = getServiceBooleanProperty(serviceId, IS_SUPPORTED);
139 		if (!isSupported) {
140 			return null;
141 		}
142 		service.setBase64(getServiceBooleanProperty(serviceId, IS_BASE64));
143 		service.setSupportsSync(getServiceBooleanProperty(serviceId, SUPPORTS_SYNC));
144 		service.setSupportsAsync(getServiceBooleanProperty(serviceId, SUPPORTS_ASYNC));
145 		service.setMimeType(getServiceProperty(serviceId, MIME_TYPE));
146 
147 		return service;
148 	}
149 	
150 	/**
151 	 * Gets the service property.
152 	 *
153 	 * @param serviceId the service id
154 	 * @param propertyName the property name
155 	 * @return the service property
156 	 */
157 	private String getServiceProperty(String serviceId, String propertyName){
158 	
159 		String propertyValue = ITKServiceProperties.getProperty(serviceId+"."+propertyName);
160 		if (propertyValue==null){
161 			propertyValue = ITKServiceProperties.getProperty(DEFAULT+"."+propertyName);
162 		}
163 		if (propertyValue==null){
164 			propertyValue = "";
165 		}
166 		return propertyValue;
167 	}
168 	
169 	/**
170 	 * Gets the service boolean property.
171 	 *
172 	 * @param serviceId the service id
173 	 * @param propertyName the property name
174 	 * @return the service boolean property
175 	 */
176 	private boolean getServiceBooleanProperty(String serviceId, String propertyName){
177 		
178 		boolean propertyValue = false;
179 		if (!propertyName.equals("")){
180 
181 			String serviceProperty = ITKServiceProperties.getProperty(serviceId+"."+propertyName);
182 			
183 			if (null!=serviceProperty){
184 				if (serviceProperty.equals("Y")){
185 					propertyValue = true;
186 				}
187 			} else {
188 				serviceProperty = ITKServiceProperties.getProperty("DEFAULT."+propertyName);
189 				
190 			}
191 		}
192 		return propertyValue;
193 	}
194 }