|
| 1 | +/* |
| 2 | + * NameQueryClient.java |
| 3 | + * Created: 22 Nov 2018 |
| 4 | + * |
| 5 | + * Copyright 2018 Systemic Pty Ltd |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 8 | + * in compliance with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 13 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 14 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 15 | + * the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package sif3.infra.rest.client; |
| 19 | + |
| 20 | +import java.util.HashMap; |
| 21 | + |
| 22 | +import javax.ws.rs.core.MediaType; |
| 23 | +import javax.ws.rs.core.Response.Status; |
| 24 | + |
| 25 | +import com.sun.jersey.api.client.ClientResponse; |
| 26 | +import com.sun.jersey.api.client.WebResource; |
| 27 | + |
| 28 | +import au.com.systemic.framework.utils.StringUtils; |
| 29 | +import sif3.common.exception.ServiceInvokationException; |
| 30 | +import sif3.common.header.HeaderProperties; |
| 31 | +import sif3.common.header.HeaderValues.RequestType; |
| 32 | +import sif3.common.header.HeaderValues.ServiceType; |
| 33 | +import sif3.common.header.RequestHeaderConstants; |
| 34 | +import sif3.common.model.PagingInfo; |
| 35 | +import sif3.common.model.QueryTemplateInfo; |
| 36 | +import sif3.common.model.SIFContext; |
| 37 | +import sif3.common.model.SIFZone; |
| 38 | +import sif3.common.model.URLQueryParameter; |
| 39 | +import sif3.common.ws.Response; |
| 40 | +import sif3.infra.common.env.types.ConsumerEnvironment.ConnectorName; |
| 41 | +import sif3.infra.common.interfaces.ClientEnvironmentManager; |
| 42 | + |
| 43 | +/** |
| 44 | + * @author Joerg Huber |
| 45 | + * |
| 46 | + */ |
| 47 | +public class NameQueryClient extends BaseClient |
| 48 | +{ |
| 49 | + |
| 50 | +// private String queryName = null; |
| 51 | + |
| 52 | + /** |
| 53 | + * Constructor |
| 54 | + * |
| 55 | + * @param clientEnvMgr Session manager to access the clients session information. |
| 56 | + */ |
| 57 | + public NameQueryClient(ClientEnvironmentManager clientEnvMgr) |
| 58 | + { |
| 59 | + super(clientEnvMgr, clientEnvMgr.getEnvironmentInfo().getConnectorBaseURI(ConnectorName.requestsConnector), clientEnvMgr.getEnvironmentInfo().getMediaType(), clientEnvMgr.getEnvironmentInfo().getMediaType(), null, null, clientEnvMgr.getEnvironmentInfo().getSecureConnection(), clientEnvMgr.getEnvironmentInfo().getCompressionEnabled()); |
| 60 | + } |
| 61 | + |
| 62 | +// public String getQueryName() |
| 63 | +// { |
| 64 | +// return queryName; |
| 65 | +// } |
| 66 | +// |
| 67 | +// public void setQueryName(String queryName) |
| 68 | +// { |
| 69 | +// this.queryName = queryName; |
| 70 | +// } |
| 71 | + |
| 72 | + /** |
| 73 | + * This method is used to retrieve data from a named query service. This can be any data. It is up to the implementation of the |
| 74 | + * named query service to know what that data is for a given query. This framework is agnostic to that data. The returned |
| 75 | + * value is a String that must represent the "marshalled" version of the data in the format indicated by the "returnMimeType". |
| 76 | + * Because the data that can be returned as part of a service might be a collection, the paging parameter can be provided. If the |
| 77 | + * data to be returned is considered too large by the provider (implementation dependent) then an appropriate error is |
| 78 | + * returned (HTTP Status 413 - Response too large). |
| 79 | + * |
| 80 | + * @param queryTemplateInfo Hold the query name and query parameters of the named query service from where the data shall be |
| 81 | + * retrieved. The parameter and the queryTemplateInfo.queryName property must not be null/empty. |
| 82 | + * @param returnMimeType The mime type the response data is in. It is expected that the consumer provides that and the provider |
| 83 | + * should attempt to marshal the data to the given mime type and return the resulting string as |
| 84 | + * part of this call. If the provider cannot marshal the data to the requested mime type then an |
| 85 | + * appropriate error is returned to this consumer (HTTP Status 400 - Bad Request). |
| 86 | + * @param pagingInfo Page information to determine which results to return. Null = Return all (NOT RECOMMENDED! Might be rejected |
| 87 | + * by provider.). |
| 88 | + * @param hdrProperties Header Properties to be added to the header of the request. Can be null. |
| 89 | + * @param urlQueryParams Additional URL query parameters to be added to the request. It is assumed that these are custom |
| 90 | + * URL query parameters that may not form part of the actual named query parameters. They are conveyed |
| 91 | + * to the provider unchanged. URL query parameter names are case sensitive. This parameter can be null. |
| 92 | + * @param zone The Zone from which the request is being issued. Can be Null (default Zone) |
| 93 | + * @param context The Context for which the the request is being issued. Can be Null (default Zone) |
| 94 | + * @param requestType Indicating if IMMEDIATE or DELAYED request is desired. |
| 95 | + * |
| 96 | + * @return Response Object holding appropriate values and results of the call. Because the framework is agnostic to the |
| 97 | + * data that is returned for a phase the Response.dataObjectType will be set to "String" and the Response.dataObject |
| 98 | + * will hold the string representation of the returned payload. It is up to the caller of this method to potentially |
| 99 | + * marshal that payload into an appropriate object. |
| 100 | + * |
| 101 | + * @throws ServiceInvokationException Any underlying errors occurred such as failure to invoke actual web-service etc. |
| 102 | + */ |
| 103 | + public Response retrieveDataFromNamedQuery(QueryTemplateInfo queryTemplateInfo, MediaType returnMimeType, PagingInfo pagingInfo, HeaderProperties hdrProperties, URLQueryParameter urlQueryParams, SIFZone zone, SIFContext context, RequestType requestType) |
| 104 | + throws ServiceInvokationException |
| 105 | + { |
| 106 | + if ((queryTemplateInfo == null) || StringUtils.isEmpty(queryTemplateInfo.getQueryName())) |
| 107 | + { |
| 108 | + String errorMsg = "queryTemplateInfo parameter or queryTemplateInfo.queryName is null or empty. These values must be provided."; |
| 109 | + logger.error(errorMsg); |
| 110 | + throw new ServiceInvokationException(errorMsg); |
| 111 | + } |
| 112 | + WebResource service = getService(); |
| 113 | + try |
| 114 | + { |
| 115 | + //Add Named Query Parameters to URL Query Parameters |
| 116 | + if ((queryTemplateInfo.getQueryParameters() != null) && !queryTemplateInfo.getQueryParameters().isEmpty()) |
| 117 | + { |
| 118 | + if (urlQueryParams == null) |
| 119 | + { |
| 120 | + urlQueryParams = new URLQueryParameter(); |
| 121 | + urlQueryParams.setQueryParams(new HashMap<String, String>()); |
| 122 | + } |
| 123 | + |
| 124 | + urlQueryParams.getQueryParams().putAll(queryTemplateInfo.getQueryParameters()); |
| 125 | + } |
| 126 | + |
| 127 | + service = buildURI(service, zone, context, urlQueryParams, queryTemplateInfo.getQueryName()); |
| 128 | + hdrProperties = addAuthenticationHdrProps(hdrProperties); |
| 129 | + hdrProperties.setHeaderProperty(RequestHeaderConstants.HDR_SERVICE_TYPE, ServiceType.XQUERYTEMPLATE.name()); |
| 130 | + addPagingInfoToHeaders(pagingInfo, hdrProperties); |
| 131 | + addDelayedInfo(hdrProperties, zone, context, queryTemplateInfo.getQueryName(), ServiceType.XQUERYTEMPLATE, requestType); |
| 132 | + |
| 133 | + ClientResponse response = setRequestHeaderAndMediaTypes(service, returnMimeType, returnMimeType, hdrProperties, requestType, true, true, false).get(ClientResponse.class); |
| 134 | + |
| 135 | + return setResponse(service, response, String.class, hdrProperties, zone, context, requestType, true, Status.OK, Status.NOT_MODIFIED, Status.NO_CONTENT, Status.ACCEPTED); |
| 136 | + } |
| 137 | + catch (Exception ex) |
| 138 | + { |
| 139 | + String errorMsg = "Failed to invoke 'retrieveDataFromNamedQuery' service (REST GET) on URI " + service.getURI() + ": " + ex.getMessage(); |
| 140 | + logger.error(errorMsg); |
| 141 | + throw new ServiceInvokationException(errorMsg, ex); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments