All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ja.smarkdown.location.http.AbstractHttpListingProvider Maven / Gradle / Ivy

There is a newer version: 0.8.0
Show newest version
package com.ja.smarkdown.location.http;

import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.inject.Inject;

import lombok.extern.slf4j.Slf4j;

import com.ja.smarkdown.json.ListingParser;
import com.ja.smarkdown.load.AbstractListingProvider;
import com.ja.smarkdown.load.MountPointUtil;
import com.ja.smarkdown.model.Listing;

@Slf4j
public abstract class AbstractHttpListingProvider extends
		AbstractListingProvider {

	@Inject
	private ListingParser parser;

	@Override
	protected void readDocumentsFromListingFile(HttpLocation location,
			List documents, String listingFileName) {
		try {
			final URL listingFile = resolveListingFileUrl(location,
					listingFileName);
			try (Reader in = new InputStreamReader(listingFile.openStream())) {
				final Listing listing = parser.parse(in);
				for (final String file : listing.getFiles()) {
					documents.add(transformFileName(location, file));
				}
			}
		} catch (Exception e) {
			log.error("Failed to read the listing file for location={}",
					location, e);
		}
	}

	protected URL resolveListingFileUrl(HttpLocation location,
			String listingFileName) throws MalformedURLException {
		return new URL(String.format("%s/%s", location.getUrl(),
				listingFileName));
	}

	protected String transformFileName(HttpLocation location, final String file) {
		return MountPointUtil.apply(location, file);
	}

	@Override
	protected void readDocuments(final HttpLocation location,
			final List documents) {
		// not supported. We can only read the listing file.
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy