Java Code Examples for com.sedmelluq.discord.lavaplayer.tools.ExceptionTools#closeWithWarnings()

The following examples show how to use com.sedmelluq.discord.lavaplayer.tools.ExceptionTools#closeWithWarnings() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpStreamTools.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
public static InputStream streamContent(HttpInterface httpInterface, HttpUriRequest request) {
  CloseableHttpResponse response = null;
  boolean success = false;

  try {
    response = httpInterface.execute(request);
    int statusCode = response.getStatusLine().getStatusCode();

    if (!HttpClientTools.isSuccessWithContent(statusCode)) {
      throw new IOException("Invalid status code from " + request.getURI() + " URL: " + statusCode);
    }

    success = true;
    return response.getEntity().getContent();
  } catch (IOException e) {
    throw new RuntimeException(e);
  } finally {
    if (response != null && !success) {
      ExceptionTools.closeWithWarnings(response);
    }
  }
}
 
Example 2
Source File: TrustManagerBuilder.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
private void addFromResourceList(String basePath, String listPath) throws Exception {
  InputStream listFileStream = TrustManagerBuilder.class.getResourceAsStream(listPath);

  if (listFileStream == null) {
    log.debug("Certificate list {} not present in classpath.", listPath);
    return;
  }

  try {
    for (String line : IOUtils.readLines(listFileStream, StandardCharsets.UTF_8)) {
      String fileName = line.trim();

      if (!fileName.isEmpty()) {
        addFromResourceFile(basePath + "/" + fileName);
      }
    }
  } finally {
    ExceptionTools.closeWithWarnings(listFileStream);
  }
}
 
Example 3
Source File: TrustManagerBuilder.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
private void addFromResourceFile(String resourcePath) throws Exception {
  InputStream fileStream = TrustManagerBuilder.class.getResourceAsStream(resourcePath);

  if (fileStream == null) {
    log.warn("Certificate {} not present in classpath.", resourcePath);
    return;
  }

  try {
    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(fileStream, null);
    addFromKeyStore(keyStore);
  } finally {
    ExceptionTools.closeWithWarnings(fileStream);
  }
}
 
Example 4
Source File: MatroskaAudioTrack.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
private MatroskaTrackConsumer loadAudioTrack(MatroskaStreamingFile file, AudioProcessingContext context) {
  MatroskaTrackConsumer trackConsumer = null;
  boolean success = false;

  try {
    trackConsumer = selectAudioTrack(file.getTrackList(), context);

    if (trackConsumer == null) {
      throw new IllegalStateException("No supported audio tracks in the file.");
    } else {
      log.debug("Starting to play track with codec {}", trackConsumer.getTrack().codecId);
    }

    trackConsumer.initialise();
    success = true;
  } finally {
    if (!success && trackConsumer != null) {
      ExceptionTools.closeWithWarnings(trackConsumer);
    }
  }

  return trackConsumer;
}
 
Example 5
Source File: RemoteNodeManager.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
/**
 * Shut down, freeing all threads and stopping all tracks executed on remote nodes.
 * @param terminal True if initialise will never be called again.
 */
public void shutdown(boolean terminal) {
  synchronized (lock) {
    if (!enabled.compareAndSet(true, false)) {
      return;
    }

    ExecutorTools.shutdownExecutor(scheduler, "node manager");

    for (RemoteNodeProcessor processor : processors) {
      processor.processHealthCheck(true);
    }

    abandonedTrackManager.shutdown();

    processors.clear();
    activeProcessors = new ArrayList<>(processors);
  }

  if (terminal) {
    ExceptionTools.closeWithWarnings(httpInterfaceManager);
  }
}
 
Example 6
Source File: M3uStreamSegmentUrlProvider.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
/**
 * Fetches the input stream for the next segment in the M3U stream.
 *
 * @param httpInterface HTTP interface to use for any requests required to perform to find the segment URL.
 * @return Input stream of the next segment.
 */
public InputStream getNextSegmentStream(HttpInterface httpInterface) {
  String url = getNextSegmentUrl(httpInterface);
  if (url == null) {
    return null;
  }

  CloseableHttpResponse response = null;
  boolean success = false;

  try {
    response = httpInterface.execute(createSegmentGetRequest(url));
    int statusCode = response.getStatusLine().getStatusCode();

    if (!HttpClientTools.isSuccessWithContent(statusCode)) {
      throw new IOException("Invalid status code from segment data URL: " + statusCode);
    }

    success = true;
    return response.getEntity().getContent();
  } catch (IOException e) {
    throw new RuntimeException(e);
  } finally {
    if (response != null && !success) {
      ExceptionTools.closeWithWarnings(response);
    }
  }
}
 
Example 7
Source File: MatroskaAudioTrack.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
@Override
public void process(LocalAudioTrackExecutor localExecutor) {
  MatroskaStreamingFile file = loadMatroskaFile();
  MatroskaTrackConsumer trackConsumer = loadAudioTrack(file, localExecutor.getProcessingContext());

  try {
    localExecutor.executeProcessingLoop(() -> {
      file.provideFrames(trackConsumer);
    }, position -> {
      file.seekToTimecode(trackConsumer.getTrack().index, position);
    });
  } finally {
    ExceptionTools.closeWithWarnings(trackConsumer);
  }
}
 
Example 8
Source File: YoutubeAudioSourceManager.java    From kyoko with MIT License 4 votes vote down vote up
@Override
public void shutdown() {
    ExceptionTools.closeWithWarnings(httpInterfaceManager);

    mixProvider.shutdown();
}
 
Example 9
Source File: TwitchStreamAudioSourceManager.java    From lavaplayer with Apache License 2.0 4 votes vote down vote up
@Override
public void shutdown() {
  ExceptionTools.closeWithWarnings(httpInterfaceManager);
}
 
Example 10
Source File: BandcampAudioSourceManager.java    From lavaplayer with Apache License 2.0 4 votes vote down vote up
@Override
public void shutdown() {
  ExceptionTools.closeWithWarnings(httpInterfaceManager);
}
 
Example 11
Source File: VimeoAudioSourceManager.java    From lavaplayer with Apache License 2.0 4 votes vote down vote up
@Override
public void shutdown() {
  ExceptionTools.closeWithWarnings(httpInterfaceManager);
}
 
Example 12
Source File: YoutubeAudioSourceManager.java    From lavaplayer with Apache License 2.0 4 votes vote down vote up
@Override
public void shutdown() {
  ExceptionTools.closeWithWarnings(httpInterfaceManager);
}
 
Example 13
Source File: RemoteNodeProcessor.java    From lavaplayer with Apache License 2.0 4 votes vote down vote up
private boolean dispatchOneTick(HttpInterface httpInterface, TickBuilder tickBuilder) throws Exception {
  boolean success = false;
  HttpPost post = new HttpPost("http://" + nodeAddress + "/tick");

  abandonedTrackManager.distribute(Collections.singletonList(this));

  ByteArrayEntity entity = new ByteArrayEntity(buildRequestBody());
  post.setEntity(entity);

  tickBuilder.requestSize = (int) entity.getContentLength();

  CloseableHttpResponse response = httpInterface.execute(post);

  try {
    tickBuilder.responseCode = response.getStatusLine().getStatusCode();
    if (tickBuilder.responseCode != HttpStatus.SC_OK) {
      throw new IOException("Returned an unexpected response code " + tickBuilder.responseCode);
    }

    if (connectionState.compareAndSet(ConnectionState.PENDING.id(), ConnectionState.ONLINE.id())) {
      log.info("Node {} came online.", nodeAddress);
    } else if (connectionState.get() != ConnectionState.ONLINE.id()) {
      log.warn("Node {} received successful response, but had already lost control of its tracks.", nodeAddress);
      return false;
    }

    lastAliveTime = System.currentTimeMillis();

    if (!handleResponseBody(response.getEntity().getContent(), tickBuilder)) {
      return false;
    }

    success = true;
  } finally {
    if (!success) {
      ExceptionTools.closeWithWarnings(response);
    } else {
      ExceptionTools.closeWithWarnings(response.getEntity().getContent());
    }
  }

  return true;
}