private String buildBatchId(ODataRequestContext requestContext) throws ODataBatchRendererException { StringBuilder sb = new StringBuilder(); String contentType = requestContext.getRequest().getHeaders().get(CONTENT_TYPE.toLowerCase()); if (contentType == null) { contentType = requestContext.getRequest().getHeader(CONTENT_TYPE); } if (isNullOrEmpty(contentType)) { throw new ODataBatchRendererException("Request Context Content-Type is missing"); } sb.append("--"); // get the batch UUID sb.append(contentType.substring(contentType.indexOf("=") + 1)); // substring existing batch id after "batch_" charset return sb.toString(); } }
private void doWireLogging(ODataRequest request) throws UnsupportedEncodingException { if (LOG.isTraceEnabled()) { LOG.trace("RAW REQUEST LOGGING"); LOG.trace("{} request for URL: {}", request.getMethod().name(), request.getUri()); for (Map.Entry<String, String> headerEntry : request.getHeaders().entrySet()) { LOG.trace("Header: {} value: {}", headerEntry.getKey(), headerEntry.getValue()); } LOG.trace("BODY: {}", request.getBodyText(UTF_8.name())); } } }
@Override public String toString() { return request.toString(); } }
protected boolean isGetRequest(ODataRequest request) { return request.getMethod() == Method.GET; }
public static TargetType getTargetType(ODataRequest request, EntityDataModel entityDataModel, ODataUri oDataUri) throws ODataTargetTypeException { Option<TargetType> targetTypeOption = ODataUriUtil.resolveTargetType(oDataUri, entityDataModel); if (targetTypeOption.isEmpty()) { throw new ODataTargetTypeException("The target type of this URI cannot be determined: " + request.getUri()); } return targetTypeOption.get(); }
private Map<String, String> buildDefaultEntityHeaders(ODataRequestContext odataRequestContext, ChangeSetEntity changeSetEntity) { Map<String, String> headers = new HashMap<>(); ODataRequest oDataRequest = odataRequestContext.getRequest(); headers.putAll(oDataRequest.getHeaders()); headers.put("changeSetId", changeSetEntity.getChangeSetId()); return headers; }
@Override protected String getToEntityId(ODataRequestContext requestContext) throws ODataUnmarshallingException { // The body is expected to contain a single entity reference // See OData Atom XML specification chapter 13 String bodyText; try { bodyText = requestContext.getRequest().getBodyText(StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { throw new ODataSystemException("UTF-8 is not supported", e); } Document document = parseXML(bodyText); Element rootElement = document.getDocumentElement(); if (!rootElement.getNodeName().equals(REF)) { throw new ODataUnmarshallingException("A " + requestContext.getRequest().getMethod() + " request to an entity reference URI must contain a single entity reference in the body," + " but something else was found instead: " + rootElement.getNodeName()); } String idAttr = rootElement.getAttribute(ID); if (isNullOrEmpty(idAttr)) { throw new ODataUnmarshallingException("The <metadata:ref> element in the body has no 'id' attribute," + " or the attribute is empty. The element must have an 'id' attribute that refers" + " to the entity to link to."); } return idAttr; }
public List<String> getPrefer() { String preferHeader = getHeader(HeaderNames.PREFER); if (isNullOrEmpty(preferHeader)) { return Collections.emptyList(); } List<String> preferList = Arrays.stream(preferHeader.split(",")).map(String::trim).collect(Collectors.toList()); return Collections.unmodifiableList(preferList); }
protected String getBodyText() { try { return request.getBodyText(UTF_8.name()); } catch (UnsupportedEncodingException e) { throw new ODataSystemException(e); } }
@Override public int score(ODataRequestContext requestContext) { // NOTE: Only POST and PUT are allowed for creating and updating links // See OData specification part 1, paragraph 11.4.6 ODataRequest.Method method = requestContext.getRequest().getMethod(); if ((isPostMethod(method) || isPutMethod(method)) && ODataUriUtil.isRefPathUri(requestContext.getUri())) { MediaType contentType = requestContext.getRequest().getContentType(); int score = super.score(contentType, supportedMediaTypes()); LOG.debug("Match for {}: {} with score: {}", this.getClass().getSimpleName(), requestContext.getRequest(), score); return score; } return DEFAULT_SCORE; }
private boolean checkForContentType(ODataRequestContext oDataRequestContext, MediaType expected) { MediaType requestContentType = oDataRequestContext.getRequest().getContentType(); return requestContentType != null && requestContentType.matches(expected); }
@Override public int score(ODataRequestContext requestContext, QueryResult data) { if (data == null || data.getType() == NOTHING || data.getType() != EXCEPTION) { return DEFAULT_SCORE; } // Try the types that should be allowed according to the OData specification // See: OData JSON Format Version 4.0, chapter 19: Error Response List<MediaType> accept = requestContext.getRequest().getAccept(); int jsonAcceptScore = scoreByMediaType(accept, JSON); int contentTypeScore = scoreByContentType(requestContext, JSON); int resultScore = max(jsonAcceptScore, contentTypeScore); return resultScore > 0 ? (resultScore + ERROR_EXTRA_SCORE) : DEFAULT_SCORE; }
public ODataRequest build() { return new ODataRequest(this); } }
protected boolean isGetRequest(ODataRequest request) { return request.getMethod() == Method.GET; }
public static TargetType getTargetType(ODataRequest request, EntityDataModel entityDataModel, ODataUri oDataUri) throws ODataTargetTypeException { Option<TargetType> targetTypeOption = ODataUriUtil.resolveTargetType(oDataUri, entityDataModel); if (targetTypeOption.isEmpty()) { throw new ODataTargetTypeException("The target type of this URI cannot be determined: " + request.getUri()); } return targetTypeOption.get(); }
private Map<String, String> buildDefaultEntityHeaders(ODataRequestContext odataRequestContext, ChangeSetEntity changeSetEntity) { Map<String, String> headers = new HashMap<>(); ODataRequest oDataRequest = odataRequestContext.getRequest(); headers.putAll(oDataRequest.getHeaders()); headers.put("changeSetId", changeSetEntity.getChangeSetId()); return headers; }