List of usage examples for java.rmi AccessException AccessException
public AccessException(String s)
AccessException
with the specified detail message. From source file:de.mpg.escidoc.services.dataacquisition.DataHandlerBean.java
/** * {@inheritDoc}/*from www . j a v a 2 s. co m*/ */ public byte[] doFetch(String sourceName, String identifier, String trgFormatName, String trgFormatType, String trgFormatEncoding) throws SourceNotAvailableException, AccessException, IdentifierNotRecognisedException, FormatNotRecognisedException, RuntimeException, FormatNotAvailableException { byte[] fetchedData = null; this.setFileEnding(this.util.retrieveFileEndingFromCone(trgFormatType)); try { if (sourceName.equalsIgnoreCase("escidoc")) { // necessary for escidoc sources sourceName = this.util.trimSourceName(sourceName, identifier); identifier = this.util.setEsciDocIdentifier(identifier); this.currentSource = this.sourceHandler.getSourceByName(sourceName); } else { this.currentSource = this.sourceHandler.getSourceByName(sourceName); identifier = this.util.trimIdentifier(this.currentSource, identifier); } String fetchType = this.getFetchingType(trgFormatName, trgFormatType, trgFormatEncoding); if (fetchType.equals(this.fetchTypeTEXTUALDATA)) { String textualData = this.fetchTextualData(identifier, trgFormatName, trgFormatType, trgFormatEncoding); if (textualData == null) { return null; } fetchedData = textualData.getBytes(this.enc); } if (fetchType.equals(this.fetchTypeFILEDATA)) { Format format = new Format(trgFormatName, trgFormatType, trgFormatEncoding); fetchedData = this.fetchData(identifier, new Format[] { format }); } if (fetchType.equals(this.fetchTypeESCIDOCTRANS)) { fetchedData = this .fetchTextualData(identifier, "eSciDoc-publication-item", "application/xml", this.enc) .getBytes(this.enc); TransformationBean transformer = new TransformationBean(); fetchedData = transformer.transform(fetchedData, "eSciDoc-publication-item", "application/xml", this.enc, trgFormatName, trgFormatType, trgFormatEncoding, "escidoc"); this.setContentType(trgFormatType); } if (fetchType.equals(this.fetchTypeUNKNOWN)) { throw new FormatNotRecognisedException(); } } catch (AccessException e) { throw new AccessException(sourceName); } catch (IdentifierNotRecognisedException e) { throw new IdentifierNotRecognisedException(e); } catch (SourceNotAvailableException e) { throw new SourceNotAvailableException(e); } catch (FormatNotRecognisedException e) { throw new FormatNotRecognisedException(e); } catch (FormatNotAvailableException e) { throw new FormatNotAvailableException(e); } catch (Exception e) { throw new RuntimeException(e); } return fetchedData; }
From source file:de.mpg.mpdl.inge.dataacquisition.DataHandlerBean.java
/** * Operation for fetching data of type TEXTUALDATA. * //from w w w . java2 s . c om * @param identifier * @param format * @return itemXML * @throws IdentifierNotRecognisedException * @throws SourceNotAvailableException * @throws AccessException * @throws FormatNotSupportedException */ private String fetchTextualData(String identifier, String trgFormatName, String trgFormatType, String trgFormatEncoding) throws IdentifierNotRecognisedException, AccessException, SourceNotAvailableException, FormatNotAvailableException, FormatNotRecognisedException { String fetchedItem = null; String item = null; boolean supportedProtocol = false; ProtocolHandler protocolHandler = new ProtocolHandler(); try { MetadataVO md = this.util.getMdObjectToFetch(this.currentSource, trgFormatName, trgFormatType, trgFormatEncoding); if (md == null) { return null; } String decoded = java.net.URLDecoder.decode(md.getMdUrl().toString(), this.currentSource.getEncoding()); md.setMdUrl(new URL(decoded)); md.setMdUrl(new URL(md.getMdUrl().toString().replaceAll(this.regex, identifier.trim()))); this.currentSource = this.sourceHandler.updateMdEntry(this.currentSource, md); // Select harvesting method if (this.currentSource.getHarvestProtocol().equalsIgnoreCase("oai-pmh")) { this.logger.debug("Fetch OAI record from URL: " + md.getMdUrl()); item = fetchOAIRecord(md); // Check the record for error codes protocolHandler.checkOAIRecord(item); supportedProtocol = true; } if (this.currentSource.getHarvestProtocol().equalsIgnoreCase("ejb")) { this.logger.debug("Fetch record via EJB."); item = this.fetchEjbRecord(md, identifier); supportedProtocol = true; } if (this.currentSource.getHarvestProtocol().equalsIgnoreCase("http")) { this.logger.debug("Fetch record via http."); item = this.fetchHttpRecord(md); supportedProtocol = true; } if (!supportedProtocol) { this.logger .warn("Harvesting protocol " + this.currentSource.getHarvestProtocol() + " not supported."); throw new RuntimeException(); } fetchedItem = item; // Transform the itemXML if necessary if (item != null && !trgFormatName.trim().equalsIgnoreCase(md.getName().toLowerCase())) { TransformationBean transformer = new TransformationBean(); // Transform item metadata Format srcFormat = new Format(md.getName(), md.getMdFormat(), "*"); Format trgFormat = new Format(trgFormatName, trgFormatType, trgFormatEncoding); item = new String(transformer.transform(item.getBytes(this.enc), srcFormat, trgFormat, "escidoc"), this.enc); if (this.currentSource.getItemUrl() != null) { this.setItemUrl( new URL(this.currentSource.getItemUrl().toString().replace("GETID", identifier))); } try { // Create component if supported String name = trgFormatName.replace("item", "component"); Format trgFormatComponent = new Format(name, trgFormatType, trgFormatEncoding); if (transformer.checkTransformation(srcFormat, trgFormatComponent)) { byte[] componentBytes = transformer.transform(fetchedItem.getBytes(this.enc), srcFormat, trgFormatComponent, "escidoc"); if (componentBytes != null) { String componentXml = new String(componentBytes, this.enc); InitialContext initialContext = new InitialContext(); de.mpg.mpdl.inge.xmltransforming.XmlTransforming xmlTransforming = (de.mpg.mpdl.inge.xmltransforming.XmlTransforming) initialContext .lookup(de.mpg.mpdl.inge.xmltransforming.XmlTransforming.SERVICE_NAME); this.componentVO = xmlTransforming.transformToFileVO(componentXml); } } } catch (Exception e) { this.logger.info("No component was created from external sources metadata"); } } this.setContentType(trgFormatType); } catch (AccessException e) { this.logger.error("Access denied.", e); throw new AccessException(this.currentSource.getName()); } catch (IdentifierNotRecognisedException e) { this.logger.error("The Identifier " + identifier + "was not recognized by source " + this.currentSource.getName() + ".", e); throw new IdentifierNotRecognisedException(e); } catch (BadArgumentException e) { this.logger.error("The request contained illegal arguments", e); throw new RuntimeException(e); } catch (FormatNotRecognisedException e) { this.logger.error("The requested format was not recognised by the import source", e); throw new FormatNotRecognisedException(e); } catch (Exception e) { throw new RuntimeException(e); } return item; }
From source file:de.mpg.escidoc.services.dataacquisition.DataHandlerBean.java
/** * Operation for fetching data of type TEXTUALDATA. * //from w w w. j a v a2s . c om * @param identifier * @param format * @return itemXML * @throws IdentifierNotRecognisedException * @throws SourceNotAvailableException * @throws AccessException * @throws FormatNotSupportedException */ private String fetchTextualData(String identifier, String trgFormatName, String trgFormatType, String trgFormatEncoding) throws IdentifierNotRecognisedException, AccessException, SourceNotAvailableException, FormatNotAvailableException, FormatNotRecognisedException { String fetchedItem = null; String item = null; boolean supportedProtocol = false; ProtocolHandler protocolHandler = new ProtocolHandler(); try { MetadataVO md = this.util.getMdObjectToFetch(this.currentSource, trgFormatName, trgFormatType, trgFormatEncoding); if (md == null) { return null; } String decoded = java.net.URLDecoder.decode(md.getMdUrl().toString(), this.currentSource.getEncoding()); md.setMdUrl(new URL(decoded)); md.setMdUrl(new URL(md.getMdUrl().toString().replaceAll(this.regex, identifier.trim()))); this.currentSource = this.sourceHandler.updateMdEntry(this.currentSource, md); // Select harvesting method if (this.currentSource.getHarvestProtocol().equalsIgnoreCase("oai-pmh")) { this.logger.debug("Fetch OAI record from URL: " + md.getMdUrl()); item = fetchOAIRecord(md); // Check the record for error codes protocolHandler.checkOAIRecord(item); supportedProtocol = true; } if (this.currentSource.getHarvestProtocol().equalsIgnoreCase("ejb")) { this.logger.debug("Fetch record via EJB."); item = this.fetchEjbRecord(md, identifier); supportedProtocol = true; } if (this.currentSource.getHarvestProtocol().equalsIgnoreCase("http")) { this.logger.debug("Fetch record via http."); item = this.fetchHttpRecord(md); supportedProtocol = true; } if (!supportedProtocol) { this.logger .warn("Harvesting protocol " + this.currentSource.getHarvestProtocol() + " not supported."); throw new RuntimeException(); } fetchedItem = item; // Transform the itemXML if necessary if (item != null && !trgFormatName.trim().equalsIgnoreCase(md.getName().toLowerCase())) { TransformationBean transformer = new TransformationBean(); // Transform item metadata Format srcFormat = new Format(md.getName(), md.getMdFormat(), "*"); Format trgFormat = new Format(trgFormatName, trgFormatType, trgFormatEncoding); item = new String(transformer.transform(item.getBytes(this.enc), srcFormat, trgFormat, "escidoc"), this.enc); if (this.currentSource.getItemUrl() != null) { this.setItemUrl( new URL(this.currentSource.getItemUrl().toString().replace("GETID", identifier))); } try { // Create component if supported String name = trgFormatName.replace("item", "component"); Format trgFormatComponent = new Format(name, trgFormatType, trgFormatEncoding); if (transformer.checkTransformation(srcFormat, trgFormatComponent)) { byte[] componentBytes = transformer.transform(fetchedItem.getBytes(this.enc), srcFormat, trgFormatComponent, "escidoc"); if (componentBytes != null) { String componentXml = new String(componentBytes, this.enc); InitialContext initialContext = new InitialContext(); XmlTransforming xmlTransforming = (XmlTransforming) initialContext .lookup(XmlTransforming.SERVICE_NAME); this.componentVO = xmlTransforming.transformToFileVO(componentXml); } } } catch (Exception e) { this.logger.info("No component was created from external sources metadata"); } } this.setContentType(trgFormatType); } catch (AccessException e) { this.logger.error("Access denied.", e); throw new AccessException(this.currentSource.getName()); } catch (IdentifierNotRecognisedException e) { this.logger.error("The Identifier " + identifier + "was not recognized by source " + this.currentSource.getName() + ".", e); throw new IdentifierNotRecognisedException(e); } catch (BadArgumentException e) { this.logger.error("The request contained illegal arguments", e); throw new RuntimeException(e); } catch (FormatNotRecognisedException e) { this.logger.error("The requested format was not recognised by the import source", e); throw new FormatNotRecognisedException(e); } catch (Exception e) { throw new RuntimeException(e); } return item; }
From source file:de.mpg.escidoc.services.dataacquisition.DataHandlerBean.java
/** * fetch data from a given url.//ww w.j a v a 2 s. c om * * @param url * @return byte[] * @throws SourceNotAvailableException * @throws RuntimeException * @throws AccessException */ public byte[] fetchMetadatafromURL(URL url) throws SourceNotAvailableException, RuntimeException, AccessException { byte[] input = null; URLConnection conn = null; Date retryAfter = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(baos); try { conn = ProxyHelper.openConnection(url); HttpURLConnection httpConn = (HttpURLConnection) conn; int responseCode = httpConn.getResponseCode(); switch (responseCode) { case 503: String retryAfterHeader = conn.getHeaderField("Retry-After"); if (retryAfterHeader != null) { SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); retryAfter = dateFormat.parse(retryAfterHeader); this.logger.debug("Source responded with 503, retry after " + retryAfter + "."); throw new SourceNotAvailableException(retryAfter); } break; case 302: String alternativeLocation = conn.getHeaderField("Location"); return fetchMetadatafromURL(new URL(alternativeLocation)); case 200: this.logger.info("Source responded with 200."); // Fetch file GetMethod method = new GetMethod(url.toString()); HttpClient client = new HttpClient(); ProxyHelper.executeMethod(client, method); input = method.getResponseBody(); httpConn.disconnect(); // Create zip file with fetched file ZipEntry ze = new ZipEntry("unapi"); ze.setSize(input.length); ze.setTime(this.currentDate()); CRC32 crc321 = new CRC32(); crc321.update(input); ze.setCrc(crc321.getValue()); zos.putNextEntry(ze); zos.write(input); zos.flush(); zos.closeEntry(); zos.close(); this.setContentType("application/zip"); this.setFileEnding(".zip"); break; case 403: throw new AccessException("Access to url " + url + " is restricted."); default: throw new RuntimeException("An error occurred during importing from external system: " + responseCode + ": " + httpConn.getResponseMessage() + "."); } } catch (AccessException e) { this.logger.error("Access denied.", e); throw new AccessException(url.toString()); } catch (Exception e) { throw new RuntimeException(e); } return baos.toByteArray(); }
From source file:de.mpg.mpdl.inge.dataacquisition.DataHandlerBean.java
/** * Handlers the http request to fetch a file from an external source. * //from w ww .ja v a 2s . co m * @param importSource * @param fulltext * @return byte[] of the fetched file * @throws SourceNotAvailableException * @throws RuntimeException */ private byte[] fetchFile(FullTextVO fulltext) throws SourceNotAvailableException, RuntimeException, AccessException, FormatNotAvailableException { URLConnection conn = null; byte[] input = null; try { conn = ProxyHelper.openConnection(fulltext.getFtUrl()); HttpURLConnection httpConn = (HttpURLConnection) conn; int responseCode = httpConn.getResponseCode(); switch (responseCode) { case 503: // request was not processed by source this.logger.warn("Import source " + this.currentSource.getName() + "did not provide data in format " + fulltext.getFtLabel()); throw new FormatNotAvailableException(fulltext.getFtLabel()); case 302: String alternativeLocation = conn.getHeaderField("Location"); fulltext.setFtUrl(new URL(alternativeLocation)); return fetchFile(fulltext); case 200: this.logger.info("Source responded with 200."); GetMethod method = new GetMethod(fulltext.getFtUrl().toString()); HttpClient client = new HttpClient(); ProxyHelper.executeMethod(client, method); input = method.getResponseBody(); httpConn.disconnect(); break; case 403: throw new AccessException("Access to url " + this.currentSource.getName() + " is restricted."); default: throw new RuntimeException("An error occurred during importing from external system: " + responseCode + ": " + httpConn.getResponseMessage()); } } catch (AccessException e) { this.logger.error("Access denied.", e); throw new AccessException(this.currentSource.getName()); } catch (Exception e) { throw new RuntimeException(e); } // bmc needs transformation from xml to html if (this.currentSource.getName().equalsIgnoreCase("BioMed Central") && fulltext.getFtFormat().equalsIgnoreCase("text/html")) { Format from = new Format("bmc_fulltext_xml", "application/xml", "*"); Format to = new Format("bmc_fulltext_html", "text/html", "*"); TransformationBean transformer = new TransformationBean(); try { input = transformer.transform(input, from, to, "escidoc"); } catch (Exception e) { this.logger.error("Could not transform BMC fulltext", e); } } else if (this.currentSource.getName().equalsIgnoreCase("PubMedCentral") && fulltext.getFtFormat().equalsIgnoreCase("application/pdf")) { // pmc pdf is generated from oai-pmh-xml Format from = new Format("pmc_fulltext_xml", "application/xml", "*"); Format to = new Format("pmc_fulltext_xslfo", "text/xsl", "*"); TransformationBean transformer = new TransformationBean(); byte[] xslFo = null; try { xslFo = transformer.transform(input, from, to, "escidoc"); } catch (Exception e) { this.logger.error("Could not transform PMC fulltext", e); } // Step 1: Construct a FopFactory FopFactory fopFactory = FopFactory.newInstance(); // Step 2: Set up output stream. ByteArrayOutputStream outputPdf = new ByteArrayOutputStream(); try { // Trying to load FOP-Configuration from the pubman.properties fopFactory.setUserConfig(new File( PropertyReader.getProperty("escidoc.dataacquisition.resources.fop.configuration"))); } catch (Exception e) { try { logger.info("FopFactory configuration couldn't be loaded from '" + PropertyReader.getProperty("escidoc.dataacquisition.resources.fop.configuration") + "'", e); // loading in-EAR configuration an fonts String dataaquisitionUrl = DataHandlerBean.class.getClassLoader().getResource("dataaquisition/") .toString(); logger.info("Trying to load FopFactory from: '" + dataaquisitionUrl + "apache-fop-config.xml'"); fopFactory.setUserConfig(dataaquisitionUrl + "apache-fop-config.xml"); fopFactory.setBaseURL(dataaquisitionUrl); fopFactory.getFontManager().setFontBaseURL(dataaquisitionUrl + "fonts/"); if (logger.isDebugEnabled()) { logger.debug(fopFactory.getBaseURL()); logger.debug(fopFactory.getFontManager().getFontBaseURL()); } } catch (Exception exception) { logger.error("FopFactory configuration wasn't loaded correctly", exception); } } try { // Step 3: Construct fop with desired output format Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, outputPdf); SortedMap map = (new org.apache.fop.tools.fontlist.FontListGenerator()).listFonts(fopFactory, MimeConstants.MIME_PDF, new org.apache.fop.fonts.FontEventAdapter( new org.apache.fop.events.DefaultEventBroadcaster())); // Step 4: Setup JAXP using identity transformer TransformerFactory factory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", null); Transformer xmlTransformer = factory.newTransformer(); // identity transformer // Step 5: Setup input and output for XSLT transformation Source src = new StreamSource((InputStream) (new ByteArrayInputStream(xslFo))); // Resulting SAX events (the generated FO) must be piped through to FOP Result res = new SAXResult(fop.getDefaultHandler()); // Step 6: Start XSLT transformation and FOP processing xmlTransformer.transform(src, res); // setting pdf as result input = outputPdf.toByteArray(); } catch (Exception e) { logger.error("Error when trying to transform xsl-FO to PDF (Apache-FOP): ", e); } finally { // Clean-up try { outputPdf.close(); } catch (IOException e) { logger.error("Couldn't close outputPdf-Stream", e); } } } return input; }
From source file:de.mpg.escidoc.services.dataacquisition.DataHandlerBean.java
/** * Handlers the http request to fetch a file from an external source. * /* w ww . j a v a 2 s . co m*/ * @param importSource * @param fulltext * @return byte[] of the fetched file * @throws SourceNotAvailableException * @throws RuntimeException */ private byte[] fetchFile(FullTextVO fulltext) throws SourceNotAvailableException, RuntimeException, AccessException, FormatNotAvailableException { URLConnection conn = null; byte[] input = null; try { conn = ProxyHelper.openConnection(fulltext.getFtUrl()); HttpURLConnection httpConn = (HttpURLConnection) conn; int responseCode = httpConn.getResponseCode(); switch (responseCode) { case 503: // request was not processed by source this.logger.warn("Import source " + this.currentSource.getName() + "did not provide data in format " + fulltext.getFtLabel()); throw new FormatNotAvailableException(fulltext.getFtLabel()); case 302: String alternativeLocation = conn.getHeaderField("Location"); fulltext.setFtUrl(new URL(alternativeLocation)); return fetchFile(fulltext); case 200: this.logger.info("Source responded with 200."); GetMethod method = new GetMethod(fulltext.getFtUrl().toString()); HttpClient client = new HttpClient(); ProxyHelper.executeMethod(client, method); input = method.getResponseBody(); httpConn.disconnect(); break; case 403: throw new AccessException("Access to url " + this.currentSource.getName() + " is restricted."); default: throw new RuntimeException("An error occurred during importing from external system: " + responseCode + ": " + httpConn.getResponseMessage()); } } catch (AccessException e) { this.logger.error("Access denied.", e); throw new AccessException(this.currentSource.getName()); } catch (Exception e) { throw new RuntimeException(e); } // bmc needs transformation from xml to html if (this.currentSource.getName().equalsIgnoreCase("BioMed Central") && fulltext.getFtFormat().equalsIgnoreCase("text/html")) { Format from = new Format("bmc_fulltext_xml", "application/xml", "*"); Format to = new Format("bmc_fulltext_html", "text/html", "*"); TransformationBean transformer = new TransformationBean(); try { input = transformer.transform(input, from, to, "escidoc"); } catch (Exception e) { this.logger.error("Could not transform BMC fulltext", e); } } else if (this.currentSource.getName().equalsIgnoreCase("PubMedCentral") && fulltext.getFtFormat().equalsIgnoreCase("application/pdf")) { // pmc pdf is generated from oai-pmh-xml Format from = new Format("pmc_fulltext_xml", "application/xml", "*"); Format to = new Format("pmc_fulltext_xslfo", "text/xsl", "*"); TransformationBean transformer = new TransformationBean(); byte[] xslFo = null; try { xslFo = transformer.transform(input, from, to, "escidoc"); } catch (Exception e) { this.logger.error("Could not transform PMC fulltext", e); } // Step 1: Construct a FopFactory FopFactory fopFactory = FopFactory.newInstance(); // Step 2: Set up output stream. ByteArrayOutputStream outputPdf = new ByteArrayOutputStream(); try { // Trying to load FOP-Configuration from the pubman.properties fopFactory.setUserConfig(new File( PropertyReader.getProperty("escidoc.dataacquisition.resources.fop.configuration"))); } catch (Exception e) { try { logger.info("FopFactory configuration couldn't be loaded from '" + PropertyReader.getProperty("escidoc.dataacquisition.resources.fop.configuration") + "'", e); // loading in-EAR configuration an fonts String dataaquisitionUrl = DataHandlerBean.class.getClassLoader().getResource("dataaquisition/") .toString(); logger.info("Trying to load FopFactory from: '" + dataaquisitionUrl + "apache-fop-config.xml'"); fopFactory.setUserConfig(dataaquisitionUrl + "apache-fop-config.xml"); fopFactory.setBaseURL(dataaquisitionUrl); fopFactory.getFontManager().setFontBaseURL(dataaquisitionUrl + "fonts/"); if (logger.isDebugEnabled()) { logger.debug(fopFactory.getBaseURL()); logger.debug(fopFactory.getFontManager().getFontBaseURL()); } } catch (Exception exception) { logger.error("FopFactory configuration wasn't loaded correctly", exception); } } try { // Step 3: Construct fop with desired output format Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, outputPdf); SortedMap map = (new org.apache.fop.tools.fontlist.FontListGenerator()).listFonts(fopFactory, MimeConstants.MIME_PDF, new org.apache.fop.fonts.FontEventAdapter( new org.apache.fop.events.DefaultEventBroadcaster())); // Step 4: Setup JAXP using identity transformer TransformerFactory factory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", null); Transformer xmlTransformer = factory.newTransformer(); // identity transformer // Step 5: Setup input and output for XSLT transformation Source src = new StreamSource((InputStream) (new ByteArrayInputStream(xslFo))); // Resulting SAX events (the generated FO) must be piped through to FOP Result res = new SAXResult(fop.getDefaultHandler()); // Step 6: Start XSLT transformation and FOP processing xmlTransformer.transform(src, res); // setting pdf as result input = outputPdf.toByteArray(); } catch (Exception e) { logger.error("Error when trying to transform xsl-FO to PDF (Apache-FOP): ", e); } finally { //Clean-up try { outputPdf.close(); } catch (IOException e) { logger.error("Couldn't close outputPdf-Stream", e); } } } return input; }
From source file:de.mpg.escidoc.services.dataacquisition.DataHandlerBean.java
/** * Fetches an OAI record for given record identifier. * // www . j a va 2 s . c o m * @param sourceURL * @return itemXML * @throws IdentifierNotRecognisedException * @throws SourceNotAvailableException * @throws RuntimeException */ private String fetchOAIRecord(MetadataVO md) throws SourceNotAvailableException, AccessException, IdentifierNotRecognisedException, RuntimeException { String itemXML = ""; URLConnection conn; Date retryAfter; InputStreamReader isReader; BufferedReader bReader; try { conn = ProxyHelper.openConnection(md.getMdUrl()); HttpURLConnection httpConn = (HttpURLConnection) conn; int responseCode = httpConn.getResponseCode(); switch (responseCode) { case 503: String retryAfterHeader = conn.getHeaderField("Retry-After"); if (retryAfterHeader != null) { SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); retryAfter = dateFormat.parse(retryAfterHeader); this.logger.debug("Source responded with 503, retry after " + retryAfter + "."); throw new SourceNotAvailableException(retryAfter); } else { this.logger.debug( "Source responded with 503, retry after " + this.currentSource.getRetryAfter() + "."); throw new SourceNotAvailableException(this.currentSource.getRetryAfter()); } case 302: String alternativeLocation = conn.getHeaderField("Location"); md.setMdUrl(new URL(alternativeLocation)); this.currentSource = this.sourceHandler.updateMdEntry(this.currentSource, md); return fetchOAIRecord(md); case 200: this.logger.info("Source responded with 200"); break; case 403: throw new AccessException("Access to url " + this.currentSource.getName() + " is restricted."); default: throw new RuntimeException("An error occurred during importing from external system: " + responseCode + ": " + httpConn.getResponseMessage()); } // Get itemXML isReader = new InputStreamReader(md.getMdUrl().openStream(), this.enc); bReader = new BufferedReader(isReader); String line = ""; while ((line = bReader.readLine()) != null) { itemXML += line + "\n"; } httpConn.disconnect(); } catch (AccessException e) { this.logger.error("Access denied.", e); throw new AccessException(this.currentSource.getName()); } catch (Exception e) { throw new RuntimeException(e); } return itemXML; }
From source file:de.mpg.mpdl.inge.dataacquisition.DataHandlerBean.java
/** * Fetches a eSciDoc Record from eSciDoc system. * /*from w w w . ja va2s . co m*/ * @param identifier of the item * @return itemXML as String * @throws IdentifierNotRecognisedException * @throws RuntimeException */ private byte[] fetchEjbFile(FullTextVO ft, String identifier) throws IdentifierNotRecognisedException, RuntimeException { String itemXML = ""; String coreservice = ""; URLConnection contentUrl = null; de.mpg.mpdl.inge.xmltransforming.XmlTransforming xmlTransforming = new de.mpg.mpdl.inge.xmltransforming.xmltransforming.XmlTransformingBean(); byte[] input = null; try { if (this.currentSource.getName().equalsIgnoreCase("escidoc")) { itemXML = ServiceLocator.getItemHandler().retrieve(identifier); coreservice = PropertyReader.getFrameworkUrl(); } if (this.currentSource.getName().equalsIgnoreCase("escidocdev") || this.currentSource.getName().equalsIgnoreCase("escidocqa") || this.currentSource.getName().equalsIgnoreCase("escidocprod")) { itemXML = ServiceLocator.getItemHandler(ft.getFtUrl().toString()).retrieve(identifier); coreservice = ft.getFtUrl().toString(); } PubItemVO itemVO = xmlTransforming.transformToPubItem(itemXML); contentUrl = ProxyHelper.openConnection(new URL(coreservice + itemVO.getFiles().get(0).getContent())); HttpURLConnection httpConn = (HttpURLConnection) contentUrl; int responseCode = httpConn.getResponseCode(); switch (responseCode) { case 503: // request was not processed by source this.logger.warn("Import source " + this.currentSource.getName() + "did not provide file."); throw new FormatNotAvailableException(ft.getFtLabel()); case 302: String alternativeLocation = contentUrl.getHeaderField("Location"); ft.setFtUrl(new URL(alternativeLocation)); return fetchEjbFile(ft, identifier); case 200: this.logger.info("Source responded with 200."); GetMethod method = new GetMethod(coreservice + itemVO.getFiles().get(0).getContent()); HttpClient client = new HttpClient(); ProxyHelper.executeMethod(client, method); input = method.getResponseBody(); httpConn.disconnect(); break; case 403: throw new AccessException("Access to url " + this.currentSource.getName() + " is restricted."); default: throw new RuntimeException("An error occurred during importing from external system: " + responseCode + ": " + httpConn.getResponseMessage()); } } catch (ItemNotFoundException e) { this.logger.error("Item with identifier " + identifier + " was not found.", e); throw new IdentifierNotRecognisedException(e); } catch (Exception e) { this.logger.error("An error occurred while retrieving the item " + identifier + ".", e); throw new RuntimeException(e); } return input; }
From source file:de.mpg.escidoc.services.dataacquisition.DataHandlerBean.java
/** * Fetches a eSciDoc Record from eSciDoc system. * // w w w .ja v a 2 s . co m * @param identifier * of the item * @return itemXML as String * @throws IdentifierNotRecognisedException * @throws RuntimeException */ private byte[] fetchEjbFile(FullTextVO ft, String identifier) throws IdentifierNotRecognisedException, RuntimeException { String itemXML = ""; String coreservice = ""; URLConnection contentUrl = null; XmlTransforming xmlTransforming = new XmlTransformingBean(); byte[] input = null; try { if (this.currentSource.getName().equalsIgnoreCase("escidoc")) { itemXML = ServiceLocator.getItemHandler().retrieve(identifier); coreservice = ServiceLocator.getFrameworkUrl(); } if (this.currentSource.getName().equalsIgnoreCase("escidocdev") || this.currentSource.getName().equalsIgnoreCase("escidocqa") || this.currentSource.getName().equalsIgnoreCase("escidocprod")) { itemXML = ServiceLocator.getItemHandler(ft.getFtUrl().toString()).retrieve(identifier); coreservice = ft.getFtUrl().toString(); } PubItemVO itemVO = xmlTransforming.transformToPubItem(itemXML); contentUrl = ProxyHelper.openConnection(new URL(coreservice + itemVO.getFiles().get(0).getContent())); HttpURLConnection httpConn = (HttpURLConnection) contentUrl; int responseCode = httpConn.getResponseCode(); switch (responseCode) { case 503: // request was not processed by source this.logger.warn("Import source " + this.currentSource.getName() + "did not provide file."); throw new FormatNotAvailableException(ft.getFtLabel()); case 302: String alternativeLocation = contentUrl.getHeaderField("Location"); ft.setFtUrl(new URL(alternativeLocation)); return fetchEjbFile(ft, identifier); case 200: this.logger.info("Source responded with 200."); GetMethod method = new GetMethod(coreservice + itemVO.getFiles().get(0).getContent()); HttpClient client = new HttpClient(); ProxyHelper.executeMethod(client, method); input = method.getResponseBody(); httpConn.disconnect(); break; case 403: throw new AccessException("Access to url " + this.currentSource.getName() + " is restricted."); default: throw new RuntimeException("An error occurred during importing from external system: " + responseCode + ": " + httpConn.getResponseMessage()); } } catch (ItemNotFoundException e) { this.logger.error("Item with identifier " + identifier + " was not found.", e); throw new IdentifierNotRecognisedException(e); } catch (Exception e) { this.logger.error("An error occurred while retrieving the item " + identifier + ".", e); throw new RuntimeException(e); } return input; }
From source file:de.mpg.escidoc.services.dataacquisition.DataHandlerBean.java
/** * Fetches a record via http protocol./*from www. j av a 2s .c om*/ * * @param importSource * @param md * @return * @throws IdentifierNotRecognisedException * @throws RuntimeException * @throws AccessException */ private String fetchHttpRecord(MetadataVO md) throws IdentifierNotRecognisedException, RuntimeException, AccessException { String item = ""; URLConnection conn; String charset = this.currentSource.getEncoding(); InputStreamReader isReader; BufferedReader bReader; try { conn = ProxyHelper.openConnection(md.getMdUrl()); HttpURLConnection httpConn = (HttpURLConnection) conn; int responseCode = httpConn.getResponseCode(); switch (responseCode) { case 503: // request was not processed by source this.logger.warn("Import source " + this.currentSource.getName() + "did not provide file."); throw new FormatNotAvailableException(md.getMdLabel()); case 302: String alternativeLocation = conn.getHeaderField("Location"); md.setMdUrl(new URL(alternativeLocation)); this.currentSource = this.sourceHandler.updateMdEntry(this.currentSource, md); return fetchHttpRecord(md); case 200: this.logger.info("Source responded with 200"); break; case 403: throw new AccessException("Access to url " + this.currentSource.getName() + " is restricted."); default: throw new RuntimeException("An error occurred during importing from external system: " + responseCode + ": " + httpConn.getResponseMessage()); } // Get itemXML isReader = new InputStreamReader(md.getMdUrl().openStream(), charset); bReader = new BufferedReader(isReader); String line = ""; while ((line = bReader.readLine()) != null) { item += line + "\n"; } httpConn.disconnect(); } catch (AccessException e) { this.logger.error("Access denied.", e); throw new AccessException(this.currentSource.getName()); } catch (Exception e) { throw new RuntimeException(e); } return item; }