List of usage examples for javax.xml.namespace QName getLocalPart
public String getLocalPart()
Get the local part of this QName
.
From source file:com.predic8.membrane.core.util.SOAPUtil.java
public static boolean isFault(XMLInputFactory xmlInputFactory, XOPReconstitutor xopr, Message msg) { int state = 0; /*/*from www . ja v a 2 s.co m*/ * 0: waiting for "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">" * 1: waiting for "<soapenv:Body>" (skipping any "<soapenv:Header>") * 2: waiting for "<soapenv:Fault>" */ try { XMLEventReader parser; synchronized (xmlInputFactory) { parser = xmlInputFactory.createXMLEventReader(xopr.reconstituteIfNecessary(msg)); } while (parser.hasNext()) { XMLEvent event = parser.nextEvent(); if (event.isStartElement()) { QName name = ((StartElement) event).getName(); if (!Constants.SOAP11_NS.equals(name.getNamespaceURI()) && !Constants.SOAP12_NS.equals(name.getNamespaceURI())) return false; if ("Header".equals(name.getLocalPart())) { // skip header int stack = 0; while (parser.hasNext()) { event = parser.nextEvent(); if (event.isStartElement()) stack++; if (event.isEndElement()) if (stack == 0) break; else stack--; } continue; } String expected; switch (state) { case 0: expected = "Envelope"; break; case 1: expected = "Body"; break; case 2: expected = "Fault"; break; default: return false; } if (expected.equals(name.getLocalPart())) { if (state == 2) return true; else state++; } else return false; } if (event.isEndElement()) return false; } } catch (Exception e) { log.warn("Ignoring exception: ", e); } return false; }
From source file:Main.java
/** * Return a string for a particular QName, mapping a new prefix * if necessary.//from w w w. j a va 2s . com */ public static String getStringForQName(QName qname, Element e) { String uri = qname.getNamespaceURI(); String prefix = getPrefix(uri, e); if (prefix == null) { int i = 1; prefix = "ns" + i; while (getNamespace(prefix, e) != null) { i++; prefix = "ns" + i; } e.setAttributeNS(NS_URI_XMLNS, "xmlns:" + prefix, uri); } return prefix + ":" + qname.getLocalPart(); }
From source file:ee.ria.xroad.common.message.SoapUtils.java
private static String getServiceCode(SOAPMessage soap, QName requestElementQName) throws SOAPException { for (SOAPElement eachHeaderElement : getChildElements(soap.getSOAPHeader())) { QName headerElementQName = eachHeaderElement.getElementQName(); if (!"service".equals(headerElementQName.getLocalPart())) { continue; }/*from w w w .j a v a 2 s . co m*/ for (SOAPElement eachServicePart : getChildElements(eachHeaderElement)) { QName headerPartQName = eachServicePart.getElementQName(); if (headerPartQName.getLocalPart().equals("serviceCode")) { return eachServicePart.getValue(); } } } return requestElementQName.getLocalPart(); }
From source file:com.palominolabs.crm.sf.soap.MetadataConnectionImplTest.java
License:asdf
private static void assertInvalidSession(ApiException e) { assertEquals("Call failed", e.getMessage()); Throwable cause = e.getCause(); assertTrue(cause instanceof SOAPFaultException); SOAPFaultException soapFaultException = (SOAPFaultException) cause; String expectedMsg = "INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session. Session not found, missing session key: "; String actualMsg = soapFaultException.getMessage(); assertEquals(expectedMsg, truncateSessionId(actualMsg)); SOAPFault fault = soapFaultException.getFault(); QName codeQname = fault.getFaultCodeAsQName(); assertEquals("INVALID_SESSION_ID", codeQname.getLocalPart()); String faultMsg = fault.getFaultString(); assertEquals(expectedMsg, truncateSessionId(faultMsg)); }
From source file:com.evolveum.midpoint.util.QNameUtil.java
public static String qNameToUri(QName qname) { String qUri = qname.getNamespaceURI(); StringBuilder sb = new StringBuilder(qUri); // TODO: Check if there's already a fragment // e.g. http://foo/bar#baz if (!qUri.endsWith("#") && !qUri.endsWith("/")) { sb.append("#"); }//w w w . j a v a 2s . c o m sb.append(qname.getLocalPart()); return sb.toString(); }
From source file:com.predic8.membrane.core.interceptor.rest.XML2HTTP.java
/** * Checks, if the response contains an XML doc with NS {@link Constants#HTTP_NS}. * If it does, the HTTP data (uri, method, status, headers, body) is extracted from the doc * and set as the response.//from ww w . j a v a 2s .c o m * * Reverse of {@link com.predic8.membrane.core.http.xml.Request#write(XMLStreamWriter)} and * {@link com.predic8.membrane.core.http.xml.Response#write(XMLStreamWriter)}. */ public static void unwrapMessageIfNecessary(Message message) { if (MimeType.TEXT_XML_UTF8.equals(message.getHeader().getContentType())) { try { if (message.getBody().getLength() == 0) return; XMLEventReader parser; synchronized (xmlInputFactory) { parser = xmlInputFactory.createXMLEventReader(message.getBodyAsStreamDecoded(), message.getCharset()); } /* States: * 0 = before root element, * 1 = root element has HTTP_NS namespace */ int state = 0; boolean keepSourceHeaders = false, foundHeaders = false, foundBody = false; while (parser.hasNext()) { XMLEvent event = parser.nextEvent(); switch (state) { case 0: if (event.isStartElement()) { QName name = event.asStartElement().getName(); if (Constants.HTTP_NS.equals(name.getNamespaceURI())) { state = 1; if ("request".equals(name.getLocalPart())) { Request req = (Request) message; req.setMethod(requireAttribute(event.asStartElement(), "method")); String httpVersion = getAttribute(event.asStartElement(), "http-version"); if (httpVersion == null) httpVersion = "1.1"; req.setVersion(httpVersion); } } else { return; } } break; case 1: if (event.isStartElement()) { String localName = event.asStartElement().getName().getLocalPart(); if ("status".equals(localName)) { Response res = (Response) message; res.setStatusCode( Integer.parseInt(requireAttribute(event.asStartElement(), "code"))); res.setStatusMessage(slurpCharacterData(parser, event.asStartElement())); } if ("uri".equals(localName)) { Request req = (Request) message; req.setUri(requireAttribute(event.asStartElement(), "value")); // uri/... (port,host,path,query) structure is ignored, as value already contains everything slurpXMLData(parser, event.asStartElement()); } if ("headers".equals(localName)) { foundHeaders = true; keepSourceHeaders = "true" .equals(getAttribute(event.asStartElement(), "keepSourceHeaders")); } if ("header".equals(localName)) { String key = requireAttribute(event.asStartElement(), "name"); boolean remove = getAttribute(event.asStartElement(), "remove") != null; if (remove && !keepSourceHeaders) throw new XML2HTTPException( "<headers keepSourceHeaders=\"false\"><header name=\"...\" remove=\"true\"> does not make sense."); message.getHeader().removeFields(key); if (!remove) message.getHeader().add(key, slurpCharacterData(parser, event.asStartElement())); } if ("body".equals(localName)) { foundBody = true; String type = requireAttribute(event.asStartElement(), "type"); if ("plain".equals(type)) { message.setBodyContent(slurpCharacterData(parser, event.asStartElement()) .getBytes(Constants.UTF_8_CHARSET)); } else if ("xml".equals(type)) { message.setBodyContent(slurpXMLData(parser, event.asStartElement()) .getBytes(Constants.UTF_8_CHARSET)); } else { throw new XML2HTTPException("XML-HTTP doc body type '" + type + "' is not supported (only 'plain' or 'xml')."); } } } break; } } if (!foundHeaders && !keepSourceHeaders) message.getHeader().clear(); if (!foundBody) message.setBodyContent(new byte[0]); } catch (XMLStreamException e) { log.error("", e); } catch (XML2HTTPException e) { log.error("", e); } catch (IOException e) { log.error("", e); } } }
From source file:edu.jhu.hlt.concrete.ingesters.bolt.BoltForumPostIngester.java
private static Section handleHeadline(final XMLEventReader rdr, final String content) throws XMLStreamException, ConcreteException { // The first type is always a document start event. Skip it. rdr.nextEvent();//from ww w .j ava 2s.com // The second type is a document ID block. Skip it. rdr.nextEvent(); // The third type is a whitespace block. Skip it. rdr.nextEvent(); // The next type is a headline start tag. XMLEvent hl = rdr.nextEvent(); StartElement hlse = hl.asStartElement(); QName hlqn = hlse.getName(); final String hlPart = hlqn.getLocalPart(); LOGGER.debug("QN: {}", hlPart); int hlPartOff = hlse.getLocation().getCharacterOffset(); LOGGER.debug("HL part offset: {}", hlPartOff); // Text of the headline. This would be useful for purely getting // the content, but for offsets, it's not that useful. Characters cc = rdr.nextEvent().asCharacters(); int charOff = cc.getLocation().getCharacterOffset(); int clen = cc.getData().length(); // The next part is the headline end element. Skip. rdr.nextEvent(); // Whitespace. Skip. rdr.nextEvent(); // Reader is now pointing at the first post. // Construct section, text span, etc. final int charOffPlusLen = charOff + clen; // Strip whitespace off TextSpan ts; if (STRIP_WHITESPACE_OFF_HEADLINE) { final String hlText = content.substring(charOff, charOffPlusLen); SimpleImmutableEntry<Integer, Integer> pads = trimSpacing(hlText); ts = new TextSpan(charOff + pads.getKey(), charOffPlusLen - pads.getValue()); } else { ts = new TextSpan(charOff, charOffPlusLen); } assert ts.getStart() <= ts.getEnding() : "ts=" + ts; Section s = new Section(); s.setKind("headline"); s.setTextSpan(ts); List<Integer> intList = new ArrayList<>(); intList.add(0); s.setNumberList(intList); return s; }
From source file:eu.eidas.auth.commons.attribute.AttributeSetPropertiesConverter.java
/** * Converts the given Iterable of <code>AttributeDefinition</code>s to an immutable sorted Map of Strings. * * @param properties the Iterable of <code>AttributeDefinition</code>s * @return an immutable sorted Map of Strings. *//*from ww w .ja va 2s . c o m*/ @Nonnull public static ImmutableSortedMap<String, String> toMap(@Nonnull Iterable<AttributeDefinition<?>> attributes) { ImmutableSortedMap.Builder<String, String> builder = new ImmutableSortedMap.Builder<String, String>( Ordering.natural()); int i = 0; for (AttributeDefinition<?> attributeDefinition : attributes) { String id = String.valueOf(++i); builder.put(Suffix.NAME_URI.computeEntry(id), attributeDefinition.getNameUri().toASCIIString()); builder.put(Suffix.FRIENDLY_NAME.computeEntry(id), attributeDefinition.getFriendlyName()); builder.put(Suffix.PERSON_TYPE.computeEntry(id), attributeDefinition.getPersonType().getValue()); builder.put(Suffix.REQUIRED.computeEntry(id), String.valueOf(attributeDefinition.isRequired())); if (attributeDefinition.isTransliterationMandatory()) { builder.put(Suffix.TRANSLITERATION_MANDATORY.computeEntry(id), Boolean.TRUE.toString()); } if (attributeDefinition.isUniqueIdentifier()) { builder.put(Suffix.UNIQUE_IDENTIFIER.computeEntry(id), Boolean.TRUE.toString()); } QName xmlType = attributeDefinition.getXmlType(); builder.put(Suffix.XML_TYPE_NAMESPACE_URI.computeEntry(id), String.valueOf(xmlType.getNamespaceURI())); builder.put(Suffix.XML_TYPE_LOCAL_PART.computeEntry(id), String.valueOf(xmlType.getLocalPart())); builder.put(Suffix.XML_TYPE_NAMESPACE_PREFIX.computeEntry(id), String.valueOf(xmlType.getPrefix())); builder.put(Suffix.ATTRIBUTE_VALUE_MARSHALLER.computeEntry(id), attributeDefinition.getAttributeValueMarshaller().getClass().getName()); } return builder.build(); }
From source file:com.evolveum.midpoint.report.impl.ReportUtils.java
public static String prettyPrintForReport(QName qname) { String ret = ""; if (qname.getLocalPart() != null) { ret = qname.getLocalPart();/*from w w w . j av a 2s . c o m*/ } return ret; }
From source file:fr.gouv.finances.dgfip.xemelios.utils.XmlUtils.java
public static String getPath(Stack<QName> stack, NamespaceContext ctx) { if (ctx == null) logger.fatal("NamespaceContext is null"); StringBuilder ret = new StringBuilder(); for (QName q : stack) { if (q == null) throw new IllegalStateException("q is null"); ret.append("/"); String prefix = ctx.getPrefix(q.getNamespaceURI()); if (prefix != null && prefix.length() > 0) ret.append(prefix).append(":"); ret.append(q.getLocalPart()); }/*from ww w .j a v a 2 s. c om*/ return ret.toString(); }