Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sshd.common.config.keys.loader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StreamCorruptedException;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import org.apache.sshd.common.NamedResource;
import org.apache.sshd.common.config.keys.FilePasswordProvider;
import org.apache.sshd.common.session.SessionContext;
import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.ValidateUtils;
import org.apache.sshd.common.util.buffer.BufferUtils;
import org.apache.sshd.common.util.logging.AbstractLoggingBean;
/**
* @author Apache MINA SSHD Project
*/
public abstract class AbstractKeyPairResourceParser extends AbstractLoggingBean implements KeyPairResourceParser {
private final List beginners;
private final List enders;
private final List> endingMarkers;
/**
* @param beginners The markers that indicate the beginning of a parsing block
* @param enders The matching (by position) markers that indicate the end of a parsing block
*/
protected AbstractKeyPairResourceParser(List beginners, List enders) {
this.beginners = ValidateUtils.checkNotNullAndNotEmpty(beginners, "No begin markers");
this.enders = ValidateUtils.checkNotNullAndNotEmpty(enders, "No end markers");
ValidateUtils.checkTrue(
beginners.size() == enders.size(),
"Mismatched begin(%d)/end(%d) markers sizes",
beginners.size(), enders.size());
endingMarkers = new ArrayList<>(enders.size());
enders.forEach(m -> endingMarkers.add(Collections.singletonList(m)));
}
public List getBeginners() {
return beginners;
}
public List getEnders() {
return enders;
}
/**
* @return A {@link List} of same size as the ending markers, where each ending marker is encapsulated inside a
* singleton list and resides as the same index as the marker it encapsulates
*/
public List> getEndingMarkers() {
return endingMarkers;
}
@Override
public boolean canExtractKeyPairs(NamedResource resourceKey, List lines)
throws IOException, GeneralSecurityException {
return KeyPairResourceParser.containsMarkerLine(lines, getBeginners());
}
@Override
public Collection loadKeyPairs(
SessionContext session, NamedResource resourceKey, FilePasswordProvider passwordProvider, List lines)
throws IOException, GeneralSecurityException {
Collection keyPairs = Collections.emptyList();
List beginMarkers = getBeginners();
List> endMarkers = getEndingMarkers();
for (Map.Entry markerPos = KeyPairResourceParser.findMarkerLine(lines, beginMarkers);
markerPos != null;) {
int startIndex = markerPos.getKey();
String startLine = lines.get(startIndex);
startIndex++;
int markerIndex = markerPos.getValue();
List ender = endMarkers.get(markerIndex);
markerPos = KeyPairResourceParser.findMarkerLine(lines, startIndex, ender);
if (markerPos == null) {
throw new StreamCorruptedException("Missing end marker (" + ender + ") after line #" + startIndex);
}
int endIndex = markerPos.getKey();
String endLine = lines.get(endIndex);
Map.Entry, ? extends List> result = separateDataLinesFromHeaders(
session, resourceKey, startLine, endLine, lines.subList(startIndex, endIndex));
Map headers = result.getKey();
List dataLines = result.getValue();
Collection kps = extractKeyPairs(
session, resourceKey, startLine, endLine, passwordProvider,
(dataLines == null) ? Collections.emptyList() : dataLines,
(headers == null) ? Collections.emptyMap() : headers);
if (GenericUtils.isNotEmpty(kps)) {
if (GenericUtils.isEmpty(keyPairs)) {
keyPairs = new LinkedList<>(kps);
} else {
keyPairs.addAll(kps);
}
}
// see if there are more
markerPos = KeyPairResourceParser.findMarkerLine(lines, endIndex + 1, beginMarkers);
}
return keyPairs;
}
protected Map.Entry