List of usage examples for org.apache.commons.lang3 StringUtils substringAfterLast
public static String substringAfterLast(final String str, final String separator)
Gets the substring after the last occurrence of a separator.
From source file:org.apache.olingo.client.core.serialization.AtomDeserializer.java
private Property property(final XMLEventReader reader, final StartElement start) throws XMLStreamException, EdmPrimitiveTypeException { final Property property = new Property(); if (propertyValueQName.equals(start.getName())) { // retrieve name from context final Attribute context = start.getAttributeByName(contextQName); if (context != null) { property.setName(StringUtils.substringAfterLast(context.getValue(), "/")); }//from w ww . j av a 2s .c o m } else { property.setName(start.getName().getLocalPart()); } valuable(property, reader, start); return property; }
From source file:org.apache.olingo.client.core.serialization.AtomGeoValueDeserializer.java
public Geospatial deserialize(final XMLEventReader reader, final StartElement start, final EdmPrimitiveTypeKind type) throws XMLStreamException { SRID srid = null;/*from ww w . ja va 2 s. c o m*/ final Attribute srsName = start.getAttributeByName(Constants.QNAME_ATTR_SRSNAME); if (srsName != null) { srid = SRID.valueOf(StringUtils.substringAfterLast(srsName.getValue(), "/")); } Geospatial value; switch (type) { case GeographyPoint: case GeometryPoint: value = points(reader, start, type, srid).get(0); break; case GeographyMultiPoint: case GeometryMultiPoint: value = multipoint(reader, start, type, srid); break; case GeographyLineString: case GeometryLineString: value = lineString(reader, start, type, srid); break; case GeographyMultiLineString: case GeometryMultiLineString: value = multiLineString(reader, start, type, srid); break; case GeographyPolygon: case GeometryPolygon: value = polygon(reader, start, type, srid); break; case GeographyMultiPolygon: case GeometryMultiPolygon: value = multiPolygon(reader, start, type, srid); break; case GeographyCollection: case GeometryCollection: value = collection(reader, start, type, srid); break; default: value = null; } return value; }
From source file:org.apache.olingo.client.core.serialization.JsonEntitySerializer.java
protected void doContainerSerialize(final ResWrap<Entity> container, final JsonGenerator jgen) throws IOException, EdmPrimitiveTypeException { final Entity entity = container.getPayload(); jgen.writeStartObject();/* w ww. j av a 2 s . com*/ if (serverMode) { if (container.getContextURL() != null) { jgen.writeStringField(Constants.JSON_CONTEXT, container.getContextURL().toASCIIString()); } if (StringUtils.isNotBlank(container.getMetadataETag())) { jgen.writeStringField(Constants.JSON_METADATA_ETAG, container.getMetadataETag()); } if (StringUtils.isNotBlank(entity.getETag())) { jgen.writeStringField(Constants.JSON_ETAG, entity.getETag()); } } if (StringUtils.isNotBlank(entity.getType()) && !isODataMetadataNone(contentType)) { jgen.writeStringField(Constants.JSON_TYPE, new EdmTypeInfo.Builder().setTypeExpression(entity.getType()).build().external()); } if (entity.getId() != null && !isODataMetadataNone(contentType)) { jgen.writeStringField(Constants.JSON_ID, entity.getId().toASCIIString()); } for (Annotation annotation : entity.getAnnotations()) { valuable(jgen, annotation, "@" + annotation.getTerm()); } for (Property property : entity.getProperties()) { valuable(jgen, property, property.getName()); } if (serverMode && entity.getEditLink() != null && StringUtils.isNotBlank(entity.getEditLink().getHref())) { jgen.writeStringField(Constants.JSON_EDIT_LINK, entity.getEditLink().getHref()); if (entity.isMediaEntity()) { jgen.writeStringField(Constants.JSON_MEDIA_READ_LINK, entity.getEditLink().getHref() + "/$value"); } } if (!isODataMetadataNone(contentType)) { links(entity, jgen); } for (Link link : entity.getMediaEditLinks()) { if (link.getTitle() == null) { jgen.writeStringField(Constants.JSON_MEDIA_EDIT_LINK, link.getHref()); } if (link.getInlineEntity() != null) { jgen.writeObjectField(link.getTitle(), link.getInlineEntity()); } if (link.getInlineEntitySet() != null) { jgen.writeArrayFieldStart(link.getTitle()); for (Entity subEntry : link.getInlineEntitySet().getEntities()) { jgen.writeObject(subEntry); } jgen.writeEndArray(); } } if (serverMode) { for (Operation operation : entity.getOperations()) { jgen.writeObjectFieldStart( "#" + StringUtils.substringAfterLast(operation.getMetadataAnchor(), "#")); jgen.writeStringField(Constants.ATTR_TITLE, operation.getTitle()); jgen.writeStringField(Constants.ATTR_TARGET, operation.getTarget().toASCIIString()); jgen.writeEndObject(); } } jgen.writeEndObject(); }
From source file:org.apache.olingo.client.core.serialization.JsonPropertyDeserializer.java
protected ResWrap<Property> doDeserialize(final JsonParser parser) throws IOException { final ObjectNode tree = parser.getCodec().readTree(parser); final String metadataETag; final URI contextURL; final Property property = new Property(); if (tree.hasNonNull(Constants.JSON_METADATA_ETAG)) { metadataETag = tree.get(Constants.JSON_METADATA_ETAG).textValue(); tree.remove(Constants.JSON_METADATA_ETAG); } else {//from w w w .j a va2 s . c o m metadataETag = null; } if (tree.hasNonNull(Constants.JSON_CONTEXT)) { contextURL = URI.create(tree.get(Constants.JSON_CONTEXT).textValue()); property.setName(StringUtils.substringAfterLast(contextURL.toASCIIString(), "/")); tree.remove(Constants.JSON_CONTEXT); } else if (tree.hasNonNull(Constants.JSON_METADATA)) { contextURL = URI.create(tree.get(Constants.JSON_METADATA).textValue()); property.setType(new EdmTypeInfo.Builder() .setTypeExpression(StringUtils.substringAfterLast(contextURL.toASCIIString(), "#")).build() .internal()); tree.remove(Constants.JSON_METADATA); } else { contextURL = null; } if (tree.has(Constants.JSON_TYPE)) { property.setType(new EdmTypeInfo.Builder().setTypeExpression(tree.get(Constants.JSON_TYPE).textValue()) .build().internal()); tree.remove(Constants.JSON_TYPE); } if (tree.has(Constants.JSON_NULL) && tree.get(Constants.JSON_NULL).asBoolean()) { property.setValue(ValueType.PRIMITIVE, null); tree.remove(Constants.JSON_NULL); } if (property.getValue() == null) { try { value(property, tree.has(Constants.VALUE) ? tree.get(Constants.VALUE) : tree, parser.getCodec()); } catch (final EdmPrimitiveTypeException e) { throw new IOException(e); } tree.remove(Constants.VALUE); } Set<String> toRemove = new HashSet<String>(); // any remaining entry is supposed to be an annotation or is ignored for (final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields(); itor.hasNext();) { final Map.Entry<String, JsonNode> field = itor.next(); if (field.getKey().charAt(0) == '@') { final Annotation annotation = new Annotation(); annotation.setTerm(field.getKey().substring(1)); try { value(annotation, field.getValue(), parser.getCodec()); } catch (final EdmPrimitiveTypeException e) { throw new IOException(e); } property.getAnnotations().add(annotation); } else if (field.getKey().charAt(0) == '#') { final Operation operation = new Operation(); operation.setMetadataAnchor(field.getKey()); final ObjectNode opNode = (ObjectNode) tree.get(field.getKey()); operation.setTitle(opNode.get(Constants.ATTR_TITLE).asText()); operation.setTarget(URI.create(opNode.get(Constants.ATTR_TARGET).asText())); property.getOperations().add(operation); toRemove.add(field.getKey()); } } tree.remove(toRemove); return new ResWrap<Property>(contextURL, metadataETag, property); }
From source file:org.apache.olingo.commons.core.data.AtomGeoValueDeserializer.java
public Geospatial deserialize(final XMLEventReader reader, final StartElement start, final EdmPrimitiveTypeKind type) throws XMLStreamException { String crs = null;// www . jav a2 s.com final Attribute srsName = start.getAttributeByName(Constants.QNAME_ATTR_SRSNAME); if (srsName != null) { crs = StringUtils.substringAfterLast(srsName.getValue(), "/"); } Geospatial value; switch (type) { case GeographyPoint: case GeometryPoint: value = points(reader, start, type, crs).get(0); break; case GeographyMultiPoint: case GeometryMultiPoint: value = multipoint(reader, start, type, crs); break; case GeographyLineString: case GeometryLineString: value = lineString(reader, start, type, crs); break; case GeographyMultiLineString: case GeometryMultiLineString: value = multiLineString(reader, start, type, crs); break; case GeographyPolygon: case GeometryPolygon: value = polygon(reader, start, type, crs); break; case GeographyMultiPolygon: case GeometryMultiPolygon: value = multiPolygon(reader, start, type, crs); break; case GeographyCollection: case GeometryCollection: value = collection(reader, start, type, crs); break; default: value = null; } return value; }
From source file:org.apache.olingo.commons.core.serialization.AtomDeserializer.java
private Property property(final XMLEventReader reader, final StartElement start) throws XMLStreamException, EdmPrimitiveTypeException { final PropertyImpl property = new PropertyImpl(); if (ODataServiceVersion.V40 == version && propertyValueQName.equals(start.getName())) { // retrieve name from context final Attribute context = start.getAttributeByName(contextQName); if (context != null) { property.setName(StringUtils.substringAfterLast(context.getValue(), "/")); }//from w w w . jav a 2 s . c om } else { property.setName(start.getName().getLocalPart()); } valuable(property, reader, start); return property; }
From source file:org.apache.olingo.commons.core.serialization.JsonEntitySerializer.java
protected void doContainerSerialize(final ResWrap<Entity> container, final JsonGenerator jgen) throws IOException, EdmPrimitiveTypeException { final Entity entity = container.getPayload(); jgen.writeStartObject();/*from w w w. j a v a 2 s.c o m*/ if (serverMode) { if (container.getContextURL() != null) { jgen.writeStringField(version.compareTo(ODataServiceVersion.V40) >= 0 ? Constants.JSON_CONTEXT : Constants.JSON_METADATA, container.getContextURL().toASCIIString()); } if (version.compareTo(ODataServiceVersion.V40) >= 0 && StringUtils.isNotBlank(container.getMetadataETag())) { jgen.writeStringField(Constants.JSON_METADATA_ETAG, container.getMetadataETag()); } if (StringUtils.isNotBlank(entity.getETag())) { jgen.writeStringField(version.getJsonName(ODataServiceVersion.JsonKey.ETAG), entity.getETag()); } } if (StringUtils.isNotBlank(entity.getType())) { jgen.writeStringField(version.getJsonName(ODataServiceVersion.JsonKey.TYPE), new EdmTypeInfo.Builder().setTypeExpression(entity.getType()).build().external(version)); } if (entity.getId() != null) { jgen.writeStringField(version.getJsonName(ODataServiceVersion.JsonKey.ID), entity.getId().toASCIIString()); } for (Annotation annotation : entity.getAnnotations()) { valuable(jgen, annotation, "@" + annotation.getTerm()); } for (Property property : entity.getProperties()) { valuable(jgen, property, property.getName()); } if (serverMode && entity.getEditLink() != null && StringUtils.isNotBlank(entity.getEditLink().getHref())) { jgen.writeStringField(version.getJsonName(ODataServiceVersion.JsonKey.EDIT_LINK), entity.getEditLink().getHref()); if (entity.isMediaEntity()) { jgen.writeStringField(version.getJsonName(ODataServiceVersion.JsonKey.MEDIA_READ_LINK), entity.getEditLink().getHref() + "/$value"); } } links(entity, jgen); for (Link link : entity.getMediaEditLinks()) { if (link.getTitle() == null) { jgen.writeStringField(version.getJsonName(ODataServiceVersion.JsonKey.MEDIA_EDIT_LINK), link.getHref()); } if (link.getInlineEntity() != null) { jgen.writeObjectField(link.getTitle(), link.getInlineEntity()); } if (link.getInlineEntitySet() != null) { jgen.writeArrayFieldStart(link.getTitle()); for (Entity subEntry : link.getInlineEntitySet().getEntities()) { jgen.writeObject(subEntry); } jgen.writeEndArray(); } } if (serverMode) { for (ODataOperation operation : entity.getOperations()) { jgen.writeObjectFieldStart( "#" + StringUtils.substringAfterLast(operation.getMetadataAnchor(), "#")); jgen.writeStringField(Constants.ATTR_TITLE, operation.getTitle()); jgen.writeStringField(Constants.ATTR_TARGET, operation.getTarget().toASCIIString()); jgen.writeEndObject(); } } jgen.writeEndObject(); }
From source file:org.apache.olingo.commons.core.serialization.JsonPropertyDeserializer.java
protected ResWrap<Property> doDeserialize(final JsonParser parser) throws IOException { final ObjectNode tree = (ObjectNode) parser.getCodec().readTree(parser); final String metadataETag; final URI contextURL; final PropertyImpl property = new PropertyImpl(); if (tree.hasNonNull(Constants.JSON_METADATA_ETAG)) { metadataETag = tree.get(Constants.JSON_METADATA_ETAG).textValue(); tree.remove(Constants.JSON_METADATA_ETAG); } else {/* w w w .ja v a 2 s. c om*/ metadataETag = null; } if (tree.hasNonNull(Constants.JSON_CONTEXT)) { contextURL = URI.create(tree.get(Constants.JSON_CONTEXT).textValue()); property.setName(StringUtils.substringAfterLast(contextURL.toASCIIString(), "/")); tree.remove(Constants.JSON_CONTEXT); } else if (tree.hasNonNull(Constants.JSON_METADATA)) { contextURL = URI.create(tree.get(Constants.JSON_METADATA).textValue()); property.setType(new EdmTypeInfo.Builder() .setTypeExpression(StringUtils.substringAfterLast(contextURL.toASCIIString(), "#")).build() .internal()); tree.remove(Constants.JSON_METADATA); } else { contextURL = null; } if (tree.has(jsonType)) { property.setType( new EdmTypeInfo.Builder().setTypeExpression(tree.get(jsonType).textValue()).build().internal()); tree.remove(jsonType); } if (tree.has(Constants.JSON_NULL) && tree.get(Constants.JSON_NULL).asBoolean()) { property.setValue(ValueType.PRIMITIVE, null); tree.remove(Constants.JSON_NULL); } if (property.getValue() == null) { try { value(property, tree.has(Constants.VALUE) ? tree.get(Constants.VALUE) : tree, parser.getCodec()); } catch (final EdmPrimitiveTypeException e) { throw new IOException(e); } tree.remove(Constants.VALUE); } // any remaining entry is supposed to be an annotation or is ignored for (final Iterator<Map.Entry<String, JsonNode>> itor = tree.fields(); itor.hasNext();) { final Map.Entry<String, JsonNode> field = itor.next(); if (field.getKey().charAt(0) == '@') { final Annotation annotation = new AnnotationImpl(); annotation.setTerm(field.getKey().substring(1)); try { value(annotation, field.getValue(), parser.getCodec()); } catch (final EdmPrimitiveTypeException e) { throw new IOException(e); } property.getAnnotations().add(annotation); } } return new ResWrap<Property>(contextURL, metadataETag, property); }
From source file:org.apache.olingo.fit.CXFOAuth2HttpClientFactory.java
@Override protected void init() throws OAuth2Exception { final URI authURI = OAuthClientUtils.getAuthorizationURI(oauth2GrantServiceURI.toASCIIString(), OAuth2Provider.CLIENT_ID, OAuth2Provider.REDIRECT_URI, null, null); // Disable automatic redirects handling final HttpParams params = new BasicHttpParams(); params.setParameter(ClientPNames.HANDLE_REDIRECTS, false); final DefaultHttpClient httpClient = new DefaultHttpClient(params); JsonNode oAuthAuthorizationData = null; String authenticityCookie = null; try {//from w w w. j a va 2 s .c o m // 1. Need to (basic) authenticate against the OAuth2 service final HttpGet method = new HttpGet(authURI); method.addHeader("Authorization", "Basic " + Base64.encodeBase64String("odatajclient:odatajclient".getBytes())); final HttpResponse response = httpClient.execute(method); // 2. Pull out OAuth2 authorization data and "authenticity" cookie (CXF specific) oAuthAuthorizationData = new XmlMapper().readTree(EntityUtils.toString(response.getEntity())); final Header setCookieHeader = response.getFirstHeader("Set-Cookie"); if (setCookieHeader == null) { throw new IllegalStateException("OAuth flow is broken"); } authenticityCookie = setCookieHeader.getValue(); } catch (Exception e) { throw new OAuth2Exception(e); } String code = null; try { // 3. Submit the HTTP form for allowing access to the application final URI location = new URIBuilder(oAuthAuthorizationData.get("replyTo").asText()) .addParameter("session_authenticity_token", oAuthAuthorizationData.get("authenticityToken").asText()) .addParameter("client_id", oAuthAuthorizationData.get("clientId").asText()) .addParameter("redirect_uri", oAuthAuthorizationData.get("redirectUri").asText()) .addParameter("oauthDecision", "allow").build(); final HttpGet method = new HttpGet(location); method.addHeader("Authorization", "Basic " + Base64.encodeBase64String("odatajclient:odatajclient".getBytes())); method.addHeader("Cookie", authenticityCookie); final HttpResponse response = httpClient.execute(method); final Header locationHeader = response.getFirstHeader("Location"); if (response.getStatusLine().getStatusCode() != 303 || locationHeader == null) { throw new IllegalStateException("OAuth flow is broken"); } // 4. Get the authorization code value out of this last redirect code = StringUtils.substringAfterLast(locationHeader.getValue(), "="); EntityUtils.consumeQuietly(response.getEntity()); } catch (Exception e) { throw new OAuth2Exception(e); } // 5. Obtain the access token try { accessToken = OAuthClientUtils.getAccessToken(getAccessTokenService(), OAUTH2_CONSUMER, new AuthorizationCodeGrant(code)); } catch (OAuthServiceException e) { throw new OAuth2Exception(e); } if (accessToken == null) { throw new OAuth2Exception("No OAuth2 access token"); } }
From source file:org.apache.olingo.fit.metadata.Metadata.java
public EntityType getEntityOrComplexType(final String fqn) { EntityType result = null;/*from ww w. j av a2 s. co m*/ final String ns = StringUtils.substringBeforeLast(fqn, "."); if (getSchema(ns) != null) { final String name = StringUtils.substringAfterLast(fqn, "."); result = getSchema(ns).getEntityType(name); if (result != null && result.getBaseType() != null) { final String baseNS = StringUtils.substringBeforeLast(result.getBaseType(), "."); if (getSchema(baseNS) != null) { final String baseName = StringUtils.substringAfterLast(result.getBaseType(), "."); final EntityType baseType = getSchema(baseNS).getEntityType(baseName); if (baseType != null) { for (Map.Entry<String, Property> entry : baseType.getPropertyMap().entrySet()) { result.addProperty(entry.getKey(), entry.getValue()); } for (Map.Entry<String, NavigationProperty> entry : baseType.getNavigationPropertyMap() .entrySet()) { result.addNavigationProperty(entry.getKey(), entry.getValue()); } } } } } return result; }